iTextSharp套件经常被使用来产出PDF档案,
且新版iTextSharp套件有支援将HTML直接转换成PDF,使用上相当方便直觉,
但若使用4.1.6以上的版本,会有授权相关问题,
如果要应用在商业行为中,就要特别注意这个部分,
为了当一个快乐的免费仔,我们使用4.1.6版手绘产出需要的PDF格式。
在开始之前,请先下载iTextSharp套件,并将其加入参考:
(以下使用visual studio的NuGet套件管理员操作)
//加入参考using iTextSharp.text.pdf;using iTextSharp.text;
範例PDF
这个範例先不考虑如何将动态值塞入PDF中,仅针对文字及表格绘製做处理。
首先先建立一个新的空白文件,并设定需要使用的文字字体:
//初始化Document doc = new Document();PdfWriter pdfWriter = PdfWriter.GetInstance(doc, stream);//字型设定string kaiu = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "KAIU.TTF");string wingDing = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "WINGDNG2.TTF");string sinhei = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "msjh.ttc,0");BaseFont wingDings = BaseFont.CreateFont(wingDing, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);Font wingdins2 = new Font(wingDings, 14); BaseFont bfChinese = BaseFont.CreateFont(kaiu, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);Font ChFont_Title = new Font(bfChinese, 24);Font ChFont_Content = new Font(bfChinese, 12);Font ChFont_Font18 = new Font(bfChinese, 18, 1);Font ChFont_Font16 = new Font(bfChinese, 16);Font ChFont_Font10 = new Font(bfChinese, 10);Font ChFont_Font14_UnderLine = new Font(bfChinese, 14, Font.UNDERLINE);BaseFont sinheiC = BaseFont.CreateFont(sinhei, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);Font sinhei_Title = new Font(sinheiC, 18);Font sinhei_Content = new Font(sinheiC, 12);Font sinhei_Content_red = new Font(sinheiC, 12, Font.NORMAL, Color.RED);
接下来开始在对应位置放上需要的物件:
doc.Open();PdfContentByte cb = pdfWriter.DirectContent; //---画左上角的框框----cb.MoveTo(30, 820);cb.LineTo(210, 820);cb.MoveTo(210, 820);cb.LineTo(210, 780);cb.MoveTo(210, 780);cb.LineTo(30, 780);cb.MoveTo(30, 780);cb.LineTo(30, 820);cb.ClosePathStroke();//---画左上角的框框----//第一个表格(放非表格的内容)PdfPTable table = new PdfPTable(1);table.TotalWidth = 500f;table.LockedWidth = true;Phrase doc_Id = new Phrase("申请单单号:" , ChFont_Content);ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, doc_Id, doc.Right / 2, doc.Top + 40, 0);//文字显示于左上角doc.Add(doc_Id);PdfPCell header = new PdfPCell(new Phrase("测试申请单", ChFont_Title));header.HorizontalAlignment = Element.ALIGN_CENTER;//文字置中header.Border = PdfPCell.NO_BORDER; //不要有框线(因为是非表格的内容)header.MinimumHeight = 20; //设定最小列高table.AddCell(header); //把header的内容加到table中//请办事由PdfPCell subject = new PdfPCell(new Phrase("主旨:测试申请单", ChFont_Font18));subject.Border = PdfPCell.NO_BORDER;subject.SetLeading(1.5f, 1.5f);subject.MinimumHeight = 50;table.AddCell(subject);//回覆截止日Paragraph paragraph1 = new Paragraph();//"方框打勾"这个符号没办法直接输出,所以用WINGDNG2字体的R代码输出 Chunk c1 = new Chunk("R", wingdins2);Chunk c2 = new Chunk("请于 112 年 5 月 15 日下班前回覆", ChFont_Font14_UnderLine);paragraph1.Add(c1);paragraph1.Add(c2);PdfPCell dueDate = new PdfPCell(paragraph1);dueDate.Border = PdfPCell.NO_BORDER;dueDate.MinimumHeight = 50;table.AddCell(dueDate);//申请人PdfPCell applyName = new PdfPCell(new Phrase("申请人:王小明", ChFont_Font16));applyName.Border = PdfPCell.NO_BORDER;dueDate.SetLeading(3f, 1.5f);table.AddCell(applyName);//分机PdfPCell txt = new PdfPCell(new Phrase("分机:111" , ChFont_Font16));txt.Border = PdfPCell.NO_BORDER;txt.MinimumHeight = 80;table.AddCell(txt);doc.Add(table);//第二个表格(真的表格)PdfPTable table2 = new PdfPTable(2);table.TotalWidth = 500f;table.LockedWidth = true; PdfPCell header2 = new PdfPCell(new Phrase("测试表格", sinhei_Title));header2.Colspan = 2; //标头要横跨2栏,所以Colspan设定为2header2.HorizontalAlignment = Element.ALIGN_CENTER;table2.AddCell(header2);PdfPCell test1 = new PdfPCell(new Phrase("111", sinhei_Content));test1.Colspan = 1;table2.AddCell(test1);PdfPCell content = new PdfPCell(new Phrase("内容内容", sinhei_Content));content.Colspan = 1;table2.AddCell(content);PdfPCell test2 = new PdfPCell(new Phrase("2222", sinhei_Content));test2.Colspan = 1;table2.AddCell(test2);PdfPCell content2 = new PdfPCell(new Phrase("内容内容内容", sinhei_Content_red));content2.Colspan = 1;table2.AddCell(content2);doc.Add(table2);doc.Close();
这样就能够绘製出範例内容了!
上方非表格的内容同样放进表格中,是觉得表格比较容易控制内容排版,
若要使用文字段落呈现也没有问题~