簡體   English   中英

由PdfSharp C#Library提取的破碎圖像

[英]Broken images extracted by PdfSharp C# Library

我正在嘗試執行PDFSharp庫的示例程序 我已經在項目中提供了參考資料。

問題:代碼執行時沒有任何錯誤,我看到方法ExportJpegImage()被調用7次(這是pdf中的圖像數)。 但是當我嘗試打開程序寫入的圖像(在.jpeg )時,Windows無法打開它們。

static void Main(string[] args)
    {
        Console.WriteLine("Starting PDF Sharp sample program...");

        const string filename = @"D:/Test/test.pdf";

        PdfDocument document = PdfReader.Open(filename);

        int imageCount = 0;
        // Iterate pages
        foreach (PdfPage page in document.Pages)
        {
            // Get resources dictionary
            PdfDictionary resources = page.Elements.GetDictionary("/Resources");
            if (resources != null)
            {
                // Get external objects dictionary
                PdfDictionary xObjects = resources.Elements.GetDictionary("/XObject");
                if (xObjects != null)
                {
                    ICollection<pdfitem> items = xObjects.Elements.Values;
                    // Iterate references to external objects
                    foreach (PdfItem item in items)
                    {
                        PdfReference reference = item as PdfReference;
                        if (reference != null)
                        {
                            PdfDictionary xObject = reference.Value as PdfDictionary;
                            // Is external object an image?
                            if (xObject != null && xObject.Elements.GetString("/Subtype") == "/Image")
                            {
                                ExportJpegImage(xObject, ref imageCount);
                            }
                        }
                    }
                }
            }
        }

    }

 static void ExportJpegImage(PdfDictionary image, ref int count)
    {
        // Fortunately JPEG has native support in PDF and exporting an image is just writing the stream to a file.
        byte[] stream = image.Stream.Value;
        FileStream fs = new FileStream(String.Format(@"D:\Test\Image{0}.jpeg", count++), FileMode.Create, FileAccess.Write);
        BinaryWriter bw = new BinaryWriter(fs);
        bw.Write(stream);
        bw.Close();
    }

請閱讀該示例前面的注釋:

注意:此代碼段顯示如何從PDF文件導出JPEG圖像 PDFsharp無法將PDF頁面轉換為JPEG文件。 此示例不處理非JPEG圖像 它(尚未)處理已經過flate編碼的JPEG圖像。

PDF中的非JPEG圖像幾種不同的格式 這些簡單的樣本不支持這些,並且需要幾個小時的編碼,但這仍然是讀者的練習。

因此,您想要從PDF中提取的圖像很可能不是作為JPEG圖像嵌入的(至少沒有像通貨緊縮那樣進一步過濾)。 要將數據轉換為普通圖像查看器可以處理的內容,您很可能需要花費數小時的編碼時間

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM