我使用iTextSharp 5.5.3和Ii需要用ConformanceLevel = ZUGFeRD生成PDF/A。我在設置正確的XMP架構標誌時遇到了麻煩。iTextSharp XML ZUGFeRD-invoice.xml附件
的代碼工作,但我總是異常
ZUGFeRD XMP模式應包含附件名稱
當我關閉作家。 PDF是之前生成的,但似乎不符合ZUGFeRD。
我不知道如何解決此警告。我真的希望有人能幫助我。我一直在處理這個問題2天,無法找到方法。
Dim document As New Document(PageSize.A4, 0, 0, 0, 0)
Dim writer As PdfAWriter = PdfAWriter.GetInstance(document, New FileStream(tmpPDFDatei, FileMode.Create), PdfAConformanceLevel.ZUGFeRD)
writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_7)
writer.CreateXmpMetadata()
Dim PDFbaseFont As BaseFont = BaseFont.CreateFont(Application.StartupPath & "\Courier Prime.ttf", BaseFont.CP1252, BaseFont.EMBEDDED)
document.Open()
Dim icc As ICC_Profile = ICC_Profile.GetInstance(Application.StartupPath & "\sRGB_IEC61966-2-1_black_scaled.icc")
writer.SetOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc)
Dim cb As PdfContentByte = writer.DirectContent
cb.BeginText()
cb.SetFontAndSize(PDFbaseFont, 10)
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "TEST TEXT", 0, 0, 0)
cb.SetHorizontalScaling(100)
cb.EndText()
Dim Params As PdfDictionary = New PdfDictionary
Params.Put(PdfName.MODDATE, New PdfDate)
Dim fileSpec As PdfFileSpecification = PdfFileSpecification.FileEmbedded(writer, tmpXMLDatei, "ZUGFeRD-invoice.xml", Nothing, False, "text/xml", Params)
fileSpec.Put(New PdfName("AFRelationship"), New PdfName("Alternative"))
writer.AddFileAttachment("ZUGFeRD Invoice", fileSpec)
Dim aRR As PdfArray = New PdfArray
aRR.Add(fileSpec.Reference)
writer.ExtraCatalog.Put(New PdfName("AF"), aRR)
writer.XmpWriter.SetProperty(PdfAXmpWriter.zugferdSchemaNS, PdfAXmpWriter.zugferdDocumentFileName, "ZUGFeRD-invoice.xml")
writer.XmpWriter.SetProperty(PdfAXmpWriter.zugferdSchemaNS, PdfAXmpWriter.zugferdDocumentType, "INVOICE")
document.Close()
writer.Close()
如果刪除'writer.Close()',會發生什麼?沒有理由爲什麼這條線是必要的。你有沒有在* writer官方文檔中看到任何例子(即:我寫的文檔),'writer'是關閉的? – 2014-09-25 11:54:14
Yeeaar!而已!異常消失了!非常感謝你! – Chris 2014-09-25 12:43:27
你有任何意見使我的代碼更好的工作?我認爲我仍然有一個問題是ZUGFeRD兼容。我必須在/ Names/EmbeddedFiles/Names中設置File-Attachement-Name,但是我找不到這樣做的方法?你能給我鏈接到你的官方文件嗎?我只發現有關使用iTextSharp創建符合ZUGFeRD的PDF/A的非常差的信息... 謝謝! – Chris 2014-09-25 12:47:07