我无法使用itext(xmlWorker)读取h1标签pdf

时间:2018-04-18 07:22:27

标签: java xmlworkerhelper

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

对于我的代码,除了标题标记之外所有其他标记都正常工作

1 个答案:

答案 0 :(得分:0)

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

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