0

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()
Vad1mo
  • 5,156
  • 6
  • 36
  • 65
Chris
  • 3
  • 2
  • What happens if you remove `writer.Close()`? There is no reason why that line is necessary. Have you seen any example in the *official* documentation (that is: documentation written by me) where the `writer` is closed? – Bruno Lowagie Sep 25 '14 at 11:54
  • Yeeaar! Thats it! exception has gone! Thank you very much! – Chris Sep 25 '14 at 12:43
  • Do you have any comments to make my code better work? I think i have still a Problem to be ZUGFeRD compliant. I have to set the File-Attachement-Name in the /Names/EmbeddedFiles/Names too but i can not find a way to do this? Could you give me the link to the official documentation which you mean? i only have found very poor informations about creating a ZUGFeRD compliant PDF/A with iTextSharp... Thanks! – Chris Sep 25 '14 at 12:47

1 Answers1

2

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?

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • I'm about to fly to San Francisco for Java One. For further answers, you'll have to contact iText Software directly, hope that somebody else answers, or wait until half October when I'm back. – Bruno Lowagie Sep 25 '14 at 14:45
  • 2
    I see that you edited my answer and added an extra question **removing my initial answer.** I strongly suggest that you read the StackOverflow FAQ as your edit was rejected by different moderators at StackOverflow. – Bruno Lowagie Sep 25 '14 at 14:47
  • @Chris - Do not edit answers to reply to or comment on them. If you have a new question, use the "Ask Question" link at the top of any page to post it. – Andrew Sep 25 '14 at 15:12