简体   繁体   中英

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.

Read/Modify PDF Metadata using iTextSharp

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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