0

I have the below method that was supposed to return the number of layers in a PDF but it doesn't appear to work. When I pass the path to a PDF file containing two layers, the layer count property has a value of 1. I confirmed with two different PDF readers that there are two layers in the PDF. My only thought is ABCPDF is flattening the PDF layers during the read. If so, how can I prevent that so an accurate count of the number of layers in the PDF is returned?

Thank you

    public static int GetPDFLayerCount(string pdfFile)
    {

        int layerCount = 0;

        Doc doc = new Doc();
        doc.SetInfo(0, "License", _License);

        // Attempt to read File
        try
        {
            if (System.IO.File.Exists(pdfFile) == false)
            {
                throw new ApplicationException("File does not exist.");
            }

            doc.Read(pdfFile);
            layerCount = doc.LayerCount;

            doc.Clear();
            doc.Dispose();

        }
        catch (Exception ex)
        {
            System.ApplicationException appException = new ApplicationException(ex.Message + "\r\n\r\n" + pdfFile,ex);
            throw appException;
        }

        return layerCount;


    }
rsine
  • 91
  • 1
  • 7
  • As a thought, I tested my method using a pdf I layered using ABCPDF and the LayerCount method worked as expected. So, I no longer think ABCPDF is flattening a PDF during the read. So, now I'm at a loss to determine why the LayerCount isn't working for a file that didn't have layers added via ABCPDF. Any help is appreciated. – rsine Aug 12 '13 at 19:36
  • As an update, I found that iTextSharp has a GetPDFLayers method that correctly provides the count of layers in a PDF (both those created with ABCPDF and those that were not). Thanks. – rsine Aug 13 '13 at 12:00
  • What version of ABCPDF are you using? – Ross Presser Aug 14 '13 at 09:20
  • 1
    ABCpdf layers are different from Acrobat Optional Content Groups (OCG s). The former relate to visual groups added by ABCpdf. The latter relate to visual groups which can be visible or invisible. The former are represented as individual streams within the Contents entry of a page. The latter are represented via special PDF tags. We (at ABCpdf) do have sample code for dealing with OCG based layers and if you contact us direct we will send it to you. :-) – OnceUponATimeInTheWest Aug 28 '13 at 09:47

1 Answers1

0

Below is the same method using iTextSharp. I tested with layered PDF files created in ABCPDF and those that were not and it worked in both instances.

    public static int GetPDFLayerCount(string pdfFile, bool includeHiddenLayersInCount = true)
    {
        int layerCount = 0;

        string tempOutputFile = "";

        try
        {

            tempOutputFile = System.IO.Path.GetTempFileName();

            iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(pdfFile);

            iTextSharp.text.pdf.PdfStamper pdfStamper = new iTextSharp.text.pdf.PdfStamper(pdfReader, new System.IO.FileStream(tempOutputFile, System.IO.FileMode.Create));

            System.Collections.Generic.Dictionary<string, iTextSharp.text.pdf.PdfLayer> layers = pdfStamper.GetPdfLayers();

            layerCount = layers.Count;


            if (!includeHiddenLayersInCount)
            {
                foreach (System.Collections.Generic.KeyValuePair<string, iTextSharp.text.pdf.PdfLayer> dictLayer in layers)
                {
                    iTextSharp.text.pdf.PdfLayer layer = (iTextSharp.text.pdf.PdfLayer)dictLayer.Value;

                    //On = whether a layer is hidden or visible. If false, layer is hidden.
                    //
                    //OnPanel = the visibility of the layer in Acrobat's layer panel.  If false, the layer cannot be directly manipulated by the user and appears hidden. 
                    //Note that any children layers will also be absent from the panel.
                    if (layer.Value.On == false || layer.Value.OnPanel == false)
                    {
                        layerCount--;
                    }
                } 
            }

            pdfStamper.Close();
            pdfReader.Close();

        }
        catch (Exception ex)
        {
            System.ApplicationException appException = new ApplicationException(ex.Message + "\r\n\r\n" + pdfFile, ex);
            throw appException;
        }
        finally
        {

            try
            {
                if (!String.IsNullOrEmpty(tempOutputFile))
                {
                    System.IO.File.Delete(tempOutputFile); 
                }
            }
            catch (Exception ex)
            {
            }

        }
               }
rsine
  • 91
  • 1
  • 7