Convert to JPG #9

Open
opened 2025-06-18 14:06:50 -04:00 by efrick · 1 comment
Owner

Add the ability to convert the PDFs to an image after merging.

Add the ability to convert the PDFs to an image after merging.
efrick added the
Kind/Feature
Priority
Medium
labels 2025-06-18 14:06:50 -04:00
Author
Owner

Example code using PDFsharp.

using System.Drawing.Imaging;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;

// Load the PDF document
PdfDocument document = PdfReader.Open("input.pdf", PdfDocumentOpenMode.Import);

// Loop through each page in the document
for (int pageIndex = 0; pageIndex < document.Pages.Count; pageIndex++)
{
    // Get the current page
    PdfPage page = document.Pages[pageIndex];

    // Create a new XGraphics object to render the page
    XGraphics gfx = XGraphics.FromPdfPage(page);

    // Create a new image with the same dimensions as the page
    int width = (int)page.Width;
    int height = (int)page.Height;
    System.Drawing.Image image = new System.Drawing.Bitmap(width, height);

    // Render the page to the image
    using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image))
    {
        g.DrawImage(gfx.ToImage(), 0, 0, width, height);
    }

    // Save the image to a file
    image.Save($"output_{pageIndex}.png", ImageFormat.Png);
}
Example code using PDFsharp. ```C# using System.Drawing.Imaging; using PdfSharp.Drawing; using PdfSharp.Pdf; using PdfSharp.Pdf.IO; // Load the PDF document PdfDocument document = PdfReader.Open("input.pdf", PdfDocumentOpenMode.Import); // Loop through each page in the document for (int pageIndex = 0; pageIndex < document.Pages.Count; pageIndex++) { // Get the current page PdfPage page = document.Pages[pageIndex]; // Create a new XGraphics object to render the page XGraphics gfx = XGraphics.FromPdfPage(page); // Create a new image with the same dimensions as the page int width = (int)page.Width; int height = (int)page.Height; System.Drawing.Image image = new System.Drawing.Bitmap(width, height); // Render the page to the image using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image)) { g.DrawImage(gfx.ToImage(), 0, 0, width, height); } // Save the image to a file image.Save($"output_{pageIndex}.png", ImageFormat.Png); } ```
Sign in to join this conversation.
No description provided.