[英]GhostscriptRasterizer Objects Returns 0 as PageCount value
[英]GhostscriptRasterizer.PageCount always returns zero
這個問題已經在這里討論過: GhostscriptRasterizer Objects Returns 0 as PageCount value但是這個問題的答案並沒有幫助我解決問題。
就我而言,從 kat 到舊版本的 Ghostscript 沒有幫助。 26 和 25。我總是 PageCount = 0,如果版本低於 27,我會收到錯誤“未找到 Native Ghostscript 庫”。
private static void PdfToPng(string inputFile, string outputFileName)
{
var xDpi = 100; //set the x DPI
var yDpi = 100; //set the y DPI
var pageNumber = 1; // the pages in a PDF document
using (var rasterizer = new GhostscriptRasterizer()) //create an instance for GhostscriptRasterizer
{
rasterizer.Open(inputFile); //opens the PDF file for rasterizing
//set the output image(png's) complete path
var outputPNGPath = Path.Combine(outputFolder, string.Format("{0}_Page{1}.png", outputFileName,pageNumber));
//converts the PDF pages to png's
var pdf2PNG = rasterizer.GetPage(xDpi, yDpi, pageNumber);
//save the png's
pdf2PNG.Save(outputPNGPath, ImageFormat.Png);
Console.WriteLine("Saved " + outputPNGPath);
}
}
我在同樣的問題上苦苦掙扎,最終使用iTextSharp來獲取頁數。 以下是生產代碼的片段:
using (var reader = new PdfReader(pdfFile))
{
// as a matter of fact we need iTextSharp PdfReader (and all of iTextSharp) only to get the page count of PDF document;
// unfortunately GhostScript itself doesn't know how to do it
pageCount = reader.NumberOfPages;
}
不是一個完美的解決方案,但這正是解決我問題的方法。 我在那里留下那條評論是為了提醒自己,我必須以某種方式找到更好的方法,但我從來沒有費心回來,因為它可以正常工作......
PdfReader
類在iTextSharp.text.pdf
命名空間中定義。
我使用Ghostscript.NET.GhostscriptPngDevice
而不是GhostscriptRasterizer
來光柵化 PDF 文檔的特定頁面。
這是我對頁面進行光柵化並將其保存為 PNG 文件的方法
private static void PdfToPngWithGhostscriptPngDevice(string srcFile, int pageNo, int dpiX, int dpiY, string tgtFile)
{
GhostscriptPngDevice dev = new GhostscriptPngDevice(GhostscriptPngDeviceType.PngGray);
dev.GraphicsAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
dev.TextAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
dev.ResolutionXY = new GhostscriptImageDeviceResolution(dpiX, dpiY);
dev.InputFiles.Add(srcFile);
dev.Pdf.FirstPage = pageNo;
dev.Pdf.LastPage = pageNo;
dev.CustomSwitches.Add("-dDOINTERPOLATE");
dev.OutputPath = tgtFile;
dev.Process();
}
希望這會有所幫助...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.