Converting XFA Form to Static PDF in C# for Browser Compatibility

I have this C# code and I am trying to convert an XFA form into a standard static PDF so that it can be opened in a browser. Unfortunately, the code converts the XFA form in such a way that the expected result is not present in the output file. Could you please advise me on how to modify the code?"

using System;
using System.Collections.Generic;
using System.IO;
using iText.Forms;
using iText.Forms.Fields;
using iText.Kernel.Colors;
using iText.Kernel.Pdf;

namespace iText.Samples.Sandbox.Acroforms
{
    public class RemoveXFA
    {
        public static readonly String DEST = @"C:\temp\xfa-form-filled.pdf";
        public static readonly String SRC = @"C:\temp\xfa-form.pdf";

        public static void Main(String[] args)
        {
            // Ensure the destination directory exists
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            // Create an instance of RemoveXFA and manipulate the PDF
            new RemoveXFA().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            // Open the source PDF and prepare to write to the destination PDF
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            PdfAcroForm form = PdfFormCreator.GetAcroForm(pdfDoc, true);

            // Remove the XFA form from the PDF
            form.RemoveXfaForm();

            // Get all form fields from the PDF
            IDictionary<String, PdfFormField> fields = form.GetAllFormFields();

            // Iterate over the form fields
            foreach (KeyValuePair<String, PdfFormField> name in fields)
            {
                // Check if the field name contains "Total"
                if (name.Key.IndexOf("Total") > 0)
                {
                    // Set the field color to red
                    name.Value.SetColor(ColorConstants.RED);
                }

                // Set the field value to "X"
                name.Value.SetValue("X");
            }

            // Close the PDF document
            pdfDoc.Close();
        }
    }
}




The result is this content instead of an actual standard PDF

Please wait...

If this message is not eventually replaced by the proper contents of the document, your PDF viewer may not be able to display this type of document.

You can upgrade to the latest version of Adobe Reader for Windows®, Mac, or Linux® by visiting http://www.adobe.com/products/acrobat/readstep2.html.

For more assistance with Adobe Reader visit http://www.adobe.com/support/products/ acrreader.html.

Upvotes: 0

Views: 170

Answers (0)

Related Questions