I’m using PDFBox 1.8.10 to add text to PDF documents. It works fine except on some documents where text is added to document (checked with pdf structure inspector) but not displayed in PDF. Sample document is here: https://kali-docs.ks2.fr/share/s/Ut_LdO8LR4WEeEd1y2k58Q
Because I want to set some custom AlphaConstant to text (and rectangles), I’m using Graphics State Parameter Dictionaries to add text.
Code used:
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();
Any idea ?
Thanks, Vincent