簡體   English   中英

如何使用FlatDecode到PdfSharp庫

[英]How to use FlatDecode to PdfSharp library

我想從一些PDF文件中導出一些圖像,為此,我應該使用PdfSharp庫。 我在網上找到了從一個PDF導出圖像文件的代碼,但是如果圖像是通過DCTDecode進行編碼的,那么我沒有任何問題。 如果通過FlatDecode模式對圖像進行編碼,則無法導出該圖像。

所以這是代碼:

static void Main(string[] args)
{

    //estrapolare immagine da pdf
    const string filename = "d://eresult.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")
          {
            ExportImage(xObject, ref imageCount);
          }
        }
          }
        }
      }
}
System.Diagnostics.Debug.Write(imageCount + " images exported.", "Export Images");
}

static void ExportImage(PdfDictionary image, ref int count)
{
    string filter = image.Elements.GetName("/Filter");
    switch (filter)
    {
        case "/DCTDecode":
        ExportJpegImage(image, ref count);
        break;

        case "/FlateDecode":
        ExportAsPngImage(image, ref count);
        break;
    }
}

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("Image{0}.jpeg", count++), FileMode.Create, FileAccess.Write);
    BinaryWriter bw = new BinaryWriter(fs);
    bw.Write(stream);
    bw.Close();
}

static void ExportAsPngImage(PdfDictionary image, ref int count)
{
    int width = image.Elements.GetInteger(PdfImage.Keys.Width);
    int height = image.Elements.GetInteger(PdfImage.Keys.Height);
    int bitsPerComponent = image.Elements.GetInteger(PdfImage.Keys.BitsPerComponent);

    PdfSharp.Pdf.Filters.FlateDecode flate = new PdfSharp.Pdf.Filters.FlateDecode();
    byte[] decodedBytes = flate.Decode(image.Stream.Value);
    System.Drawing.Imaging.PixelFormat pixelFormat;

    switch (bitsPerComponent)
    {
        case 1:
        pixelFormat = PixelFormat.Format1bppIndexed;
        break;
        case 8:
        pixelFormat = PixelFormat.Format8bppIndexed;
        break;
        case 24:
        pixelFormat = PixelFormat.Format24bppRgb;
        break;
        default:
        throw new Exception("Unknown pixel format " + bitsPerComponent);
    }

    Bitmap bmp = new Bitmap(width, height, pixelFormat);
    var bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, pixelFormat);
    int length = (int)Math.Ceiling(width * bitsPerComponent / 8.0);
    for (int i = 0; i < height; i++)
    {
        int offset = i * length;
        int scanOffset = i * bmpData.Stride;
        Marshal.Copy(decodedBytes, offset, new IntPtr(bmpData.Scan0.ToInt32() + scanOffset), length);
    }
    bmp.UnlockBits(bmpData);
    using (FileStream fs = new FileStream(@"D:\\" + String.Format("Image{0}.png", count++), FileMode.Create, FileAccess.Write))
    {
        bmp.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
    }
// TODO: You can put the code here that converts vom PDF internal image format to a Windows bitmap
// and use GDI+ to save it in PNG format.
// It is the work of a day or two for the most important formats. Take a look at the file
// PdfSharp.Pdf.Advanced/PdfImage.cs to see how we create the PDF image formats.
// We don't need that feature at the moment and therefore will not implement it.
// If you write the code for exporting images I would be pleased to publish it in a future release
// of PDFsharp.
}

通過以下代碼,我可以在這種奇怪的模式下看到圖像:

在此處輸入圖片說明

但是Pdf文件中的圖像是這樣的:

在此處輸入圖片說明

如您所見,顏色差異太大

圖像數據和調色板是PDF文件中的不同對象。 圖像可以具有蒙版,這些蒙版也可以是不同的對象。

將圖像數據保存到PNG文件時,還可能需要獲取調色板並將顏色數據包括在PNG文件中。

也許PDFsharp論壇上顯示的代碼比您的代碼更好用:
http://forum.pdfsharp.net/viewtopic.php?p=6755#p6755

暫無
暫無

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

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