2017-02-16 35 views
0

我正在使用PDFBox 1.8.10将文本添加到PDF文档。 除了一些将文本添加到文档(使用pdf结构检查器进行检查)但未在PDF中显示的文档之外,它工作正常。 示例文档位于:https://kali-docs.ks2.fr/share/s/Ut_LdO8LR4WEeEd1y2k58QPDFBox新增文本不会出现在PDF文档中

因为我想将一些自定义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 gs\n"); 
contentStream.setNonStrokingColor(fontColor); 
contentStream.beginText(); 
contentStream.setFont(font, fontSize); 
contentStream.moveTextPositionByAmount(pagePosX, pagePosY); 
contentStream.drawString(text); 
contentStream.endText(); 
contentStream.close(); 

任何想法?

感谢, 文森特

+0

看到回答https://stackoverflow.com/questions/27919436/pdfbox-pdpagecontentstreams-append-mode-misbehaving和https://stackoverflow.com/questions/14657602/cannot-figure-out-how-to-使用-PDFBOX –

回答

1

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

PDPageContentStream contentStream = new PDPageContentStream(pdfDoc,pdfPage,true,true,true);

相关问题