pdfpdfbox

Remove spotcolor elements with pdfbox


I have the following operators and operands and would like to remove all non spotcolor elements:

/DeviceRGB CS 0 0 1 SC 250 791.8898 m 260 811.8898 l 270 791.8898 l 250 791.8898 l /cs1 CS 0.5 SCN s

Using the stream i can check what kind of stroking or non stroking colorspace is currently set. In this setup the moveTo and lineTo operators are in DeviceRGB space so they are removed. But the actual stroking s is done using /cs1 Colorspace. Is it possible to use the PDFGraphicsStreamEngine and find out if a operator is relevant in this case ?

At the moment i would remove all operators before the /cs1 CS entry because !(getGraphicsState().getStrokingColorSpace() instanceof PDSeparation) would return false.


Solution

  • Your content stream excerpt is broken,

    250 791.8898 m 
    260 811.8898 l 
    270 791.8898 l 
    250 791.8898 l 
    /cs1 CS 
    0.5 SCN 
    s
    

    between the path building (m and l instructions) and the path drawing (s instruction) color space and color setting operations are not allowed. So you're in a garbage-in, garbage-out situation.

    (PDF viewers often ignore such errors and draw the path anyways, some making use of the provided color information, some dropping them. Nonetheless, this is broken.)


    If you need to handle such situations in spite of the above said, consider not removing the path drawing operations (m, l, re, h, ...) and handling path drawing instructions (s, S, ...) by replacing them by a n.

    By the way, for a generic approach you need to handle the situation more cautiously anyways.

    On one hand there are path drawing instructions that both stroke and fill. If you find such an instruction, you have to check both the stroking and the non-stroking colorspaces. If only one of them is a spot color, you have to replace the drawing instruction by one that only strokes or only fills accordingly.

    On the other hand, a path clipping instruction may be associated with the path construction and drawing you inspect. In that case, even if the path would be drawn in a non-spot color, you cannot simply remove it, you still need to add it to the clip path.