iText -PDF reading issue on heading levels ( h1 -

2019-09-04 02:01发布

Generated PDF by iText-XMLWorker 5.5.4. Everything is reading perfectly except heading levels (h1-h6) in screen reader.
Below code works fine on browsers but not in PDF.

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

3条回答
Evening l夕情丶
2楼-- · 2019-09-04 02:01

Please take a look at the ParseHeaders example. It takes the headers.html page with headers from <h1> to <h2> and converts it to headers.pdf:

enter image description here

In your question, you claim that everything is working perfectly except heading levels (h1-h6), but you don't explain what isn't working. Please elaborate. As shown in the screen shot, the PDF looks OK, doesn't it? Can you explain what is wrong with the PDF? Can you show us your code?

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-09-04 02:17

May this code work for html Tag Rendering :

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;
    }
查看更多
对你真心纯属浪费
4楼-- · 2019-09-04 02:22

use CSS style in your code use this code may be for you it will work

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;
}
查看更多
登录 后发表回答