0

I want to create PDF/A from HTML using iTextsharp, but is having a hard time creating something that validateds to PDF/. This is the code that I use to test the functionality. What needs to be changed to get what I want?

    static void Main(string[] args)
    {
        //create stylesheet (used to change font from default Helvetica in order to embed font in PDF/A)
        var styles = new StyleSheet();
        var fontPath = Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\verdana.ttf";
        FontFactory.Register(fontPath);
        styles.LoadTagStyle("body", "face", "Verdana");


        var pdFdocument = new Document(PageSize.A4);
        const string strPdFpath = @"MyPDF.pdf-PDFX1A2001";
        var sr = new StringReader("<html><head></head><body><h1>Dette er en HTML-fil</h1><p>Dette er litt tekst i html-fila</p><br /><p>Dette er enda ett avsnitt. iTextSharp ser ut til å fungere greit...</p></body></html>");

        var htmlParser = new HTMLWorker(pdFdocument, null, styles);
        PdfWriter writer = PdfWriter.GetInstance(pdFdocument, new FileStream(strPdFpath, FileMode.OpenOrCreate));
        writer.PDFXConformance = PdfWriter.PDFX1A2001;
        pdFdocument.Open();
        htmlParser.Parse(sr);
        pdFdocument.Close();
    }
2
  • 1
    Your code of course. You need to explain what is what you want and what you're getting that differs from what you want and what you did to get there. Then we can talk about finer, more detailed changes. Commented Mar 14, 2013 at 12:50
  • In plain text; I want a valid pdf/a file! The PdfWriter provides three possible PDFXConformance options (PDFX1A2001, PDFX32002 and PDFXNONE), and non of the resulting PDF-files validates correctly. Commented Mar 14, 2013 at 14:17

1 Answer 1

3

PDFX1A2001 requires a version 1.3 PDF or less but setting the PDFXConformance property alone doesn't do this, you must manually do it:

writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_3);

Additionally, you also need to call CreateXmpMetadata() to "apply" the metadata.

writer.CreateXmpMetadata();

Most people seem to do it just before closing the document but I don't know if this is a requirement.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.