iTextSharp 生成PDF 实例

时间:2021-05-04 05:22:18
 1   static void Main(string[] args)
 2         {  
 3             //CreatePDF
 4             string name=Guid.NewGuid().ToString("N");
 5             Document document = new Document(PageSize.A5);
 6             PdfWriter writerA = PdfWriter.GetInstance(document, 
 7                       new FileStream(name+".pdf", FileMode.OpenOrCreate));
writerA.InitialLeading = 30;// 行间距设置
8 WriteDocument(document, writerA); 9 //UpdatePDF 10 UpdatePDF(name+".pdf", 1, "Upated: Add Text", 300,10); 11 }
 1  static void WriteDocument(Document document, PdfWriter writer)
 2         {
 3             if (!document.IsOpen())
 4                 document.Open();
 5 
 6             AddListWithLink(document, writer);
 7             FillBlock(document, writer);
 8             AddCustomizeList(document, writer);
 9             AddPTable(document, writer);
10             AddTable(document, writer);
11             AddExtContent(document, writer);
12             document.NewPage();
13             AddExtContent(document, writer);
14 
15             document.Close();
16         }
17         public static string UpdatePDF(string filePath,int pageIndex,string content,float px,float py)
18         {
19             string name=Guid.NewGuid().ToString("N");
20             PdfReader reader=new PdfReader(filePath);
21             PdfStamper stamp=new PdfStamper(reader, new FileStream(name+".pdf", FileMode.Create));
22            
23             PdfContentByte cbc =stamp.GetOverContent(pageIndex);
24             BaseFont bfontc = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, 
25                 BaseFont.NOT_EMBEDDED);//设定字体:黑体 
26             cbc.BeginText();
27                 cbc.SetFontAndSize(bfontc, 9);//设定字号 
28                 cbc.SetCharacterSpacing(1);//设定字间距 
29                 cbc.SetRGBColorFill(66, 00, 00);//设定文本颜色
30                 cbc.ShowTextAligned(PdfContentByte.ALIGN_LEFT,content,px,py, 0);
31             cbc.EndText();
32             stamp.Close();
33             reader.Close();
34             File.Delete(filePath);
35             return name+".pdf";
36         }
iTextSharp 生成PDF 实例iTextSharp 生成PDF 实例AddListWithLink
 1      //添加 链接 、列表    
 2        private static void AddListWithLink(Document document,PdfWriter writer)
 3         {
 4             Phrase ph=new Phrase();
 5             Paragraph p = new Paragraph();
 6             p.Add("This Is A Link Example:");
 7             Anchor anchor = new Anchor(10, "BaiDu", FontFactory.GetFont(
 8                 FontFactory.HELVETICA, 10, Font.UNDERLINE, new Color(0, 0, 255)));
 9             anchor.Reference = "www.baidu.com";
10             anchor.Name = "baidu";
11             p.Add(anchor);
12             ph.Add(p);
13 
14             List listitems = new List(false, 10);
15             listitems.ListSymbol = new Chunk("\u2022", FontFactory.GetFont(FontFactory.HELVETICA, 20));
16             listitems.Add(new ListItem(ph));
17             listitems.Add(new ListItem("The second line"));   
18            document.Add(listitems);
19         }
iTextSharp 生成PDF 实例iTextSharp 生成PDF 实例FillBlock
 1   // 写在矩形区域里面:用于布局
 2        private static void FillBlock(Document document, PdfWriter writer)
 3         {            
 4             PdfContentByte cbb = writer.DirectContent;
 5             ColumnText ct = new ColumnText(cbb);
 6             Phrase fieldtext = new Phrase("Fill Block[10,35]~[250,150]:\n",
 7                     FontFactory.GetFont(FontFactory.HELVETICA, 10, Font.BOLD, new Color(0, 0, 255)));
 8             fieldtext.AddSpecial(new Paragraph("The  line is longer to see what happens,The  line is longer" 
 9                                    +"to see what happens,The  line is longer to see what happens,The  line "
10                                    +"is longer to see what happens,The  line is longer to see what happens,"
11                                    +"The  line is longer to see what happens."));
12             ct.SetSimpleColumn(fieldtext, 10, 35, 250, 150, 15, Element.ALIGN_LEFT);//[10,35]~[250,150]
13             //区域:左下角为[10,35],右上角为[250,150]
14             ct.Go();
15             
16         }
iTextSharp 生成PDF 实例iTextSharp 生成PDF 实例AddCustomizeList
 1   //定位输出 定制的列表文本
 2        private static void AddCustomizeList(Document document,PdfWriter writer)
 3        {
 4           
 5            PdfContentByte cb = writer.DirectContent;
 6            cb.BeginText();
 7            BaseFont bfont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, 
 8                   BaseFont.NOT_EMBEDDED);//设定字体:黑体 
 9 
