如何使用iTextSharp确定PDF文件类型

时间:2012-11-16 22:53:50

标签: c# pdf itextsharp

有没有办法确定PDF文件的类型:如果现有PDF文件是扫描图像,或者是否使用iTextSharp和C#从数据文件创建?

3 个答案:

答案 0 :(得分:0)

文档属性/高级/ Pdf制作人

答案 1 :(得分:0)

也许您可以为使用iTextSharp创建的PDF添加一些元数据。

Read/Modify PDF Metadata using iTextSharp

答案 2 :(得分:0)

我刚刚在PdfWriter对象的监视窗口中搜索正确的位置后,使用此方法替换PDF Producer,它会更改PDF中的“PDF Creator”,因为默认情况下无法访问它:

    private static void ReplacePdfCreator(PdfWriter writer)
    {
        /*

         Warning
         * 
         This is not an option offered as is and i had to workaround it by using Reflection and change it
         manually.
         * 
         Alejandro

         */
        Type writerType = writer.GetType();
        PropertyInfo writerProperty =
            writerType.GetProperties(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)
                      .FirstOrDefault(p => p.PropertyType == typeof(PdfDocument));

        if (writerProperty != null)
        {
            PdfDocument pd = (PdfDocument)writerProperty.GetValue(writer);
            Type pdType = pd.GetType();
            FieldInfo infoProperty =
                pdType.GetFields(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)
                      .FirstOrDefault(p => p.Name == "info");

            if (infoProperty != null)
            {
                PdfDocument.PdfInfo pdfInfo = (PdfDocument.PdfInfo)infoProperty.GetValue(pd);

                if (pdfInfo != null)
                {
                    string creator = pdfInfo.GetAsString(new PdfName("Producer")).ToLowerInvariant();

        if(creator.Contains("itextsharp"))
        {
            // created with itext sharp
        }
        else if(creator.Contains("adobe"))
        {
            // created with adobe something (distiller, photoshop, whatever)
        }
        else if(creator.Contains("pdfpro"))
        {
            // created with pdf pro
        }
        else if(add your own comparison here, for example a scanner manufacturer software like HP's one)
        {
        }
                }
            }
        }
}