gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:31:56 30 4
gpt4 key购买 nike

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

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

最佳答案

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

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

关于java - 我无法使用 itext (xmlWorker) 读取 h1 标签 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49893555/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com