2009-05-19 107 views
15

我一直在使用ghostscript來執行PDF從圖像生成單頁圖像。現在我需要能夠從pdf中拉出多個頁面併產生一個長的垂直圖像。Ghostscript Multipage PDF to PNG

有沒有一個論點,我錯過了,這將允許這?

到目前爲止我用下面的參數,當我叫出的ghostscript:

string[] args ={ 
       "-q",      
       "-dQUIET",     
       "-dPARANOIDSAFER", // Run this command in safe mode 
       "-dBATCH", // Keep gs from going into interactive mode 
       "-dNOPAUSE", // Do not prompt and pause for each page 
       "-dNOPROMPT", // Disable prompts for user interaction       
       "-dFirstPage="+start, 
       "-dLastPage="+stop, 
       "-sDEVICE=png16m", 
       "-dTextAlphaBits=4", 
       "-dGraphicsAlphaBits=4", 
       "-r300x300",     

       // Set the input and output files 
       String.Format("-sOutputFile={0}", tempFile), 
       originalPdfFile 
      }; 

回答

10

我最終加入「%d」到「OUTPUTFILE」參數,這樣它會產生每頁一個文件。然後,我剛讀了所有的文件,並在我的C#代碼縫合在一起的他們,像這樣:

var images =pdf.GetPreview(1,8); //All of the individual images read in one per file 

using (Bitmap b = new Bitmap(images[0].Width, images.Sum(img=>img.Height))) { 
    using (var g = Graphics.FromImage(b)) { 
     for (int i = 0; i < images.Count; i++) { 
      g.DrawImageUnscaled(images[i], 0, images.Take(i).Sum(img=>img.Height)); 
     } 
    } 
    //Do Stuff 
} 
+0

的是它在某種程度上可以在腳本/ bash的水平,離不開高水平代碼的接線起來,像上面C#。 – Supra 2015-12-07 09:57:32

1

首先檢查輸出設備的選擇;但我不認爲有這樣的選擇。

很可能你需要自己做一些拼版,或者讓GhostScript做到這一點(你必須編寫一個PostScript程序),或者使用ImageMagick或類似的東西縫合生成的渲染頁面。

如果你想嘗試的PostScript路線(可能是最有效的),檢查包括在GhostScript的包N合一的例子。

6

如果你可以使用ImageMagick的,你可以利用其良好的一個命令:

montage -mode Concatenate -tile 1x -density 144 -type Grayscale input.pdf output.png 

哪裏

  • -density 144以dpi分辨率determins如果需要增加它,默認是72
  • -type Grayscale使用它,如果你的PDF沒有顏色,你會節省一些KB的最終圖像