Remove or hide PDF layer using ABCPdf?

2019-05-22 14:35发布

问题:

Is is possible to remove or hide a layer from a PDF using ABCPdf or another framework?

回答1:

The following C# example shows how layer 2 of page 1 can be deleted:

Doc theDoc = new Doc();
theDoc.Read("source.pdf");
int thePages = theDoc.GetInfoInt(theDoc.Root, "Pages");
int thePage = theDoc.GetInfoInt(thePages, "Page 1");
int theLayer = theDoc.GetInfoInt(thePage, "Content 2");
theDoc.Delete(theLayer);


回答2:

Or perhaps you were looking for the Flatten() function?



回答3:

ABCpdf contains an Example project called OCGLayers. This project shows you how to identify and redact all the items in a layer.

For example:

        Properties props = Properties.FromDoc(_doc, false);
        Page page = (Page)_doc.ObjectSoup[_doc.Page];
        Reader reader = Reader.FromPage(props, page);
        List<OptionalContent.Layer> layers = reader.GetLayers();
        foreach (OptionalContent.Layer layer in layers) {
            if (layer.Visible == false) {
                if (reader == null)
                    reader = Reader.FromPage(props, page);
                Reader.Redact(ref reader, layer);
            }
        }
        UpdateLayers();
        UpdatePreview();