我有以下方法,该方法应该返回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;
}
答案 0 :(得分: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)
{
}
}
}