0

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>
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109

3 Answers3

1

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?

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Hi Bruno, Thanks for the quick response. I have listened both headers.html and headers.pdf files through NVDA screen reader. In the browser html file is clearly reading heading levels 1, 2, 3 etc., when I listen PDF it is not reading heading level 1,2,3 etc... Visually impaired users do not understand which type of heading it is... – user2548712 May 03 '15 at 18:03
  • Aha, this is a PDF/UA question. I've submitted a support ticket to ask our team how to make H-tags tagged as H-tags. However: this feature was developed for and thanks to our paying customers. It may very well be that it is available only to paying customers. – Bruno Lowagie May 04 '15 at 05:49
  • If you're familiar with Agile development, you'll be happy to know that [DEV-1393](https://jira.itextsupport.com/browse/DEV-1393) was added to the next sprint (with 2 story points). This means that the fix should be done in two weeks. It will be in iText 5.5.7. However: since we release iText 5.5.6 less than a week ago, you'll have to wait a couple of months for that release, so it's best to follow the commits on Github and to build iText from the source as soon as you see the commits with respect to DEV-1393. – Bruno Lowagie May 12 '15 at 11:17
  • Happy to know, fix is coming with the next sprint ( DEV - 1393 )... Thanks – user2548712 May 14 '15 at 13:24
1

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;
}
1

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;
    }
Raviprakash
  • 2,410
  • 6
  • 34
  • 56