using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Text; using iText.IO.Font; using iText.IO.Image; using iText.Kernel.Font; using iText.Kernel.Pdf; using iText.Kernel.Pdf.Canvas.Draw; using iText.Layout; using iText.Layout.Element; using iText.Layout.Properties; using iText.Pdfa; using iText.IO; using iText.Kernel.Pdf.Xobject; using iText.Kernel.Utils; using iText.Kernel.Events; using iText.Kernel.Colors; using iText.Kernel.Geom; using iText.Kernel.Pdf.Canvas; using iText.Layout.Borders; namespace WebAppPdfDemo { /// <summary> /// iText 8.0 /// /// </summary> public partial class WebForm1 : System.Web.UI.Page { /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { try { //字体设定,c:\windows\fonts\kaiu.ttf,中文楷体 string filePath = Server.MapPath(@"sRGB_CS_profile.icm"); string fontFile = @"c:\windows\fonts\kaiu.ttf"; string fontFile2 = Server.MapPath(@"font\\MHeiHK-Light.TTF"); string fileName = Server.MapPath(@"sample\\" + DateTime.Now.ToString("yyyyMMHHmmss") + ".pdf"); List<Orders> orders = new List<Orders>(); Orders order = new Orders(); order.productId = 1; order.product = "中國電器"; order.qty = 50; order.price = 52.0; orders.Add(order); order = new Orders(); order.productId = 2; order.product = "日本電器"; order.qty = 150; order.price = 252.0; orders.Add(order); //PdfADocument pdf = new PdfADocument( //new PdfWriter(fileName), //PdfAConformanceLevel.PDF_A_1B, //new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", // new FileStream(filePath, FileMode.Open, FileAccess.Read))); PdfDocument pdf = new PdfDocument(new PdfWriter(new FileStream(fileName, FileMode.Create, FileAccess.Write))); //FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read); //PdfReader reader=new PdfReader(stream); //PdfADocument pdf = new PdfADocument(reader, new PdfWriter(fileName)); //中文 字體和文字要相適應 //FontProgramFactory.CreateFont(fontFile); PdfFont font = PdfFontFactory.CreateFont(fontFile2, PdfEncodings.IDENTITY_H);//PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED Document document = new Document(pdf, iText.Kernel.Geom.PageSize.A4); document.SetFont(font); //指定位置添加图片 images\luflog20180410145504.png //PdfContentByte pcb1 = writer.DirectContent; //image1.SetAbsolutePosition(12, 18); //底部 PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(Server.MapPath("images/luflog20180410145504.png"))); iText.Layout.Element.Image image1 = new iText.Layout.Element.Image(xObject, 200); // ColumnText ct = new ColumnText(pcb1); image1.ScaleAbsolute(148, 50); //pcb1.AddImage(image1); //Resize image depend upon your need image1.ScaleToFit(140f, 120f); //p = new Paragraph(new Chunk(image1, 10f, 10f)); iText.Layout.Element.Table datatable = new iText.Layout.Element.Table(5, true); //5列 Cell pCTitle = new Cell(1, 5).SetBorder(iText.Layout.Borders.Border.NO_BORDER);//无边 Paragraph p = new Paragraph().SetTextAlignment(TextAlignment.CENTER); p.Add(image1.SetTextAlignment(TextAlignment.CENTER)); pCTitle.Add(p); datatable.AddCell(pCTitle); document.Add(datatable); Paragraph header = new Paragraph("ORDER DETAIL 顧客塗聚文訂單詳情").SetTextAlignment(TextAlignment.CENTER).SetFontSize(20); document.Add(header); Paragraph subheader = new Paragraph("C# WINDOWS FORM CREATE PDF DOCUMENT USING iTEXT7 LIBRARY").SetTextAlignment(TextAlignment.CENTER).SetFontSize(10); document.Add(subheader); LineSeparator ls = new LineSeparator(new SolidLine()); document.Add(ls); Paragraph sellerHeader = new Paragraph("Sold by:").SetBold().SetTextAlignment(TextAlignment.LEFT); Paragraph sellerDetail = new Paragraph("Seller Company").SetTextAlignment(TextAlignment.LEFT); Paragraph sellerAddress = new Paragraph("Mumbai, Maharashtra India").SetTextAlignment(TextAlignment.LEFT); Paragraph sellerContact = new Paragraph("+91 1000000000").SetTextAlignment(TextAlignment.LEFT); document.Add(sellerHeader); document.Add(sellerDetail); document.Add(sellerAddress); document.Add(sellerContact); Paragraph customerHeader = new Paragraph("Customer details:").SetBold().SetTextAlignment(TextAlignment.RIGHT); Paragraph customerDetail = new Paragraph("Customer ABC").SetTextAlignment(TextAlignment.RIGHT); Paragraph customerAddress1 = new Paragraph("R783, Rose Apartments, Santacruz (E)").SetTextAlignment(TextAlignment.RIGHT); Paragraph customerAddress2 = new Paragraph("Mumbai 400054, Maharashtra India").SetTextAlignment(TextAlignment.RIGHT); Paragraph customerContact = new Paragraph("+91 0000000000").SetTextAlignment(TextAlignment.RIGHT); document.Add(customerHeader); document.Add(customerDetail); document.Add(customerAddress1); document.Add(customerAddress2); document.Add(customerContact); Paragraph orderNo = new Paragraph("Order No:15484659").SetBold().SetTextAlignment(TextAlignment.LEFT); Paragraph invoiceNo = new Paragraph("Invoice No:MH-MU-1077").SetTextAlignment(TextAlignment.LEFT); Paragraph invoiceTimestamp = new Paragraph("Date: 30/05/2021 04:25:37 PM").SetTextAlignment(TextAlignment.LEFT); document.Add(orderNo); document.Add(invoiceNo); document.Add(invoiceTimestamp); iText.Layout.Element.Table table = new iText.Layout.Element.Table(5, true); table.SetFontSize(9); Cell headerProductId = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph("ID")); Cell headerProduct = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph("產品名稱")); Cell headerProductPrice = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph("價格")); Cell headerProductQty = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph("數量")); Cell headerTotal = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph("合計")); table.AddCell(headerProductId); table.AddCell(headerProduct); table.AddCell(headerProductPrice); table.AddCell(headerProductQty); table.AddCell(headerTotal); double grandTotalVal = 0; foreach (Orders c in orders) { Cell productid = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph(c.productId.ToString())); Cell product = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph(c.product)); Cell price = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph(c.price.ToString())); Cell qty = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph(c.qty.ToString())); var value = c.price * c.qty; Cell total = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph(value.ToString())); grandTotalVal += value; table.AddCell(productid); table.AddCell(product); table.AddCell(price); table.AddCell(qty); table.AddCell(total); } Cell grandTotalHeader = new Cell(1, 4).SetTextAlignment(TextAlignment.RIGHT).Add(new Paragraph("合計: ")); Cell grandTotal = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph(" " + grandTotalVal.ToString())); table.AddCell(grandTotalHeader); table.AddCell(grandTotal); document.Add(table); //底部 float[] tableWidth = { 445, 50F }; iText.Layout.Element.Table footerTable = new iText.Layout.Element.Table(5).SetFixedPosition(20F, 25F, 780F).SetBorder(iText.Layout.Borders.Border.NO_BORDER); PdfImageXObject xObject2 = new PdfImageXObject(ImageDataFactory.Create(Server.MapPath("images/lufaddress_20180410145531.png"))); iText.Layout.Element.Image image2 = new iText.Layout.Element.Image(xObject2, 500); //image2.SetMarginBottom(18); //底部 //image2.ScaleAbsolute(585, 40); image2.SetTextAlignment(TextAlignment.LEFT); Paragraph p2 = new Paragraph().SetTextAlignment(TextAlignment.LEFT); p2.Add(image2.SetTextAlignment(TextAlignment.LEFT)); Cell cfoot = new Cell(1, 5).SetBorder(iText.Layout.Borders.Border.NO_BORDER); //离得远 cfoot.Add(p2); footerTable.AddCell(cfoot).SetTextAlignment(TextAlignment.LEFT); document.Add(footerTable); // document.Add(image2); table.Flush(); table.Complete(); document.Close(); Response.Charset = "utf-8";//设置输出流的字符集-中文 Response.ContentType = "application/pdf"; // Response.Write(fileName); MemoryStream m = new MemoryStream(); FileStream fs = new FileStream(fileName, FileMode.Open); fs.CopyTo(m); Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length); Response.OutputStream.Flush(); Response.OutputStream.Close(); //自动下载 //Response.Clear(); //Response.ContentType = "application/pdf"; //Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName); //Response.TransmitFile(fileName); //Response.End(); } catch (Exception ex) { ex.Message.ToString(); Response.Write(ex.Message.ToString()); } } } /// <summary> /// 订单实体类 /// </summary> public class Orders { /// <summary> /// ID /// </summary> public int productId { get; set; } /// <summary> /// 产品名称 /// </summary> public string product { get; set; } /// <summary> /// 数量 /// </summary> public int qty { get; set; } /// <summary> /// 价格 /// </summary> public double price { get; set; } } }
标签:SetTextAlignment,8.0,4.8,iText,Add,Paragraph,new,TextAlignment From: https://www.cnblogs.com/geovindu/p/17997684