简体   繁体   English

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

[英]how to determine PDF file type using iTextSharp

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

文档属性/高级/ Pdf生成器

maybe you can add some metadata to the PDF you create with iTextSharp. 也许您可以使用iTextSharp为您创建的PDF添加一些元数据。

Read/Modify PDF Metadata using iTextSharp 使用iTextSharp读取/修改PDF元数据

I just made this method to replace the PDF Producer after searching the right place in the watch window of the PdfWriter object, it changes the "PDF Creator" in the PDF as it is not accessible by default: 我刚刚在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)
        {
        }
                }
            }
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM