在iTextSharp中没有实现HR标签
本文关键字:实现 HR 标签 iTextSharp | 更新日期: 2023-09-27 18:17:51
当我使用HTMLWorker时,我有一个标签HR问题,我的代码是:
var document = new Document(PageSize.A4, 50, 50, 25, 25);
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
document.Open();
String contents = File.ReadAllText("C://TemplateCotizaciones//Cotizacion.html");
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader (contents), null);
但是,当html中包含一个标签HR时,HTMLWorker的ParseToList方法会抛出一个异常"nullReferenceException"
例如:
输入错误hello<br/>
<hr>
world
且无错误
hello<br/>
world
你知道为什么吗?我认为HR没有在ParseToList的代码中实现。你知道如何在html中写一行没有HR的html(由htmlworker . parsetolist)吗?
感谢您的建议和帮助
HTMLWorker已弃用。仍然支持XMLWorker,但不是实现"html到pdf"功能的最新方式。pdfHTML是最新的iText7插件,用于将HTML转换为pdf。支持HTML5和CSS3。
一个小代码示例:
public void createPdf(String src, String dest, String resources) throws IOException {
try {
FileOutputStream outputStream = new FileOutputStream(dest);
WriterProperties writerProperties = new WriterProperties();
//Add metadata
writerProperties.addXmpMetadata();
PdfWriter pdfWriter = new PdfWriter(outputStream, writerProperties);
PdfDocument pdfDoc = new PdfDocument(pdfWriter);
pdfDoc.getCatalog().setLang(new PdfString("en-US"));
//Set the document to be tagged
pdfDoc.setTagged();
pdfDoc.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
//Set meta tags
PdfDocumentInfo pdfMetaData = pdfDoc.getDocumentInfo();
pdfMetaData.setAuthor("Samuel Huylebroeck");
pdfMetaData.addCreationDate();
pdfMetaData.getProducer();
pdfMetaData.setCreator("iText Software");
pdfMetaData.setKeywords("example, accessibility");
pdfMetaData.setSubject("PDF accessibility");
//Title is derived from html
// pdf conversion
ConverterProperties props = new ConverterProperties();
FontProvider fp = new FontProvider();
fp.addStandardPdfFonts();
fp.addDirectory(resources);//The noto-nashk font file (.ttf extension) is placed in the resources
props.setFontProvider(fp);
props.setBaseUri(resources);
//Setup custom tagworker factory for better tagging of headers
DefaultTagWorkerFactory tagWorkerFactory = new AccessibilityTagWorkerFactory();
props.setTagWorkerFactory(tagWorkerFactory);
HtmlConverter.convertToPdf(new FileInputStream(src), pdfDoc, props);
pdfDoc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
浏览http://itextpdf.com/itext7/pdfHTML