代码之家  ›  专栏  ›  技术社区  ›  F. Baydemir

使用iTextSharp在PDF草稿表单上书写

  •  0
  • F. Baydemir  · 技术社区  · 9 年前

    我想用itexsharp在PDF上填写表单草稿。但当我运行代码时,它不起作用。数据隐藏在PDF格式下。您可以在下面看到详细代码。我该如何修复它?

    enter image description here

    我认为图片中的方形可能是图像格式。

            string oldFile ="~\Document\oldFile.pdf";
            string newFile ="~\Document\newFile.pdf";
    
            PdfReader reader = new PdfReader(oldFile);
            Rectangle size = reader.GetPageSizeWithRotation(1);
            Document document = new Document(size);
    
            FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
            PdfWriter writer = PdfWriter.GetInstance(document, fs);
            document.Open();
    
            PdfContentByte cb = writer.DirectContent;
    
    
            BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            cb.SetColorFill(BaseColor.DARK_GRAY); cb.SetFontAndSize(bf, 8);
    
            cb.BeginText();
            string text = TextBox1.Text;
            cb.ShowTextAligned(1, text, 350, 710, 0); // or cb.SetTextMatrix(1, text, 350, 710, 0);
            cb.EndText();
    
            cb.BeginText();
            text = TextBox1.Text;
            cb.ShowTextAligned(2, text, 520, 640, 0);
            cb.EndText();           
    
            cb.BeginText();
            text = "Signature";
            cb.ShowTextAligned(3, text, 100, 200, 0);
            cb.EndText();
    
            PdfImportedPage page = writer.GetImportedPage(reader, 1);
            cb.AddTemplate(page, 0, 0);
    
            document.Close();
            fs.Close();
            writer.Close();
            reader.Close();
    
    3 回复  |  直到 9 年前
        1
  •  3
  •   serhiyb    9 年前

    如果这是可编辑的表单(可以使用Adobe Reader填写的表单),那么您应该查看 Pdf冲压机 AcroFields公司

    这里有一篇关于它的好文章 http://www.codeproject.com/Articles/23112/Fill-in-PDF-Form-Fields-using-the-Open-Source-iTex

        2
  •  1
  •   Razvan    9 年前

    试着把

    PdfImportedPage page = writer.GetImportedPage(reader, 1);
    cb.AddTemplate(page, 0, 0);
    

    在添加自己的文本之前。 你把文本写在页面上,然后重叠原始pdf。

        3
  •  0
  •   Community CDub    7 年前

    本文 How to bring added images using iTextSharp in C# to the forefront 建议PDF中的z顺序由添加到内容中的对象的顺序决定。因此,首先导入源页面,然后绘制文本,这应该可以解决问题。

    正方形也可以是实际的表单字段。如果是这样的话,你可以用Acrobat打开PDF,找出它们的名字(如果你没有Acrobat,也可以通过iTextSharp编码枚举它们),并使用PdfStamper类来实际“填充它们”。