2012-07-25 81 views
0

我想從pdf文件,使用Itextsharp和Bitmiracle創建tif圖像。試圖從PDF創建Tif。越來越差的圖像

首先,我想使用iTextsharp獲取pdf文件每個頁面的字節細節。

string bpp = pd.Get(PdfName.BITSPERCOMPONENT).ToString(); 
PixelFormat pixelFormat; 
switch (bpp) 
{ 
    case "1": 
     pixelFormat = PixelFormat.Format1bppIndexed; 
     break; 
    case "8": 
     pixelFormat = PixelFormat.Format24bppRgb; 
     break; 
    default: 
     throw new Exception(String.Format("Unknown pixel format {0}.", bpp)); 
} 

之後,使用bitmiracle我保存該圖像在TIFF格式。但圖像不可見。

string filter = PDFStremObj.Get(PdfName.FILTER).ToString(); 
switch (filter) 
{ 
    case "/FlateDecode": 

    byte[] arr = PdfReader.GetStreamBytes((PRStream)PDFStremObj); 

    Bitmap bmp = new Bitmap(Int32.Parse(width), Int32.Parse(height), PixelFormat.Format24bppRgb); 
    BitmapData bmd = bmp.LockBits(new System.Drawing.Rectangle(0, 0, Int32.Parse(width), Int32.Parse(height)), ImageLockMode.WriteOnly, 
      PixelFormat.Format24bppRgb); 
    Marshal.Copy(arr, 0, bmd.Scan0, arr.Length); 
    bmp.UnlockBits(bmd); 
    bmp.Save(strFileNewName, System.Drawing.Imaging.ImageFormat.Tiff); 
    bmp.Dispose(); 
    page++; 
    break; 
} 

請幫我解決代碼中的問題,或者建議我更改。

在此先感謝您的幫助。

+0

爲什麼人們似乎認爲PDF是一種圖像格式?這顯然不是。 – Nyerguds 2018-01-08 15:29:20

回答

0

我不認爲可以將數組複製到位圖數據。步幅可能不同,比特深度可能不同,字節順序可能不同等等。 Lajja Thaker發佈了其中一種方法。 另請注意,在Windows XP下不支持一些彩色tiff。

0

Bitmiracle不是圖書館,it is a name of a software company,它是libtiff你試圖使用什麼?也許你可以聯繫Bitmiracle尋求你正在從事的任務的支持。

您不能通過從PDF中提取二進制數據並將其轉儲到位圖對象中來從PDF創建tiff圖像。這就像試圖通過擠壓蘋果來製作橙汁。

如果您想將PDF文件轉換爲TIFF圖像,我建議您使用允許您呈現PDF文件的庫。搜索PDF to Image conversion with C#瞭解更多詳情。

如果您只是想從PDF文件中提取已經壓縮爲TIFF的圖像,那麼您確實可以將extract the binary data保存到其他地方。