[ 
https://issues.apache.org/jira/browse/PDFBOX-4399?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16744394#comment-16744394
 ] 

Tilman Hausherr commented on PDFBOX-4399:
-----------------------------------------

One can filter the active text in the stripper by adding this code:

in constructor:
{code}
        addOperator(new BeginMarkedContentSequence());
        addOperator(new BeginMarkedContentSequenceWithProperties());
        addOperator(new EndMarkedContentSequence());    }
{code}
At the beginning of processTextPosition:
{code}
        if (!isContentRendered())
        {
            return;
        }
{code}
and at the end:
{code}

    @Override
    public void beginMarkedContentSequence(COSName tag, COSDictionary 
properties)
    {
        if (nestedHiddenOCGCount > 0)
        {
            nestedHiddenOCGCount++;
            return;
        }
        if (tag == null || getCurrentPage().getResources() == null)
        {
            return;
        }
        PDPropertyList propertyList = 
getCurrentPage().getResources().getProperties(tag);
        if (propertyList instanceof PDOptionalContentGroup)
        {
            PDOptionalContentGroup group = (PDOptionalContentGroup) 
propertyList;
            PDOptionalContentGroup.RenderState printState = 
group.getRenderState(RenderDestination.EXPORT /**/);
            if (printState == null)
            {
                if (!isGroupEnabled(group))
                {
                    nestedHiddenOCGCount = 1;
                }
            }
            else if (PDOptionalContentGroup.RenderState.OFF.equals(printState))
            {
                nestedHiddenOCGCount = 1;
            }
        }
    }

    @Override
    public void endMarkedContentSequence()
    {
        if (nestedHiddenOCGCount > 0)
        {
            nestedHiddenOCGCount--;
        }
    }

    private boolean isContentRendered()
    {
        return nestedHiddenOCGCount <= 0;
    }
    
    private int nestedHiddenOCGCount;

    public boolean isGroupEnabled(PDOptionalContentGroup group) /**/
    {
        PDOptionalContentProperties ocProperties = 
document.getDocumentCatalog().getOCProperties();
        return ocProperties == null || ocProperties.isGroupEnabled(group);
    }
{code}

After that worked I tried to move it to the base class and failed - we need 
PDPage and PDDocument. The base class doesn't have access to the document.

I'm not sure if we should pass PDDocument to the base class, although I don't 
see a reason against it.

But considering that no user has asked for the feature, I'm resolving this. We 
can still open a new issue.

> Disabled optional content groups are rendered
> ---------------------------------------------
>
>                 Key: PDFBOX-4399
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-4399
>             Project: PDFBox
>          Issue Type: Bug
>          Components: Rendering
>    Affects Versions: 2.0.6, 2.0.13
>            Reporter: Stefan Ziel
>            Priority: Major
>              Labels: OCGs
>             Fix For: 2.0.14, 3.0.0 PDFBox
>
>         Attachments: 1822-AGB.pdf, 
> PDFBOX-4399-046615_p9_OCG-NoRotate-Annotations.pdf, PDFBOX-4399-2.patch, 
> gs.png, original.pdf, pop-bugzilla42823-OCG.pdf, printed.png
>
>
> Printing  a Documents with hidden optional content groups [^original.pdf] 
> renders hidden content [^printed.png]. Code used to print
> {code:java}
> InputStream sourceStream = new FileInputStream(pFile);
> try {
>   PDDocument source = PDDocument.load(sourceStream);
>   job.setPageable(new PDFPageable(source));
>   job.print(atts);
> } finally {
>   sourceStream.close();
> }
> {code}
> This is not only a problem of PDFBox ;) but can be done right ... ghostscript 
> does it [^gs.png].



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to