0

由 iText-XMLWorker 5.5.4 生成的 PDF。除了屏幕阅读器中的标题级别(h1-h6)之外,一切都在完美阅读。
下面的代码在浏览器上运行良好,但在 PDF 中不行。

<section>
 <h1>heading 1</h1>
 <h2>heading 2 </h2>
 <h3>heading 3 </h3>
 <h4>heading 4 </h4>
 </section>
4

3 回答 3

1

请看一下这个ParseHeaders例子。它将headers.html页面与 headers from<h1>转换<h2>headers.pdf

在此处输入图像描述

在您的问题中,您声称除了标题级别(h1-h6)之外一切都运行良好,但您没有解释什么不起作用。请详细说明。如屏幕截图所示,PDF 看起来不错,不是吗?你能解释一下PDF有什么问题吗?你能告诉我们你的代码吗?

于 2015-05-03T12:06:19.177 回答
1

在您的代码中使用 CSS 样式使用此代码可能对您有用

enter code here

public PdfPTable renderingAdditionalInformation(PdfPTable pdfPTableAdditionInformationTable,String HTML) throws DocumentException, IOException {

    final String CSS="h1 {display: block;font-size: 2em;-webkit-margin-before: 0.67em;-webkit-margin-after: 0.67em;-webkit-margin-start: 0px;-webkit-margin-end: 0px;font-weight: bold;},"
            + "h2 {    display: block;font-size: 1.5em;-webkit-margin-before: 0.83em;-webkit-margin-after: 0.83em;-webkit-margin-start: 0px;-webkit-margin-end: 0px;font-weight: bold;},"
            + "h3 {    display: block;font-size: 1.17em;-webkit-margin-before: 1em;-webkit-margin-after: 1em;-webkit-margin-start: 0px;-webkit-margin-end: 0px;font-weight: bold;},"
            + "h4 {    display: block;-webkit-margin-before: 1.33em;-webkit-margin-after: 1.33em;-webkit-margin-start: 0px;-webkit-margin-end: 0px;font-weight: bold;},"
            + "h5 {    display: block;font-size: 0.83em;-webkit-margin-before: 1.67em;-webkit-margin-after: 1.67em;-webkit-margin-start: 0px;-webkit-margin-end: 0px;font-weight: bold;},"
            + "h6 {    display: block;font-size: 0.67em;-webkit-margin-before: 2.33em;-webkit-margin-after: 2.33em;-webkit-margin-start: 0px;-webkit-margin-end: 0px;font-weight: bold;}";
    PdfPCell cell = new PdfPCell();

    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

    CSSResolver cssResolver = new StyleAttrCSSResolver();
    CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes()));
    cssResolver.addCss(cssFile);

    ElementList elements=new ElementList();
    ElementHandlerPipeline pdf = new ElementHandlerPipeline(elements, null);
    HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

    XMLWorker worker = new XMLWorker(css, false);
    XMLParser p = new XMLParser(worker);
    p.parse(new ByteArrayInputStream(HTML.getBytes()));
    for (Element element : elements) {
        cell.addElement(element);
    }
    pdfPTableAdditionInformationTable.addCell(cell);
    return pdfPTableAdditionInformationTable;
}
于 2018-04-19T05:15:51.633 回答
1

愿此代码适用于 html 标记渲染:

public PdfPCell richTextRendering(PdfPCell pdfpCell, String HTML) throws DocumentException, IOException {

        final String CSS = "h1 {display: block;font-size: 2em;-webkit-margin-before: 0.67em;-webkit-margin-after: 0.67em;-webkit-margin-start: 0px;-webkit-margin-end: 0px;font-weight: bold;},"
                + "h2 {    display: block;font-size: 1.5em;-webkit-margin-before: 0.83em;-webkit-margin-after: 0.83em;-webkit-margin-start: 0px;-webkit-margin-end: 0px;font-weight: bold;},"
                + "h3 {    display: block;font-size: 1.17em;-webkit-margin-before: 1em;-webkit-margin-after: 1em;-webkit-margin-start: 0px;-webkit-margin-end: 0px;font-weight: bold;},"
                + "h4 {    display: block;-webkit-margin-before: 1.33em;-webkit-margin-after: 1.33em;-webkit-margin-start: 0px;-webkit-margin-end: 0px;font-weight: bold;},"
                + "h5 {    display: block;font-size: 0.83em;-webkit-margin-before: 1.67em;-webkit-margin-after: 1.67em;-webkit-margin-start: 0px;-webkit-margin-end: 0px;font-weight: bold;},"
                + "h6 {    display: block;font-size: 0.67em;-webkit-margin-before: 2.33em;-webkit-margin-after: 2.33em;-webkit-margin-start: 0px;-webkit-margin-end: 0px;font-weight: bold;}";
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

        CSSResolver cssResolver = new StyleAttrCSSResolver();
        CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes()));
        cssResolver.addCss(cssFile);

        ElementList elements=new ElementList();
        ElementHandlerPipeline pdf = new ElementHandlerPipeline(elements, null);
        HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
        CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

        XMLWorker worker = new XMLWorker(css, false);
        XMLParser p = new XMLParser(worker);
        p.parse(new ByteArrayInputStream(HTML.getBytes()));
        for (Element element : elements) {
            pdfpCell.addElement(element);
        }
        return pdfpCell;
    }
于 2018-04-19T05:20:52.020 回答