1

Is it possible to use iText 7 to flatten an XFA PDF? I'm only seeing Java documentation about it (http://developers.itextpdf.com/content/itext-7-examples/itext-7-form-examples/flatten-xfa-using-pdfxfa).

It seems like you can use iTextSharp, however to do this.

I believe it's not an AcroForm PDF because doing something similar to this answer How to flatten pdf with Itext in c#? simply created a PDF that wouldn't open properly.

Community
  • 1
  • 1
KSib
  • 893
  • 1
  • 7
  • 24
  • You need a closed source add-on called [pdfXFA](http://itextpdf.com/itext7/pdfXFA) if you want to flatten an XFA file. (Too many people assumed that free/open source meant that the software could be used *for free*; which isn't always the case). That's why we made some strategic add-ons closed source. – Bruno Lowagie Apr 12 '17 at 06:29
  • I'm currently using the free trial of itext7 but I don't see The pdfxfa stuff in here for the .Net version. Am I overlooking it? – KSib Apr 12 '17 at 13:21
  • 1
    I'm not sure, maybe the C# version of pdfXFA hasn't been released yet. I'm not sure. If it's all the same to you, you might try version 5's XFAWorker. From a functional point of view pdfXFA and XFAWorker are identical. – Bruno Lowagie Apr 12 '17 at 13:23
  • 1
    C# version of pdfXFA is still in the process of being ported. Java version should be available in the free trial. – Samuel Huylebroeck Apr 12 '17 at 13:33
  • @BrunoLowagie It seems you're correct. There is no .NET iText7 XFA flattening functionality included. I had to use iTextSharp (which I think is the .NET port of iText 5) to do what I wanted. – KSib Apr 13 '17 at 19:02
  • OK, thank you for adding an answer to your question. That is very helpful for further reference (in case other people encounter the same problem). – Bruno Lowagie Apr 14 '17 at 05:46

2 Answers2

2

It looks like you have to use iTextSharp and not iText7. Looking at the NuGet version it looks like iTextSharp is essentially the iText5 .NET version and like Bruno mentioned in the comments above, the XFA stuff simply hasn't been ported to iText7 for .NET.

The confusion stemmed from having both iText7 and iTextSharp versions in NuGet and also the trial page didn't state that the XFA worker wasn't available for the .NET version of iText7 (yet?)

I did the following to accomplish what I needed at least for a trial:

  1. Request trial copy here: http://demo.itextsupport.com/newslicense/

  2. You'll be emailed an xml license key, you can just place it on your desktop for now.

  3. Create a new console application in Visual Studio

  4. Open the Project Manager Console and type in the following and press ENTER (this will install other dependencies as well)

    Install-Package itextsharp.xfaworker
    
  5. Use the following code:

static void Main(string[] args)
{
    ValidateLicense();
    var sourcePdfPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "<your_xfa_pdf_file>");
    var destinationPdfPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "output.pdf");
    FlattenPDF(sourcePdfPath, destinationPdfPath);
}

private static void ValidateLicense()
{
    var licenseFileLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "itextkey.xml");
    iTextSharp.license.LicenseKey.LoadLicenseFile(licenseFileLocation);
}

private static void FlattenPDF(string sourcePdfPath, string destinationPdfPath)
{
    using (var sourcePdfStream = File.OpenRead(sourcePdfPath))
    {
        var document = new iTextSharp.text.Document();
        var writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(destinationPdfPath, FileMode.Create));
        var xfaf = new iTextSharp.tool.xml.xtra.xfa.XFAFlattener(document, writer);
        sourcePdfStream.Position = 0;
        xfaf.Flatten(new iTextSharp.text.pdf.PdfReader(sourcePdfStream));
        document.Close();
    }
}

The trial will put a huge watermark on the resulting PDF, but at least you can get it working and see how the full license should work.

ˈvɔlə
  • 9,204
  • 10
  • 63
  • 89
KSib
  • 893
  • 1
  • 7
  • 24
1

For IText 7 this could be done in the following way

LicenseKey.LoadLicenseFile(@"Path of the license file");
MemoryStream dest_File = new MemoryStream();
XFAFlattener xfaFlattener = new XFAFlattener();
xfaFlattener.Flatten(new MemoryStream( File.ReadAllBytes(@"C:\\Unflattened file")), dest_File);
File.WriteAllBytes("flatten.pdf", dest_File.ToArray());
heyyan khan
  • 173
  • 1
  • 1
  • 12