如何使用iTextSharp从HTML创建PDF/A文件 [英] How create PDF/A file from HTML using iTextSharp
问题描述
我想使用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屋!