利用itextsharp.dll 导出pdf格式文件

导入pdf文件,如下图格式:

贴代码:

View Code
  1  public static string connectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
  2     protected void Page_Load(object sender, EventArgs e)
  3     {
  4         if (!IsPostBack)
  5         {
  6             ImportPdf();
  7         }
  8     }
  9     #region 绑定数据
 10     public DataTable BindData()
 11     {
 12         string oracleSql = "select * from ORDER_DETAILS t where  ForPerson ='李援朝' AND to_date(to_char(shippeddate,'yyyy-MM-dd'),'yyyy-MM-dd')=to_date('2011-10-8','yyyy-MM-dd')";
 13         using (OracleConnection con = new OracleConnection(connectionString))
 14         {
 15             con.Open();
 16             using (OracleCommand cmd = con.CreateCommand())
 17             {
 18                 cmd.CommandText = oracleSql;
 19                 DataSet ds = new DataSet();
 20                 OracleDataAdapter da = new OracleDataAdapter(cmd);
 21                 da.Fill(ds);
 22                 if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0)
 23                 {
 24                     return new DataTable();
 25                 }
 26                 return ds.Tables[0];
 27             }
 28         }
 29     }
 30     #endregion
 31 
 32     #region 数据格式化
 33     public void ImportPdf()
 34     {
 35         DataTable dt = BindData();
 36         GridView1.DataSource = dt;
 37         GridView1.DataBind();
 38         DataTable newdt = new DataTable();
 39         newdt.Columns.Add("编号", typeof(string));
 40         newdt.Columns.Add("品名", typeof(string));
 41         newdt.Columns.Add("规格", typeof(string));
 42         newdt.Columns.Add("数量", typeof(string));
 43         newdt.Columns.Add("单位", typeof(string));
 44         newdt.Columns.Add("单价", typeof(string));
 45         newdt.Columns.Add("金额", typeof(string));
 46         newdt.Columns.Add("备注", typeof(string));
 47         decimal deci = 0.0M;
 48         for (int i = 0; i < dt.Rows.Count; i++)
 49         {
 50             DataRow dr = newdt.NewRow();
 51             dr["编号"] = dt.Rows[i]["orderid"].ToString();
 52             dr["品名"] = dt.Rows[i]["productname"].ToString();
 53             dr["规格"] = dt.Rows[i]["purity"].ToString();
 54             dr["数量"] = dt.Rows[i]["quantity"].ToString();
 55             dr["单位"] = dt.Rows[i]["unit"].ToString();
 56             dr["单价"] = dt.Rows[i]["unitprice"].ToString();
 57             dr["金额"] = dt.Rows[i]["sumprice"].ToString();
 58             dr["备注"] = dt.Rows[i]["beizhu"].ToString();
 59             deci = deci + Decimal.Parse(dt.Rows[i]["sumprice"].ToString());
 60             newdt.Rows.Add(dr);
 61         }
 62         CreatePdf(newdt, deci, "李援朝研究组");
 63     }
 64     #endregion
 65 
 66     public void CreatePdf(DataTable dt, decimal sumPrice, string departmentName)
 67     {
 68         Document document = new Document(PageSize.A4, 5, 5, 20, 20);
 69         PdfWriter writer = PdfWriter.GetInstance(document, new System.IO.FileStream(HttpContext.Current.Server.MapPath("pdfsample.pdf"), System.IO.FileMode.Create));
 70         BaseFont bfChinese = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
 71         Font fontChinese1 = new Font(bfChinese, 10);
 72         Font ftitle = new Font(bfChinese, 16, Font.NORMAL, BaseColor.BLACK);
 73         Font fontRukuHeader = new Font(bfChinese, 14, Font.NORMAL, BaseColor.BLACK);
 74         Font footerChinese = new Font(bfChinese, 12, Font.NORMAL, BaseColor.BLACK);
 75         document.Open();
 76         PdfPTable table = new PdfPTable(dt.Columns.Count + 1);
 77         Paragraph title = new Paragraph("中国*******药物研究所", ftitle);
 78         title.Alignment = Element.ALIGN_CENTER;//居中
 79         document.Add(title);
 80 
 81         PdfPCell middleCell = new PdfPCell(new Phrase(departmentName + "入库传票", fontRukuHeader));
 82         middleCell.Colspan = 6;
 83         middleCell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
 84         middleCell.PaddingRight = 6;
 85         middleCell.PaddingTop = 6;
 86         middleCell.PaddingBottom = 4;
 87         middleCell.BorderWidthBottom = 0;
 88         middleCell.BorderWidthLeft = 0;
 89         middleCell.BorderWidthRight = 0;
 90         middleCell.BorderWidthTop = 0;
 91         table.AddCell(middleCell);
 92 
 93         PdfPCell cellNumber = new PdfPCell(new Phrase("连续第" + DateTime.Now.ToString("yyyyMMddssff") + "", footerChinese));
 94         cellNumber.Colspan = 3;
 95         cellNumber.PaddingRight = 6;
 96         cellNumber.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
 97         cellNumber.BorderWidthTop = 0;
 98         cellNumber.BorderWidthRight = 0;
 99         cellNumber.BorderWidthBottom = 0;
100         cellNumber.BorderWidthLeft = 0;
101         table.AddCell(cellNumber);
102 
103         PdfPCell headerleftCell = new PdfPCell(new Phrase("类别", footerChinese));
104         headerleftCell.Colspan = 4;
105         headerleftCell.BorderWidthLeft = 0;
106         headerleftCell.BorderWidthRight = 0;
107         headerleftCell.BorderWidthTop = 0;
108         headerleftCell.PaddingBottom = 4;
109         headerleftCell.HorizontalAlignment = 0;
110         headerleftCell.BorderWidth = 0;
111         table.AddCell(headerleftCell);
112 
113         PdfPCell headerMiddleCell = new PdfPCell(new Phrase(DateTime.Now.ToString("yyyy年MM月dd日"), footerChinese));
114         headerMiddleCell.Colspan = 5;
115         headerMiddleCell.HorizontalAlignment = 0;
116         headerMiddleCell.BorderWidthTop = 0;
117         headerMiddleCell.BorderWidthRight = 0;
118         headerMiddleCell.BorderWidthLeft = 0;
119         headerMiddleCell.PaddingBottom = 4;
120         headerMiddleCell.BorderWidth = 0;
121         table.AddCell(headerMiddleCell);
122 
123         table.DefaultCell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
124         table.DefaultCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
125         table.DefaultCell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
126         table.WidthPercentage = 100;
127         for (int i = 0; i < dt.Columns.Count; i++)
128         {
129             table.AddCell(new Phrase(dt.Columns[i].ColumnName, footerChinese));
130         }
131         PdfPCell cell = new PdfPCell(new Phrase("此\n联\n交\n财\n务", fontChinese1));
132         cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
133         cell.VerticalAlignment = PdfPCell.ANNOTATION;
134         cell.BorderWidth = 0;
135         cell.Colspan = 1;
136         cell.Rowspan = 3 + dt.Rows.Count;
137         table.AddCell(cell);
138         table.DefaultCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
139         for (int i = 0; i < dt.Rows.Count; i++)
140         {
141             for (int j = 0; j < dt.Columns.Count; j++)
142             {
143                 table.AddCell(new Phrase(dt.Rows[i][j].ToString(), footerChinese));
144             }
145         }
146         PdfPCell lastcell = new PdfPCell(new Phrase("   合   计   ", footerChinese));
147         lastcell.Colspan = 2;
148         lastcell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
149         table.AddCell(lastcell);
150         lastcell = new PdfPCell(new Phrase("", footerChinese));
151         table.AddCell(lastcell);
152         lastcell = new PdfPCell(new Phrase("", footerChinese));
153         table.AddCell(lastcell);
154         lastcell = new PdfPCell(new Phrase("", footerChinese));
155         table.AddCell(lastcell);
156         lastcell = new PdfPCell(new Phrase("", footerChinese));
157         table.AddCell(lastcell);
158         lastcell = new PdfPCell(new Phrase(sumPrice.ToString(), footerChinese));
159         lastcell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
160         table.AddCell(lastcell);
161         lastcell = new PdfPCell(new Phrase("", footerChinese));
162         table.AddCell(lastcell);
163 
164         PdfPCell footerCell = new PdfPCell(new Phrase("部门负责人", footerChinese));
165         footerCell.Colspan = 2;
166         footerCell.PaddingLeft = 4;
167         footerCell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
168         table.AddCell(footerCell);
169 
170         footerCell = new PdfPCell(new Phrase("记账", footerChinese));
171         footerCell.Colspan = 2;
172         footerCell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
173         table.AddCell(footerCell);
174 
175         footerCell = new PdfPCell(new Phrase("管库人员", footerChinese));
176         footerCell.Colspan = 4;
177         footerCell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
178         table.AddCell(footerCell);
179         document.Add(table);
180         document.Close();
181 
182         string fullFileName = System.Web.HttpContext.Current.Server.MapPath("pdfsample.pdf");
183         Byte[] btArray = WriteToPdf(fullFileName, DateTime.Now.ToString("yyyyMMddhhmmssff"), bfChinese);
184         fullFileName = Server.MapPath("pdfsample.pdf");
185         FileStream fs = new FileStream(fullFileName, FileMode.Create, FileAccess.Write);
186         fs.Write(btArray, 0, btArray.Length);
187         fs.Flush();
188         fs.Close();
189         fullFileName = Server.MapPath("pdfsample.pdf");
190         FileInfo downloadFile = new FileInfo(fullFileName);
191         Response.Clear();
192         Response.ClearHeaders();
193         Response.Buffer = false;
194         Response.ContentType = "application/octet-stream";
195         Response.AppendHeader("Content-Disposition", "attachment;filename=pdfsample.pdf");
196         Response.AppendHeader("Content-Length", downloadFile.Length.ToString()); System.Web.HttpContext.Current.Response.WriteFile(downloadFile.FullName);
197         Response.WriteFile(downloadFile.FullName);
198         Response.Flush();
199         Response.End();
200     }
201 
202     public static byte[] WriteToPdf(string sourceFile, string stringToWriteToPdf, BaseFont bfChinese)
203     {
204         PdfReader reader = new PdfReader(sourceFile);
205         using (MemoryStream stream = new MemoryStream())
206         {
207             PdfStamper pdfstamper = new PdfStamper(reader, stream);
208             for (int i = 1; i <= reader.NumberOfPages; i++)
209             {
210                 Rectangle pageSize = reader.GetPageSizeWithRotation(i);
211                 PdfContentByte pdfpageContents = pdfstamper.GetUnderContent(i);
212                 pdfpageContents.BeginText();
213                 pdfpageContents.SetFontAndSize(bfChinese, 40);
214                 pdfpageContents.SetRGBColorFill(192, 192, 192);
215                 float textAngle = 45.0f;
216                 pdfpageContents.ShowTextAligned(PdfContentByte.ALIGN_CENTER, stringToWriteToPdf, pageSize.Width / 2, pageSize.Height / 2, textAngle);
217                 pdfpageContents.EndText();
218             }
219             pdfstamper.FormFlattening = true;
220             pdfstamper.Close();
221             reader.Close();
222             return stream.ToArray();
223         }
224     }
原文地址:https://www.cnblogs.com/hfliyi/p/2580763.html