如何使用iTextSharp从HTML创建PDF/A文件 [英] How create PDF/A file from HTML using iTextSharp

查看:147
本文介绍了如何使用iTextSharp从HTML创建PDF/A文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用iTextsharp从HTML创建PDF/A,但是很难创建可验证为PDF/的内容.这是我用来测试功能的代码.需要改变什么才能得到我想要的东西?

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

推荐答案

PDFX1A2001要求版本为1.3或更低版本的PDF,但仅设置PDFXConformance属性不能做到这一点,您必须手动执行此操作:

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

此外,您还需要调用CreateXmpMetadata()来应用"元数据.

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.

这篇关于如何使用iTextSharp从HTML创建PDF/A文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