0

我有下面的方法应该返回 PDF 中的层数,但它似乎不起作用。当我将路径传递给包含两个图层的 PDF 文件时,图层计数属性的值为 1。我通过两个不同的 PDF 阅读器确认 PDF 中有两个图层。我唯一的想法是 ABCPDF 在阅读过程中将 PDF 图层展平。如果是这样,我怎样才能防止返回 PDF 中层数的准确计数?

谢谢

    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;


    }
4

1 回答 1

0

下面是使用 iTextSharp 的相同方法。我测试了在 ABCPDF 中创建的分层 PDF 文件和那些没有的文件,它在这两种情况下都有效。

    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)
            {
            }

        }
               }
于 2013-08-13T12:07:24.793 回答