1. 程式人生 > >c#生成pdf文件 iTextSharp方式

c#生成pdf文件 iTextSharp方式

using iTextSharp.text; using iTextSharp.text.pdf;

public void CreatePDF(string msg)         {             Document document = new Document(page, 56, 56, 50, 50);//控制距離左右上下的距離             string path = "D:\\Queue.pdf";//要生成的pdf             PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));             document.Open();

           iTextSharp.text.Font normalfont = new iTextSharp.text.Font(bfHei, 10.6f, Element.TITLE);    BaseFont bfHei = BaseFont.CreateFont(@"C:\Windows\Fonts\STSONG.TTF.TTF", BaseFont.IDENTITY_H,      BaseFont.NOT_EMBEDDED);//需要設定為中文字型,不然顯示不出來中文  iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\111.jpg"), ImageFormat.Png);                 img.ScaleAbsolute(54, 54);//設定圖片的大小,也可以用ScalePercent屬性設定百分比大小                 img.Alignment = Element.ALIGN_CENTER;//把圖片設定為居中                 document.Add(img);//插入圖片

content = new Paragraph(new Chunk(“測試段落”, normalfont ));//具體的內容,及字型樣式                         content.FirstLineIndent = -12f;//這個是設定這一個段落第一行的縮排                         content.IndentationLeft = 20f;//這個是設定這一個段落左邊的縮排                         content.IndentationRight = 0;//這個是設定這一個段落右邊的縮排                         content.Leading = 22;//設定行間距                         document.Add(content);             document.Close();         }

以上內容都是自己測試過的,先總結一下,以後可能會用到