我正在测试iTextSharp生成ZUGFeRD文件。我的第一步是从现有的PDF / A-3文件生成符合ZUGFeRD标准的文件。通过使用PDFACopy并创建必要的PDFFileSpecification,这一步已经成功。
下一步将是从现有的PDF或PDF / A-1文件生成PDF / A-3文件,这是困难的部分。
首先,当我尝试在常规PDF(而不是PDF / A)中使用PDFACopy时,会出现错误,指出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))时,会抛出“文档没有页面”的错误。 为什么?