繁体   English   中英

Java:PDF生成中带有iText的BOLD无法正常工作

[英]Java : BOLD with iText in PDF Generation doesn't work correctly

我使用iText从XML文件生成HTML内容的PDF。 除了一件小事情,一切都在工作。
当我有一组包含粗体部分的文本时,粗体不会出现在生成的PDF文件中。 如果我有一个完整的粗体字词,则表示效果很好。
例子 :

<DIV><FONT face='Arial' size='10'><B>The BOLD for this phrase works</B></FONT></DIV>
<DIV><FONT face='Arial' size='10'>The BOLD for <B>this part of the phrase </B> doesn't work</FONT></DIV>  

使用“斜体”或“下划线”,我可以进行相同的测试,但是没有问题。 运作中...
一点精度:如果我将标记<B>与标记<U><I>结合使用,则对于一部分文本来说,它也可以正常工作。
范例:

<DIV><FONT face='Arial' size='10'>The combination of <B><I>BOLD and something else (U or I)</I></B> works fine.</FONT></DIV>


对于上下文:带Struts的WebApp,PDF不会保存为文件,而是作为响应发送到导航器。 如答案所示,我将iText的版本从1.4.8更新为5.5.7。
有关保存在xml文件中的HTML代码,请参见上面的示例。
对于Java代码(我从几种长方法中选取了代码。希望我什么都没忘记...)。

ByteArrayOutputStream baoutLettre = new ByteArrayOutputStream();  
Document document = new Document();  
PdfWriter myWriter = PdfWriter.getInstance(document, baoutLettre);  
handleHeaderFooter(request, response, document, Constantes.Type_LETTRE);  
document.open();  
String lettreContent = FileHelper.readFile("myLetter.xml");  
XmlParser.parse(document, new ByteArrayInputStream(lettreContent.getBytes("UTF-8")), getTagMap());
document.close();
ByteArrayOutputStream outTmp = new ByteArrayOutputStream(64000);
PdfCopyMerge pdfCM = new PdfCopyMerge(outTmp);
pdfCM.addDocument(baoutLettre.toByteArray());
pdfCM.close();
ByteArrayOutputStream outPDF = addPageNumber(outTmp.toByteArray(), soc, dicoEdition, request);
outPDF.writeTo(request.getOutputStream());

对于PdfCopyMerge类:

public class PdfCopyMerge {

private ByteArrayOutputStream outStream = new ByteArrayOutputStream();
private Document document = null;
private PdfCopy writer = null;

public PdfCopyMerge(ByteArrayOutputStream stream) {
    super();
    outStream = stream;
}    
public int addDocument(byte[] pdfByteArray) {
    int numberOfPages = 0;
    try {
        PdfReader reader = new PdfReader(pdfByteArray);
        numberOfPages = reader.getNumberOfPages();
        if (this.document == null) {
            this.document = new Document(reader.getPageSizeWithRotation(1));
            this.writer = new PdfCopy(this.document, this.getOutputStream());
            this.document.open();
        }
        PdfImportedPage page;
       for (int i = 0; i < numberOfPages;) {
           ++i;
           page = this.writer.getImportedPage(reader, i);
           this.writer.addPage(page);
       }
       PRAcroForm form = reader.getAcroForm();
       if (form != null) {
           this.writer.copyAcroForm(reader);
       }
   } catch (Exception e) {
      logger.error(e.getMessage(),e);
   }
    return numberOfPages;
}

有人面临同样的问题吗? 我正在寻找任何有帮助的想法...
谢谢。

尝试最新版本5.5.7。 一切正常。 https://github.com/itext/itextpdf/tags

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM