0

I am using below code, which is changing the color of blue links to black from pdf:

void testAllRgbBlueToBlackConverter(String resourceName, String resultName) throws IOException {
    System.out.printf("\nConverting '%s'.\n", resourceName);
    try (   InputStream resource = getClass().getResourceAsStream(resourceName);
            PdfReader pdfReader = new PdfReader(resource);
            OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, resultName));
            PdfWriter pdfWriter = new PdfWriter(result);
            PdfDocument pdfDocument = new PdfDocument(pdfReader, pdfWriter) ) {
        PdfCanvasEditor editor = new AllRgbBlueToBlackConverter();
        for (int i = 1; i <= pdfDocument.getNumberOfPages(); i++)
        {
            editor.editPage(pdfDocument, i);
        }
    }
}

 class AllRgbBlueToBlackConverter extends PdfCanvasEditor {
    @Override
    protected void write(PdfCanvasProcessor processor, PdfLiteral operator, List<PdfObject> operands)
    {
        String operatorString = operator.toString();

        if (RGB_SETTER_CANDIDATES.contains(operatorString) && operands.size() == 4) {
            if (isBlue(operands.get(0), operands.get(1), operands.get(2))) {
                PdfNumber number0 = new PdfNumber(0);
                operands.set(0, number0);
                operands.set(1, number0);
                operands.set(2, number0);
            }
        }

        super.write(processor, operator, operands);
    }

    boolean isBlue(PdfObject red, PdfObject green, PdfObject blue) {
        if (red instanceof PdfNumber && green instanceof PdfNumber && blue instanceof PdfNumber) {
            float r = ((PdfNumber)red).floatValue();
            float g = ((PdfNumber)green).floatValue();
            float b = ((PdfNumber)blue).floatValue();
            return b > .5f && r < .9f*b && g < .9f*b;
        }
        return false;
    }

    final Set<String> RGB_SETTER_CANDIDATES = new HashSet<>(Arrays.asList("rg", "RG", "sc", "SC", "scn", "SCN"));
}

Can I restrict above code, in which i restrict that change color only when text is equal to "bla bla". Otherwise dont change color? This code changes the color of graph line as well, if there is any graph available in pdf.

I am using itext. If anyone have any code available in other library. Please share it with me. I am stuck on this since month. I want to change the color of specific text from blue to black.

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
Asad Rao
  • 3,190
  • 1
  • 22
  • 26
  • I removed the PDFBox label. I don't know itext, but PDF is not really made for editing. – Tilman Hausherr Oct 24 '19 at 17:37
  • I'm using it too sometimes. The problem here is he wants to change the text color as well, not just the link color. So this means that an existing text output may have to be splitted in three parts, one with the old color, one with the new color (where the link is), and then again one with the old color. And the position of the middle text will also have to be adjusted. All this is possible, but might take some time. – Tilman Hausherr Oct 25 '19 at 08:46
  • This answer may help somewhat: https://stackoverflow.com/questions/58475104/filter-out-all-text-above-a-certain-font-size-from-pdf/ – Tilman Hausherr Oct 25 '19 at 10:48
  • Yes @TilmanHausherr i just want to change the links color to black. That's it. – Asad Rao Oct 25 '19 at 12:45
  • So you just want to change the line and not the text? Then it is easier, in PDFBox you'd just call `getAnnotation()` on the `PDPage` object, and then check which of these is a `PDAnnotationLink`, then call `setColor(new PDColor(new float[] { 0 }, PDDeviceGray.INSTANCE));`. However this will work only if your link is an annotation. The code in your question is different, it changes the color of all lines, which brings the problem of having to decide on text content, position, etc., and then it gets really complicated. So if possible, please link to your PDF. – Tilman Hausherr Oct 25 '19 at 13:04
  • 1
    @TilmanHausherr the op in previous questions (then focused on itext) provided some example files. In them the lines were *not* generated by annotations but part of the page content. Furthermore, those lines were in some documents drawn at the start of the contents and in some after the text. So, there most likely a two-step solution, first analyzing the steam, later manipulation, will be necessary. And as the files the OP operates on come from different pdf creators, this analysis must be quite flexible. – mkl Oct 26 '19 at 06:31
  • OK... for analysis, two parts may be a good start: the PrintTextLocations.java example, and this answer that catches lines. https://stackoverflow.com/questions/38931 And the answer that you provided yesterday. All this is not easy... – Tilman Hausherr Oct 26 '19 at 11:05

0 Answers0