簡體   English   中英

使用C#iText 7壓平XFA PDF

[英]Using C# iText 7 to flatten an XFA PDF

是否可以使用iText 7壓平XFA PDF? 我只看到有關它的Java文檔( http://developers.itextpdf.com/content/itext-7-examples/itext-7-form-examples/flatten-xfa-using-pdfxfa )。

看來你可以使用iTextSharp來做到這一點。

我相信它不是AcroForm PDF,因為做類似於這個問題的答案如何在c#中用Itext展平pdf? 只是創建了一個無法正常打開的PDF。

看起來你必須使用iTextSharp而不是iText7。 看看NuGet版本,它看起來像iTextSharp本質上是iText5 .NET版本,就像上面評論中提到的Bruno一樣,XFA的東西根本沒有被移植到iText7 for .NET。

混淆源於在NuGet中同時具有iText7和iTextSharp版本,並且試用頁面也沒有說明XFA工作者不能用於iText7的.NET版本(但是?)

我做了以下工作來完成我至少需要試用的東西:

  1. 請在此處申請試用版: http//demo.itextsupport.com/newslicense/
  2. 您將通過電子郵件發送xml許可證密鑰,您現在可以將其放在桌面上。
  3. 在Visual Studio中創建一個新的控制台應用程序
  4. 打開項目管理器控制台並輸入以下內容並按ENTER(這將安裝其他依賴項)

     Install-Package itextsharp.xfaworker 
  5. 使用以下代碼:

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

該試驗將為生成的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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM