how to Add Input Text Field to Pdf (AcroForm) with PDFSharp

Deadly 提交于 2019-12-11 18:35:09

问题


it's should be simple but i couldn't find the answer anywhere in google or the source documentation.

someone have asked the same question 4 years ago, but still no one answered!

if it's not possible. can someone please confirm, so that i can find another pdf generator library that can do that. because it's very important in my case to be able to generate pdf with input capability

thanks!


回答1:


We will go through the step by step

  1. First you need to create object of PdfReader
    • PdfReader pdfReader = new PdfReader(templatePath); here "templatePath" is the template(sample file) where you write your all data
  2. Then create source file where all data is rendered(write)
    • System.IO.Directory.CreateDirectory(Server.MapPath(DocPath)); "DocPath" is the path where output file is save
  3. Then Initialize PdfStamper and AcroFields object :

    PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(Server.MapPath(newFile), FileMode.Create));
    AcroFields pdfFormFields = pdfStamper.AcroFields;
    
  4. Then set Field in PDF :

        pdfFormFields.SetField("this text should field name in PDF(template PDF)", "Actual value you want to write");
    
  5. Dispose all objects

        pdfStamper.FormFlattening = true;
        pdfStamper.Close();
        pdfReader.Close();
    


来源:https://stackoverflow.com/questions/52717558/how-to-add-input-text-field-to-pdf-acroform-with-pdfsharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!