How to combine first 3 pages of pdf into a new pdf

  Kiến thức lập trình

I want to extract the first three pages of a pdf, display them for approval, then create a new pdf which has just those approved pages. I am using Ghostscript.net in a c# app that extracts the pages I need into bitmaps and I have the save and approval sections working. However, I am at a loss for the following:

  1. pageimage.Save does not have “pdf” as an image format in Ghostscript.net – how is this done?
  2. How do I “concatenate” or insert the bitmaps so they come out as individual pages in the resulting pdf.
  3. Is there a different library I need to create the resulting pdf?

Your assistance is appreciated.
My extract code is shown attached

 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();

 }

Thanks in advance

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT