3
votes

hoping that someone can see the flaw in my code to merge to PDF-a documents using ITextSharp. Currently it complains about missing metadata which PDF-a requires.

Document document = new Document();
MemoryStream ms = new MemoryStream();
using (PdfACopy pdfaCopy = new PdfACopy(document, ms, PdfAConformanceLevel.PDF_A_1A))
{
    document.Open();
    using (PdfReader reader = new PdfReader("Doc1.pdf"))
    {
        pdfaCopy.AddDocument(reader);
    }
    using (PdfReader reader = new PdfReader("doc2.pdf"))
    {
        pdfaCopy.AddDocument(reader);
    }
}

The exact error received is

Unhandled Exception: iTextSharp.text.pdf.PdfAConformanceException: The document catalog dictionary of a PDF/A conforming file shall contain the Metadata key

I was hoping that the 'document catalog dictionary' would be copied as well, but I guess the 'new Document()' creates an empty non-conforming document or something.

Thanks! Hope you can help

Wouter

1

1 Answers

2
votes

You need to add this line:

copy.CreateXmpMetadata();

This will create some default XMP metadata. Of course: if you want to create your own XMP file containing info about the documents you're about to merge, you can also use:

copy.XmpMetadata = myMetaData;

where myMetaData is a byte array containing a correct XMP stream.

I hope you understand that iText can't automatically create the correct metadata. Providing metadata is something that needs human attention.