簡體   English   中英

如何使用iTextSharp確定PDF文件類型

[英]how to determine PDF file type using iTextSharp

有沒有辦法確定PDF文件的類型:如果現有PDF文件是掃描圖像,或者是否使用iTextSharp和C#從數據文件創建?

文檔屬性/高級/ Pdf生成器

也許您可以使用iTextSharp為您創建的PDF添加一些元數據。

使用iTextSharp讀取/修改PDF元數據

我剛剛在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