0

I am not getting how to add transparent text with the help of pdfBOX.

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
sandy
  • 93
  • 2
  • 9
  • Partial duplicate of https://stackoverflow.com/questions/4540593/draw-transparent-lines-with-pdfbox (see the non accepted answer, needs some adjustement for 2.0) – Tilman Hausherr Oct 12 '16 at 16:47
  • PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState(); graphicsState.setStrokingAlphaConstant(0.5f); COSName graphicsStateName = page.getResources().add(graphicsState); try (PDPageContentStream cs = new PDPageContentStream(document, page, true, true, true)) { cs.appendRawCommands("/" + graphicsStateName.getName() + " gs\n"); // draw your line here. } – sandy Oct 13 '16 at 06:15
  • I am using it with 1.8 and getting error as The method add(PDExtendedGraphicsState) is undefined for the type PDResources – sandy Oct 13 '16 at 06:16

1 Answers1

1

Here's something that shows alpha with 1.8 (you should use 2.*, that is a bit easier).

    PDExtendedGraphicsState gs1 = new PDExtendedGraphicsState();
    gs1.setNonStrokingAlphaConstant(1f);
    PDExtendedGraphicsState gs2 = new PDExtendedGraphicsState();
    gs2.setNonStrokingAlphaConstant(0.2f);
    Map<String, PDExtendedGraphicsState> graphicsStatesMap = page.getResources().getGraphicsStates();
    if (graphicsStatesMap == null)
    {
        graphicsStatesMap = new HashMap<String, PDExtendedGraphicsState>();
    }
    graphicsStatesMap.put("gs1", gs1);
    graphicsStatesMap.put("gs2", gs2);
    page.getResources().setGraphicsStates(graphicsStatesMap);
    cs.setFont(PDType1Font.HELVETICA_BOLD, 60);
    cs.setNonStrokingColor(255, 0, 0);
    cs.appendRawCommands("/gs1 gs\n");
    cs.beginText();
    cs.moveTextPositionByAmount(50, 600);
    cs.drawString("Apache PDFBox 1");
    cs.endText();
    cs.setNonStrokingColor(0, 0, 255);
    cs.appendRawCommands("/gs2 gs\n");
    cs.beginText();
    cs.moveTextPositionByAmount(70, 620);
    cs.drawString("Apache PDFBox 2");
    cs.endText();
    cs.close();
Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
  • *"however it does not do real transparency"* - what exactly do you mean by that? – mkl Oct 13 '16 at 16:22
  • @mkl I expected the 2nd text to somehow "merge" with the first one, but it didn't happen. The second text is clearly over the first one. I've created a second file with different colors: http://www.filedropper.com/transparent_1 – Tilman Hausherr Oct 13 '16 at 18:36
  • A transparent overlay still is an OVERlay. If you want something combining background and foreground without preference of either, you actually are looking for a *"rendering mode"* like **Darken** or **Multiply**. – mkl Oct 13 '16 at 20:08
  • @mkl You're right, thank you. That explains why it was missing on AR and not on other viewers. I'll fix that. – Tilman Hausherr Oct 13 '16 at 20:26
  • I just tested it, setting the graphics state outside of text objects works. ;) And those other viewers obviously are broken... ;)) – mkl Oct 13 '16 at 20:32