我正在测试 iTextSharp 以生成 ZUGFeRD 文件。我的第一步是从现有的 PDF/A-3 文件生成 ZUGFeRD 符合文件。这是通过使用 PDFACopy 并创建必要的 PDFFileSpecification 成功完成的。
下一步是从现有的 PDF 或 PDF/A-1 文件生成 PDF/A-3 文件,这是困难的部分。
首先,当我尝试将 PDFACopy 与常规 PDF(不是 PDF/A)结合使用时,我收到一个错误,即 PDFACopy 只能用于符合 PDF/A 的文件。我的第一个问题是,如何使用 iTextSharp 从 PDF 中获取符合 PDF/A-3 的文件?
为了缩小差距,我决定使用 ghostscript 将 PDF 转换为 PDF/A-1 文件(参见如何使用 ghostscript 将 PDF 转换为 PDF/A 或 PDF/X?)。这很成功,我又试了一次。然后是错误“不同的 PDF/A 版本”。被抛出。看来我无法从现有的 PDF/A-1 复制到新的 PDF/A-3。如何从现有 PDF(/A-1) 创建此 PDF/A-3?这甚至可能吗?
这是我的代码:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(XML);
byte[] xmlBytes = Encoding.Default.GetBytes(xmlDoc.OuterXml);
Document doc = new Document();
PdfReader src_reader = new PdfReader(pdfPath);
FileStream fs = new FileStream(DEST, FileMode.Create, FileAccess.ReadWrite);
PdfACopy aCopy = new PdfACopy(doc, fs, PdfAConformanceLevel.ZUGFeRD);
doc.AddLanguage("de-DE");
doc.AddTitle("title");
doc.SetPageSize(src_reader.GetPageSizeWithRotation(1));
aCopy.SetTagged();
aCopy.UserProperties = true;
aCopy.PdfVersion = PdfCopy.VERSION_1_7;
aCopy.ViewerPreferences = PdfCopy.DisplayDocTitle;
aCopy.CreateXmpMetadata();
aCopy.XmpWriter.SetProperty(PdfAXmpWriter.zugferdSchemaNS, PdfAXmpWriter.zugferdDocumentFileName, "ZUGFeRD-invoice.xml");
//Ab hier können keine Metadaten mehr geschrieben werden
doc.Open();
ICC_Profile icc = ICC_Profile.GetInstance(new FileStream(ICM, FileMode.Open));
aCopy.SetOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
[...add the dictionary to doc..]
aCopy.AddDocument(src_reader);
doc.Close();
还有一个问题: addDocument 有效,但是当我使用 copy.addPage(copy.getImportedPage(src_reader, i)) 时,会抛出错误“文档没有页面”。为什么?