vb.netitextxmppdfazugferd

iTextSharp XML ZUGFeRD-invoice.xml attachment


I use iTextSharp 5.5.3 and Ii need to generate a PDF/A with ConformanceLevel = ZUGFeRD. I have trouble in setting the correct XMP schema flags.

The code is working but I always get the exception

ZUGFeRD XMP schema shall contain attachment name

when I close the writer. The PDF was generated before but does not seem to be compliant with ZUGFeRD.

I dont know how to fix this warning. I really hope someone could help me. I've been working on this problem for 2 days and can't find a way.

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

Solution

  • You can solve your problem by removing the following line:

    writer.Close()
    

    The writer is automatically closed when you close the Document. The problem you are facing is that the XMP is written to the document when the writer is closed the first time. The data in the XmpWriter is checked, approved and consumed.

    When you close the writer a second time, the XMP data you added is gone. Hence the exception: some ZUGFeRD related information is missing.

    Our problem with ZUGFeRD is that we didn't find a finalized version of the standard in German yet. I don't understand what you mean with the extra question in the comments.

    I've made a screen shot of the internal structure of a ZUGFeRD PDF:

    enter image description here

    As far as I can see, the name of the file is stored in the Name tree of the EmbeddedFiles entry. Are you saying this isn't the case for you?