0

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

  • see answer of https://stackoverflow.com/questions/27919436/pdfbox-pdpagecontentstreams-append-mode-misbehaving and https://stackoverflow.com/questions/14657602/cannot-figure-out-how-to-use-pdfbox – Tilman Hausherr Feb 16 '17 at 19:26

1 Answers1

1

Resetting graphic state solved my problem (fifth parameter of PDPageContentStream constructor).

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