使用C#iText 7展平XFA PDF

使用C#iText 7展平XFA PDF,c#,pdf,itext7,xfa,C#,Pdf,Itext7,Xfa,是否可以使用iText 7展平XFA PDF?我只看到了关于它的Java文档() 不过,您似乎可以使用iTextSharp来执行此操作 我相信它不是AcroForm PDF,因为做类似于此答案的操作只会创建一个无法正常打开的PDF。看起来您必须使用iTextSharp,而不是iText7。看看NuGet版本,看起来iTextSharp本质上是iText5.NET版本,就像上面评论中提到的Bruno一样,XFA的东西根本没有被移植到iText7 for.NET 这种混乱源于在NuGet中同时拥有i

是否可以使用iText 7展平XFA PDF?我只看到了关于它的Java文档()

不过,您似乎可以使用iTextSharp来执行此操作


我相信它不是AcroForm PDF,因为做类似于此答案的操作只会创建一个无法正常打开的PDF。

看起来您必须使用iTextSharp,而不是iText7。看看NuGet版本,看起来iTextSharp本质上是iText5.NET版本,就像上面评论中提到的Bruno一样,XFA的东西根本没有被移植到iText7 for.NET

这种混乱源于在NuGet中同时拥有iText7和iTextSharp版本,而且试用页没有声明XFA worker不能用于.NET版本的iText7(还没有?)

我做了以下几件事来完成我所需要的至少是一次试验:

  • 请在此索取试用版:
  • 将通过电子邮件向您发送xml许可证密钥,您现在可以将其放在桌面上
  • 在Visual Studio中创建新的控制台应用程序
  • 打开Project Manager控制台,键入以下内容并按ENTER键(这也将安装其他依赖项)

  • 使用以下代码:

     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();
            }
        }
    
    static void Main(字符串[]args)
    {
    ValidateLicense();
    var sourcePdfPath=Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
    "");
    var destinationPdfPath=Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
    “output.pdf”);
    扁平化PDF(sourcePdfPath,destinationPdfPath);
    }
    私有静态void ValidateLicense()
    {
    var licenseFileLocation=Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
    “itextkey.xml”);
    iTextSharp.license.LicenseKey.LoadLicenseFile(licenseFileLocation);
    }
    私有静态PDF(字符串sourcePdfPath、字符串destinationPdfPath)
    {
    使用(var sourcePdfStream=File.OpenRead(sourcePdfPath))
    {
    var document=new iTextSharp.text.document();
    var writer=iTextSharp.text.pdf.PdfWriter.GetInstance(
    文件,
    新文件流(destinationPdfPath,FileMode.Create));
    var xfaf=new iTextSharp.tool.xml.xtra.xfa.xfalatener(文档,编写器);
    sourcePdfStream.Position=0;
    flatte(新的iTextSharp.text.pdf.PdfReader(sourcePdfStream));
    document.Close();
    }
    }
    

  • 试用版将在生成的PDF上添加一个巨大的水印,但至少您可以让它正常工作,并查看完整许可证应该如何工作。

    对于IText 7,这可以通过以下方式完成

    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());
    

    如果要展平XFA文件,需要一个名为的封闭源代码加载项。(太多人认为免费/开源意味着软件可以免费使用;但事实并非总是如此)。这就是为什么我们制作了一些封闭源码的战略性附加组件。我目前正在使用免费试用的itext7,但我在这里没有看到.Net版本的pdfxfa内容。我是否忽略了它?我不确定,也许pdfXFA的C版本还没有发布。我不确定。如果对您来说都一样,您可以尝试版本5的XFAWorker。从功能角度看,pdfXFA和XFAWorker是相同的。pdfXFA的C#版本仍在移植过程中。Java版本应该在免费试用版中提供。@BrunoLowagie您似乎是对的。没有包含.NET iText7 XFA展平功能。我不得不使用iTextSharp(我认为它是iText 5的.NET端口)来完成我想要的任务。你是说XFA扁平化器终于被移植到了iText7 C#吗?
    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());