问题
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