【问题标题】:Why doesn't ABCPDF count a PDF's layers correctly?为什么 ABCPDF 不能正确计算 PDF 的层数?
【发布时间】:2013-08-12 18:28:26
【问题描述】:

我有下面的方法应该返回 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;


    }

【问题讨论】:

  • 作为一个想法,我使用我使用 ABCPDF 分层的 pdf 测试了我的方法,并且 LayerCount 方法按预期工作。所以,我不再认为 ABCPDF 在阅读过程中会压平 PDF。所以,现在我不知道为什么 LayerCount 不适用于没有通过 ABCPDF 添加图层的文件。任何帮助表示赞赏。
  • 作为更新,我发现 iTextSharp 有一个 GetPDFLayers 方法,可以正确提供 PDF 中的层数(使用 ABCPDF 创建的层和未创建的层)。谢谢。
  • 您使用的是什么版本的 ABCPDF?
  • ABCpdf 图层与 Acrobat 可选内容组 (OCG) 不同。前者与 ABCpdf 添加的视觉组有关。后者涉及可见或不可见的视觉组。前者在页面的 Contents 条目中表示为单独的流。后者通过特殊的 PDF 标签表示。我们(在 ABCpdf)确实有处理基于 OCG 的图层的示例代码,如果您直接联系我们,我们会将其发送给您。 :-)

标签: layer abcpdf


【解决方案1】:

以下是使用 iTextSharp 的相同方法。我测试了在 ABCPDF 中创建的分层 PDF 文件以及那些没有在这两种情况下创建的 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)
            {
            }

        }
               }

【讨论】:

    猜你喜欢
    • 2020-07-09
    • 2013-12-12
    • 1970-01-01
    • 2022-01-14
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    相关资源
    最近更新 更多