IronPdf – How to add a blank page with an image

Forums .NETIronPdf – How to add a blank page with an image
Staff asked 2 years ago

Answers (1)

Add Answer
krishna kukadiya Marked As Accepted
Staff answered 2 years ago

To add a blank page with an image using IronPDF, you can follow these steps:

  1. Install IronPDF: If you haven’t already, install the IronPDF package in your project using NuGet. You can do this by right-clicking on your project, selecting “Manage NuGet Packages,” searching for “IronPDF,” and installing the package.
  2. Import necessary namespaces: In the code file where you’ll be working, import the required namespaces for IronPDF:

using IronPdf;
using IronPdf.Imaging;
  1. Create a new PDF document: Initialize a new PdfDocument object.

PdfDocument pdf = new PdfDocument();

  1. Add a blank page: Use the AddPage() method to add a new blank page to the PDF document.

PdfPage page = pdf.AddPage();

  1. Add an image to the blank page: Load an image file and add it to the blank page using the AddImage() method. You can specify the image file path and the position on the page where the image should be placed.

string imagePath = "path/to/your/image.jpg";
PdfImage image = PdfImage.FromFile(imagePath);
page.AddImage(image, 50, 50); // Adjust the position as needed

  1. Save the PDF document: Save the PDF document to a file or a stream using the SaveAs() method.

string outputPath = "path/to/save/output.pdf";
pdf.SaveAs(outputPath);

Here’s the complete example:

using IronPdf;
using IronPdf.Imaging;

public class PdfGenerator
{
    public void AddBlankPageWithImage()
    {
        PdfDocument pdf = new PdfDocument();
        PdfPage page = pdf.AddPage();

        string imagePath = "path/to/your/image.jpg";
        PdfImage image = PdfImage.FromFile(imagePath);
        page.AddImage(image, 50, 50); // Adjust the position as needed

        string outputPath = "path/to/save/output.pdf";
        pdf.SaveAs(outputPath);
    }
}
Make sure to replace "path/to/your/image.jpg" with the actual path to your image file and "path/to/save/output.pdf" with the desired path and filename for the generated PDF file.

That's it! Running this code will create a new PDF document with a blank page and an image placed on it.






 

Make sure to replace "path/to/your/image.jpg" with the actual path to your image file and "path/to/save/output.pdf" with the desired path and filename for the generated PDF file.

That’s it! Running this code will create a new PDF document with a blank page and an image placed on it.

Subscribe

Select Categories