C#中Ghostscript.NET提取PDF前三页后生成新PDF的技术疑问
问题描述
需求:提取PDF文件的前三页,展示供审批后生成仅包含这些已审批页面的新PDF。
当前实现:在C#应用中使用Ghostscript.NET将所需页面提取为位图,保存和审批功能已完成,但存在以下疑问:
- Ghostscript.NET中
pageimage.Save不支持PDF格式,该如何实现? - 如何拼接位图,使其成为新PDF中的独立页面?
- 是否需要使用其他库来生成目标PDF?
附提取代码:
GhostscriptVersionInfo lastInstalledVersion = GhostscriptVersionInfo.GetLastInstalledVersion( GhostscriptLicense.GPL | GhostscriptLicense.AFPL, GhostscriptLicense.GPL); // Ghostscript.NET.GhostscriptVersionInfo gvi = // new Ghostscript.NET.GhostscriptVersionInfo(AppDomain.CurrentDomain.BaseDirectory + @"Ghostscript.NET.dll"); byte[] binaryPdfData = null; binaryPdfData = File.ReadAllBytes(file); var pdfDataStream = new MemoryStream(binaryPdfData); using (var rasterizer = new Ghostscript.NET.Rasterizer.GhostscriptRasterizer()) { rasterizer.CustomSwitches.Add("-dNEWPDF=false"); rasterizer.Open(pdfDataStream, lastInstalledVersion, false); System.Drawing.Image pageImage1 = rasterizer.GetPage(100, 1); System.Drawing.Image pageImage2 = rasterizer.GetPage(100, 2); System.Drawing.Image pageImage3 = rasterizer.GetPage(100, 3); Bitmap resized = new Bitmap(100, 100); // Create a graphics object from the new bitmap using (Graphics g = Graphics.FromImage(resized)) { // Set the interpolation mode to high quality bicubic g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; // Calculate the scaling ratio double ratioX = (double)resized.Width / (double)pageImage1.Width; double ratioY = (double)resized.Height / (double)pageImage1.Height; double ratio = Math.Min(ratioX, ratioY); // Calculate the new width and height based on the scaling ratio int newWidth = (int)(pageImage1.Width * ratio); int newHeight = (int)(pageImage1.Height * ratio); // Calculate the position of the image on the new bitmap int posX = (resized.Width - newWidth) / 2; int posY = (resized.Height - newHeight) / 2; // Draw the original image onto the new bitmap g.DrawImage(pageImage1, posX, posY, newWidth, newHeight); pageImage1.Save(outfiletn + ".jpeg", ImageFormat.Jpeg); } rasterizer.Close(); }
解决方案
针对三个疑问的具体方案:
- Ghostscript.NET的
pageimage.Save不支持PDF格式的解决pageimage是System.Drawing.Image类型的位图对象,本身仅支持图像格式保存。无需先转位图再生成PDF,直接用Ghostscript提取指定PDF页面更高效,还能保留原文件的矢量信息,避免失真。
示例代码(直接提取前三页为PDF):
var inputPdfPath = "原文件路径"; var outputPdfPath = "输出文件路径"; var gsVersion = GhostscriptVersionInfo.GetLastInstalledVersion(GhostscriptLicense.GPL | GhostscriptLicense.AFPL, GhostscriptLicense.GPL); using (var processor = new GhostscriptProcessor(gsVersion, true)) { List<string> switches = new List<string> { "-dNOPAUSE", "-dBATCH", "-sDEVICE=pdfwrite", $"-sOutputFile={outputPdfPath}", "-dFirstPage=1", "-dLastPage=3", inputPdfPath }; processor.StartProcessing(switches.ToArray(), null); }
- 位图拼接为PDF独立页面的实现
如果必须基于位图生成PDF,需要借助PDF生成库,将每个位图单独作为一页添加。以PdfSharp为例:
using PdfSharp.Drawing; using PdfSharp.Pdf; var document = new PdfDocument(); // 添加第一页 var page1 = document.AddPage(); page1.Width = XUnit.FromPixel(pageImage1.Width); page1.Height = XUnit.FromPixel(pageImage1.Height); using (var gfx = XGraphics.FromPdfPage(page1)) { gfx.DrawImage(XImage.FromGdiPlusImage(pageImage1), 0, 0, page1.Width, page1.Height); } // 重复上述逻辑添加第二、第三页 document.Save("输出PDF路径");
- 是否需要其他库的判断
- 若直接提取原PDF页面,无需额外库,用Ghostscript.NET的
GhostscriptProcessor即可完成,无需转位图。 - 若基于位图生成PDF,则需要PDF生成库,比如
PdfSharp、iTextSharp(注意iTextSharp的许可协议,商业用途需谨慎)。
额外建议
优先选择直接提取PDF页面的方案,转位图再生成PDF会损失原文件的矢量信息,导致文件体积增大、清晰度下降,处理效率也更低。审批环节保留现有位图展示逻辑即可,审批通过后直接提取原PDF对应页面生成新PDF,结果更理想。
内容的提问来源于stack exchange,提问作者Al G
相关产品推荐
相关产品推荐

