Removing layers / background from PDF in PHP / Bash / C #

I have some PDF files that I need to modify using a PHP script. I can also exec (), so I can use almost everything that works on CentOS.

PDF files opened through Adobe Acrobat Pro X show 2 layers in the Layers panel:

  • Background
  • Colour

When I turn off both of these layers, I get black and white text and images (the text is not a tho vector, it is a scanned document).

I want to disable these layers and any other similar layer found in PDF files using PHP and / or C # or any command line tool.

Other useful information:

When I run pdfimages (comes with XPDF) in my PDF files, it extracts exactly what I really need to remove from each page ...

Additional Information Update: I changed the PDFSharp example here: http://www.pdfsharp.net/wiki/ExportImages-sample.ashx :

Modified:
Line 28:ExportImage(xObject, ref imageCount);

To:
PdfObject obj = xObject.Elements.GetObject("/OC");
Console.WriteLine(obj);

I got the following output in the console for each image:
<< /Name Background /Type /OCG >>
<< /OCGs [ 2234 0 R ] /P /AllOff /Type /OCMD >>
<< /Name Text Color /Type /OCG >>

This is actually layer information and PDFSharp documentation for the / OC switch:

Before image processing, its visibility is determined based on this record. If it is determined to be invisible, the entire image is skipped, as if there was no Do operator to call it.

So, how do I change the / OC value to what makes these layers invisible?

+3
source share
1 answer

After many hours of experimentation, I found a way! I am posting a code so that someone can find it useful in the future:

using System;
using System.IO;
using System.Collections.Generic;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace LayerHide {

    class MainClass
    {
        public static void Main (string[] args)
        {

            PdfReader reader = new PdfReader("test.pdf");
            PdfStamper stamp = new PdfStamper(reader, new FileStream("test2.pdf", FileMode.Create));
            Dictionary<string, PdfLayer> layers = stamp.GetPdfLayers();

            foreach(KeyValuePair<string, PdfLayer> entry in layers )
            {
                PdfLayer layer = (PdfLayer)entry.Value;
                layer.On = false;
            }

            stamp.Close();
        }
    }
}
+8
source

All Articles