10            cb.SetFontAndSize(bfont, 9);//设定字号 
11 
12            cb.SetCharacterSpacing(1);//设定字间距 
13            cb.SetRGBColorFill(66, 00, 00);//设定文本颜色 
14            cb.MoveTo(100, 70);
15            string[] lines=new string[] { 
16                 "A Customize List is longer to \n see what happens" 
17                   +"once the end of the\nline is reached. Will it start on a new line?",
18                 "B Customize List is longer to \n see what happens"
19                   +" once the end of the\n line is reached. Will it start on a new line?", 
20                 "C Customize List is longer to \n see what happens"
21                    +"once the end of the\n line is reached. Will it start on a new line?" };
22            int pointY=300;
23            foreach (var line in lines)
24            {
25                var items=line.Split('\n');
26                bool IsTop=true;
27                pointY-=6;
28 
29                foreach (var item in items)
30                {
31                    pointY-=9;
32                    if (IsTop)
33                    {
34                        cb.SetFontAndSize(bfont, 18);
35                        cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "\u2022", 100, pointY-3, 0);
36                        cb.SetFontAndSize(bfont, 9);
37                        cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, item.Trim(), 110, pointY, 0);
38                        IsTop=false;
39                    }
40                    else
41                    {
42                        cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, item.Trim(), 110, pointY, 0);
43                    }
44 
45                }
46            }
47 
48            cb.EndText();
49           // writer.Add(p);
50            writer.Flush();
51 
52          
53        }
iTextSharp 生成PDF 实例iTextSharp 生成PDF 实例Pdf Table
 1    private static void AddPTable(Document document, PdfWriter writer)
 2        {
 3           
 4            PdfPTable pdftable = new PdfPTable(3);
 5 
 6            Phrase text1 = new Phrase("Add PTable", FontFactory.GetFont(FontFactory.HELVETICA, 10, 
 7                              Font.BOLD, new Color(0, 0, 255)));
 8            PdfPCell cell = new PdfPCell(text1);
 9            cell.VerticalAlignment =Element.ALIGN_CENTER;
10            cell.HorizontalAlignment =Element.ALIGN_MIDDLE;
11            cell.Padding = 0.5F;
12 
13            cell.BorderWidthTop=2;
14            cell.BorderColorLeft=Color.RED;
15            //cell.FixedHeight = height;
16            cell.MinimumHeight =50;
17 
18            pdftable.AddCell(cell);
19 
20            pdftable.AddCell("Content");
21 
22            //GetInstance 的 img 参数是 System.Drawing 中的 Image。
23            //如果你不希望单元格被撑大,就要设置其 FixedHeigh。
24            //如果还想看整个图片,就需要调用 ScaleToFit 方法。
25            PdfPCell cell3=new PdfPCell();
26            iTextSharp.text.Image img1 = iTextSharp.text.Image.GetInstance("flower.jpg");
27 
28            img1.ScaleToFit(cell.Width, cell.Height);
29 
30            cell3.FixedHeight = cell.MinimumHeight;
31            cell3.Image = img1;
32            pdftable.AddCell(cell3);
33            document.Add(pdftable);
34               
35        }
36        private static void AddTable(Document document, PdfWriter writer)
37        {
38           
39            Table table = new Table(3);
40            var cell4 = new Cell("Add Table:");
41            cell4.Rowspan = 2;
42            cell4.Colspan = 2;
43            cell4.BackgroundColor = new Color(0xC0, 0xC0, 0xC0);
44            cell4.BorderColor = new Color(255, 0, 0);
45 
46            table.AddCell(cell4);
47 
48            table.AddCell("1.3");
49 
50            table.AddCell("2.3");
51 
52            table.AddCell("3.1");
53 
54            table.AddCell("3.2");
55            table.AddCell("3.3");
56            document.Add(table);
57        
58        }
iTextSharp 生成PDF 实例iTextSharp 生成PDF 实例AddExtContent
 1  private static void AddExtContent(Document document,PdfWriter writer)
 2        {
 3            PdfContentByte cbc = writer.DirectContent;
 4            BaseFont bfontc = BaseFont.CreateFont(BaseFont.HELVETICA, 
 5                       BaseFont.CP1252, BaseFont.NOT_EMBEDDED);//设定字体:黑体 
 6            cbc.BeginText();
 7            cbc.SetFontAndSize(bfontc, 9);//设定字号 
 8            cbc.SetCharacterSpacing(1);//设定字间距 
 9            cbc.SetRGBColorFill(66, 00, 00);//设定文本颜色 
10            
11            cbc.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Position[10,10]: Pdf123", 10,10, 0);
12            cbc.EndText();
13        }

iTextSharp 生成PDF 实例

iTextSharp 生成PDF 实例

行间距:

writer.InitialLeading = 30;

phrase.Leading = 50;

段落缩进:

paragraph.IndentationLeft = 20;
paragraph.IndentationRight = 20;
paragraph.FirstLineIndent = 20;
 
段间距:
paragraph.SpacingBefore=10;
paragraph.SpacingAfter=20;
 
嵌入字体:
BaseFont simheiBase = BaseFont.CreateFont(@"C:\Windows\Fonts\Din-balck.otf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);