【问题标题】:i am not able to read h1 tag pdf using itext (xmlWorker)我无法使用 itext (xmlWorker) 读取 h1 标签 pdf
【发布时间】:2018-04-18 07:22:26
【问题描述】:
public PdfPCell richTextRendering(PdfPCell pdfpCell, String HTML) throws DocumentException, IOException {

    final String CSS = "";
    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;
}

proposalDescriptionTableCell.setBorder(Rectangle.NO_BORDER);
String proposalDescriptionEnglish=proposalInformation.getProposalDescriptionEnglish().trim();
proposalDescriptionEnglish=proposalDescriptionEnglish.replaceAll("div", "p");     //XMLWorkerHelper not taking rendering <div> content its recommended to replace all the <div> with <p> 
proposalDescriptionEnglish=proposalDescriptionEnglish.replaceAll("<br>", "<br/>");
proposalDescriptionEnglish="<p>"+proposalDescriptionEnglish+"</p>";         //some tags are not supported the plug-in keep the code inside the <p> for safe execution
proposalDescriptionTableCell=new CustomerViewPdfDelegationServiceImpl().richTextRendering(proposalDescriptionTableCell,proposalDescriptionEnglish);  //getting the pdfPCell with HtmlRendered-Text

对于我的代码,除了标题标签之外,所有其他标签都可以工作

【问题讨论】:

    标签: java xmlworkerhelper


    【解决方案1】:

    我得到了答案,请看下面的代码

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

    【讨论】:

      猜你喜欢
      • 2013-09-24
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-27
      • 2015-12-05
      • 1970-01-01
      相关资源
      最近更新 更多