itextSharp 代码备忘

itextSharp下载

以下是C#代码

        /// 
        /// PDF 操作代码
      /// 
          private void PdfCodeDemo()
         {
             float PDFWidth = 100;    //PDF宽度 (72DPI像素值)
             float PDFHeight = 100;    //PDF高度 (72DPI像素值)
 
             Rectangle rect = new Rectangle(PDFWidth, PDFHeight);
             Document doc = new Document(rect);
 
             //将PDF和文件路径关联
             PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("PDF存储路径", FileMode.Create));
             writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_7);
 
             //PDF信息写入
             doc.AddCreationDate();
             doc.AddAuthor("作者");
            doc.AddCreator("创建人");
             doc.AddKeywords("关键字");
             doc.AddSubject("主题");
             doc.AddTitle("标题");
 
             doc.Open();
             PdfContentByte cb = writer.DirectContent;
 
             //绘制图片
             Image imageTemp = Image.GetInstance("图片路径");
             imageTemp.ScaleAbsolute(100, 100);    //设置宽高
             imageTemp.SetAbsolutePosition(0, 0);    //设置位置
             doc.Add(imageTemp);
 
             //绘制文字
             cb.BeginText();
             BaseFont bfBar = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\FZYBKSJW.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
             cb.SetRGBColorFill(255, 255, 255);
             cb.SetTextMatrix(100, 100);    //位置
             //cb.SetTextMatrix(0, 1, -1, 0, textLeft, textTop); 90度
             cb.SetFontAndSize(bfBar, 12);    //字体及大小
             cb.ShowText("文字内容");
             cb.EndText();
 
             //加载绘制现有PDF
             PdfReader reader = new PdfReader("现有PDF路径");
             PdfImportedPage page = writer.GetImportedPage(reader, 1);
             Image imagePdf = Image.GetInstance(page);
             imagePdf.ScaleAbsolute(100, 100);
             imagePdf.SetAbsolutePosition(0, 0);
             doc.Add(imagePdf);
             reader.Close();
 
             //画线
             cb.SetLineWidth(1);
             cb.SetColorStroke(BaseColor.RED);
             cb.MoveTo(0, 0);
             cb.LineTo(100, 100);
             cb.Stroke();
 
             //创建新页
             Rectangle rectNew = new Rectangle(PDFWidth * 2, PDFHeight);
             bool bo = doc.SetPageSize(rectNew);    //改变新页尺寸
             doc.NewPage();
 
             doc.Close();
         }

 


你可能感兴趣的:(itext)