LooC
LooC

Reputation: 49

Copy into new document and fillout form in iText7 c#

I have been trying to get a grip of iText7 c# with little luck.

My goal is:

  1. Load a pdf (one page) with a form (multiple fields) as a template
  2. Fill out the form, Flatten and copy the filled forms page to a new document
  3. repeat #2 x number of times with different data
  4. Save to Memory Stream

I have the different parts working , but i cant get it to work together

var memoryStream = new MemoryStream();
PdfReader reader = new PdfReader("untitled-1.pdf"); //Iput
PdfWriter writer = new PdfWriter(memoryStream); //output
PdfDocument pdfDoc = new PdfDocument(reader, writer);
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
var fields = form.GetFormFields();
if(fields.ContainsKey("address")) { 
     fields["address"].SetValue("first\nlast");
}
form.FlattenFields();
pdfDoc.Close();
byte[] b = memoryStream.ToArray();
File.WriteAllBytes(@"t.pdf", b);

clone page:

// create clone page x times
PdfDocument pdfDoc = new PdfDocument(new PdfWriter("output.pdf").SetSmartMode(true));
pdfDoc.InitializeOutlines();
PdfDocument srcDoc;
for (int i = 0; i<5; i++) {
    srcDoc = new PdfDocument(new PdfReader("untitled-1.pdf"));
    // copy content to the resulting PDF
    srcDoc.CopyPagesTo(1, srcDoc.GetNumberOfPages(), pdfDoc);
}
pdfDoc.Close();

Upvotes: 3

Views: 2169

Answers (1)

LooC
LooC

Reputation: 49

got an idea just after writing this question. Here is one solution to this problem

Create a pdf-file with a form and a text field named address to use as template, save as untitled1-pdf.

This code will create an empty document and then for each user in users load and fill the field address whit the user.

The filled form will then be flatten and copied into the new document. When all is done, the document will be saved as result.pdf

//b.

static void Main(string[] args)
    {
        List<string> users = new List<string> { "Peter", "john", "Carl" };
        byte[] result = createPdf(users, "untitled-1.pdf");
        File.WriteAllBytes(@"result.pdf", result);
    }

    public static byte[] createPdf(List<string> users,string templateFile)
    {
        // create clone page for each user in users
        using (MemoryStream memoryStream = new MemoryStream())
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(memoryStream).SetSmartMode(true));
            pdfDoc.InitializeOutlines();
            PdfDocument srcDoc;
            foreach (var u in users)
            {
                MemoryStream m = new MemoryStream(fillForm(u,templateFile));
                srcDoc = new PdfDocument(new PdfReader(m));
                // copy content to the resulting PDF
                srcDoc.CopyPagesTo(1, srcDoc.GetNumberOfPages(), pdfDoc);
            }
            pdfDoc.Close();

            return memoryStream.ToArray();
        }
    }

    public static byte[] fillForm(string user,string templateFile)
    {
        using (var memoryStream = new MemoryStream())
        {
            PdfReader reader = new PdfReader(templateFile); //Iput
            PdfWriter writer = new PdfWriter(memoryStream); //output
            PdfDocument pdfDoc = new PdfDocument(reader, writer);
            PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
            var fields = form.GetFormFields();

            if (fields.ContainsKey("address"))
            {
                fields["address"].SetValue(user);
            }

            form.FlattenFields();
            pdfDoc.Close();
            byte[] b = memoryStream.ToArray();
            return b;
        }
    }

Upvotes: 1

Related Questions