PDFBox添加的文本未显示在PDF文档中



我正在使用pdfbox 1.8.10将文本添加到PDF文档中。除了在某些文档中添加文本(与PDF结构检查器检查)但未显示在PDF中的某些文档中,它可以正常工作。示例文档在这里:https://kali-docs.ks2.fr/share/s/ut_ldo8lr4weeed1y2k58q

因为我想将一些自定义的alphaconstant设置为文本(和矩形),所以我使用图形状态参数词典来添加文本。

使用的代码:

PDPageContentStream contentStream = new PDPageContentStream(pdfDoc, pdfPage, true, true);                
this.textGraphicState = new PDExtendedGraphicsState();
textGraphicState.setNonStrokingAlphaConstant(1f);
Map<String, PDExtendedGraphicsState> graphicsStatesMap = pdfPage.getResources().getGraphicsStates();
if (graphicsStatesMap == null)
{
    graphicsStatesMap = new HashMap<String, PDExtendedGraphicsState>();
}
graphicsStatesMap.put("textGraphicState", textGraphicState);
pdfPage.getResources().setGraphicsStates(graphicsStatesMap);
contentStream.appendRawCommands("/textGraphicState gsn");
contentStream.setNonStrokingColor(fontColor);
contentStream.beginText();
contentStream.setFont( font, fontSize );
contentStream.moveTextPositionByAmount( pagePosX, pagePosY );
contentStream.drawString(text);
contentStream.endText();
contentStream.close();

有什么想法吗?

谢谢Vincent

重置图形状态解决了我的问题(pdpagecontentstream构造函数的第五参数)。

pdpageContTream contentsTream = new PdpageContentsTream(pdfdoc,pdfpage,true,true,true,true);

最新更新