How create PDF/A file from HTML using iTextSharp

流过昼夜 提交于 2019-12-24 19:29:12

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/15409617/how-create-pdf-a-file-from-html-using-itextsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!