Here I am trying to resolve this low level.
Basically when I encounter a BEGIN_TEXT operator I want to add the tokens for 
setting the color space and the non stroking color.



        PDPage spotsPage = this.spotsDocument.getPage(0);
        PDPage docPage = doc.getPage(0);

        // copy color space
        for (COSName name: spotsPage.getResources().getColorSpaceNames()) {
            PDColorSpace pdcs = spotsPage.getResources().getColorSpace(name);
            docPage.getResources().add(pdcs);
        }

        PDPageTree pages = doc.getDocumentCatalog().getPages();
        for (PDPage page : pages) {
            PDFStreamParser parser = new PDFStreamParser(page);
            parser.parse();
            List tokens = parser.getTokens();
            for (int j = 0; j < tokens.size(); j++) {
                Object next = tokens.get(j);
                if (next instanceof Operator) {
                    Operator op = (Operator) next;
                    System.out.println("Operator name:"+op.getName());
                    // Tj and TJ are the two operators that display strings in 
a PDF
                    // Tj takes one operator and that is the string to display 
so lets update that operator

                    if (op.getName().equals(OperatorName.BEGIN_TEXT)) {
                        System.out.println("begin text operator");
                        COSName csName = COSName.CS; // how to set name to CS0 
??
                        Operator csOperator = 
Operator.getOperator(OperatorName.NON_STROKING_COLORSPACE);
                        COSInteger scnInt = COSInteger.get(1);
                        Operator scnOperator = 
Operator.getOperator(OperatorName.NON_STROKING_COLOR_N);
                        tokens.addAll(j+1, Arrays.asList(csName, csOperator, 
scnInt, scnOperator));
                    }

                }
            }

            PDStream newContents = new PDStream(doc);
            writeTokensToStream(newContents, tokens);
            page.setContents(newContents);
//            processResources(page.getResources());
            doc.save("a2.pdf");
            doc.close();


However I am unable to initialise a COSName with a specific value (CS0). (The 
constructor is private).

What would be the alternative for creating these tokens and inserting them in?




On 14 Aug 2020, at 17:03, Tilman Hausherr 
<thaush...@t-online.de<mailto:thaush...@t-online.de>> wrote:

Am 14.08.2020 um 14:34 schrieb Pop, Rares:
Thanks again, I am getting the hang of it.
Working with tokens (operators) is quite tedious.
In my case a non stroking color operator is not enough, the color name is just 
an entry in the color space. I need to define (create) the space too.

You don't need to create the colorspace, you mentioned you have a PDF with it. 
You can get it from its page resources.

If you work on the high level you could prepend a content stream, see the 
constructor of PDPageContentStream. In that case, you'd do

PDPageContentStream pcs = new PDPageContentStream(doc, page, 
AppendMode.PREPEND, true);

PDColor color = new PDColor(new float[]{0}, colorSpace);   // (replace 0 with 
the actual value)
this.setNonStrokingColor(color);

This makes sense only if the color is set nowhere.


But if you have to replace stuff in the content stream, then insert the 
colorspace into the resources of the page of the new PDF. Inserting would be

COSName csName = resources.add(colorspace);

get the string of that name with csName.getName() .

That name then needs to be used like this: "name cs 0 sc". (replace 0 with the 
actual value)

Here's an example where the content stream is changed:

https://stackoverflow.com/questions/45246141/

Tilman



Is there a way (read sample source code) to mix working with PD content streams 
and low-level parser streams?
Like use high-level methods to create/copy a resource, get the change operators 
and insert them in another stream?

Thank you.

On 14 Aug 2020, at 15:03, Tilman Hausherr 
<thaush...@t-online.de<mailto:thaush...@t-online.de>> wrote:

Am 14.08.2020 um 13:05 schrieb Pop, Rares:
Thank you. I have identified the operator SetNonStrokingColorOperator but I 
think I am missing something.

I have a pdf of sample Spot colours. I am loading it and reading all the 
colours from the ColorSpace resources.

Then I must read an inputPDF (that has no spot colors - i.e. no resources in 
the color space).
I must simply apply a given spot color to all the elements within the inputPDF.
Then save it as outputPDF.

Is there an example that does anything remotely similar to that?


No... what you could do is about like this:

- open both PDFs

- get the spot color

- add it to the destination PDF

- remember the name (probably "cs1")

- replace the old color setting with the new in the content stream

- save

so maybe in the old PDF, you find "0 g" / "0 0 0 rg", then replace that with 
the appropriate colorspace selection and then color selection, e.g. "cs1 cs 0 
sc" . An example how to work with the content stream can be found in 
RemoveAllTexts.java in the source code download.

If there is nothing, then include the two commands mentioned at the beginning.

Tilman



I am happy to pay for consultancy here.

Best,
Ra



On 2020/08/14 10:46:56, Tilman Hausherr <t...@t-online.de<http://t-online.de>> 
wrote:
Am 14.08.2020 um 12:21 schrieb Pop, Rares:>
Hi guys,>
I am new to pdfbox and I am trying to modify the color of an existing text 
element.>
My current understanding is that I need to parse the stream, iterate through 
the tokens and find the one I need to change the color for.>
Then insert a Change Color Operator before it.>
Is this the correct way and could you please show me how to create a change 
colour operator?>

Yes, but it may be difficult to find the correct element. The best would >
be to have a look at that file with PDFDebugger, you can see all the >
operators there. When you hover the mouse over such a color operator it >
will show you the color.>

Tilman>


--------------------------------------------------------------------->
To unsubscribe, e-mail: 
users-unsubscr...@pdfbox.apache.org<mailto:users-unsubscr...@pdfbox.apache.org>>
For additional commands, e-mail: 
users-h...@pdfbox.apache.org<mailto:users-h...@pdfbox.apache.org>>

---------------------------------------------------------------------
To unsubscribe, e-mail: 
users-unsubscr...@pdfbox.apache.org<mailto:users-unsubscr...@pdfbox.apache.org>
For additional commands, e-mail: 
users-h...@pdfbox.apache.org<mailto:users-h...@pdfbox.apache.org>



---------------------------------------------------------------------
To unsubscribe, e-mail: 
users-unsubscr...@pdfbox.apache.org<mailto:users-unsubscr...@pdfbox.apache.org>
For additional commands, e-mail: 
users-h...@pdfbox.apache.org<mailto:users-h...@pdfbox.apache.org>




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: users-h...@pdfbox.apache.org


Reply via email to