0

I want to save a PDF file in a flatted mode. I have tried all the online solution but never works in my code.

I have a file generated with ITextSharp and Acrofields. When i try to set the option pdf.FormFlattening = true; it doesn't work for my solution.

The file is generated from an "Adobe Static PDF form" created with Adobe Livecycle Designer

This is my function:

    using (var streamPdf = new MemoryStream())
    {
       PdfStamper pdf;
       using (var pdfReader = new PdfReader(Application.StartupPath + PathToFile + Template))
       using (pdf = new PdfStamper(pdfReader, streamPdf))
       {
          AcroFields pdfFormFields = pdf.AcroFields;
          foreach (var field in fields)
          {
             pdfFormFields.SetField(field.FieldName, field.FieldValue.Trim());
          }
       }
       foreach (var de in pdfReader.AcroFields.Fields)
       {
          pdfFormFields.SetFieldProperty(de.Key.ToString(),"setfflags",PdfFormField.FF_READ_ONLY,null);
       }
       pdf.FormFlattening = true;
       pdf.AcroFields.GenerateAppearances = true;
       pdf.Close();
       _pdf = streamPdf.ToArray();
   }
   string filename = "name.pdf";
   using (var fs = new FileStream(filename, FileMode.Create))
   {
      fs.Write(_pdf, 0, _pdf.Count());
      fs.Close();
   }

The file generated is a static PDF and i need to have a PDF without Fields.

Thanks to all

  • what error are you getting? – derloopkat Jan 26 '18 at 20:10
  • `FormFlattening` works for AcroForm forms. You say, though, that you have a "dynamic PDF" which most likely means a pdf wrapping a XFA form. With iText you'll need the pdfXFA module for flattening. https://developers.itextpdf.com/content/itext-7-examples/itext-7-form-examples/flatten-xfa-using-pdfxfa – mkl Jan 26 '18 at 20:53
  • I have a static PDF filled from a form. I need to reduce because is to large. I have fixed all form field to readonly but the pdf file is not reduced – Roberto Mindoli Jan 27 '18 at 15:54
  • Ok, so it is static now. Which kind of static? Pure AcroForm? Pure XFA? Hybrid? – mkl Jan 27 '18 at 16:10
  • I think is a Pure AcroForm. I have created a new form in AdobeLiveCycleDesigner and after i have saved it as an "Adobe Static PDF Form (*.pdf)". The file is saved in the path Template of the code and i initialize it with all my value. I am creating some labels that i need to print. The final PDF is well formed but is very large and if i click to the text all form are editable. So i want to convert it in a smaller PDF. Thank you for your help – Roberto Mindoli Jan 27 '18 at 16:19

0 Answers0