Я хочу извлечь первые три страницы PDF-файла, отобразить их на утверждение, а затем создать новый PDF-файл, содержащий только эти утвержденные страницы. Я использую Ghostscript.net в приложении C#, которое извлекает нужные мне страницы в растровые изображения, и у меня работают разделы сохранения и утверждения. Однако я не понимаю следующего:
pageimage.Save не имеет формата изображения «pdf» в Ghostscript.net – как это готово?
Как «объединить» или вставить растровые изображения, чтобы они отображались как отдельные страницы в полученном 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();
}
Я хочу извлечь первые три страницы PDF-файла, отобразить их на утверждение, а затем создать новый PDF-файл, содержащий только эти утвержденные страницы. Я использую Ghostscript.net в приложении C#, которое извлекает нужные мне страницы в растровые изображения, и у меня работают разделы сохранения и утверждения. Однако я не понимаю следующего: [list] [*]pageimage.Save не имеет формата изображения «pdf» в Ghostscript.net – как это готово? [*]Как «объединить» или вставить растровые изображения, чтобы они отображались как отдельные страницы в полученном PDF-файле. [*]Есть ли другой вариант? мне нужна библиотека для создания результирующего PDF-файла? [/list] Ваша помощь оценена по достоинству. Мой код извлечения показан во вложении [code] 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())
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); }