Add a page to a PDF or a TIFF image in C# .NET
Nutrient .NET SDK (formerly GdPicture.NET) enables you to add pages to files that support pagination, such as PDFs and TIFF files.
Download sampleAdding a blank page to a PDF
To add a blank page to a PDF, use the InsertPage method. This method requires the page size and the page location where it’ll be inserted. The page size can be defined in one of the following ways:
- The page width and height in the current units, which are points by default.
- The
PdfPageSizesenumeration with predefined values.
To add a square blank page of 500×500 points to the beginning and an A5 blank page to the end of a PDF file, use the following code:
Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.
using GdPicture14;using GdPicture14.Imaging;
LicenseManager license = new LicenseManager();license.RegisterKEY("");{using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"input.pdf");// Insert a square blank page at the beginning of the PDF.gdpicturePDF.InsertPage(PageWidth: 500, PageHeight: 500, PageNo: 1);// Insert an A5 blank page at the end of the PDF.gdpicturePDF.InsertPage(PdfPageSizes.PdfPageSizeA5, gdpicturePDF.GetPageCount() + 1);gdpicturePDF.SaveToFile(@"output.pdf");}Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.
Imports GdPicture14Imports GdPicture14.Imaging
Dim license As LicenseManager = New LicenseManager()license.RegisterKEY("")Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("input.pdf") ' Insert a square blank page at the beginning of the PDF. gdpicturePDF.InsertPage(PageWidth:=500, PageHeight:=500, PageNo:=1) ' Insert an A5 blank page at the end of the PDF. gdpicturePDF.InsertPage(PdfPageSizes.PdfPageSizeA5, gdpicturePDF.GetPageCount() + 1) gdpicturePDF.SaveToFile("output.pdf")End UsingAdding an existing page to a PDF
To add an existing page to the currently loaded PDF, use the ClonePage method. This method adds the copied page to the end of the file. For more information, refer to the copying a PDF page section of the guide on moving and copying pages.
To place the copied page to a specific location after using the ClonePage method, use the MovePage method to specify the final location of the copied page. For more information, refer to the section on copying a PDF page to a specified location in the guide on moving and copying pages.
Adding a blank page to a TIFF
To add a blank page to a TIFF file, follow the steps outlined below.
- Create a
GdPictureImagingobject. - Create an empty GdPicture image by using the
CreateNewGdPictureImagemethod. - Add the empty GdPicture image to the TIFF file in one of the following ways:
- Append the image to the end of the TIFF file with the
TiffAppendPageFromFilemethod or theTiffAppendPageFromGdPictureImagemethod. - Insert the image at the desired location with the
TiffInsertPageFromFilemethod or theTiffInsertPageFromGdPictureImagemethod.
- Append the image to the end of the TIFF file with the
To insert an empty image into the second-to-last position of a TIFF image, use the following code:
{using GdPictureImaging gdpictureImaging = new GdPictureImaging();GdPictureColor backColor = GdPictureColor.White;int imageID = gdpictureImaging.TiffCreateMultiPageFromFile(@"multipage-tiff.tif");// Create an empty image.int imageBlankID = gdpictureImaging.CreateNewGdPictureImage(Width: 500, Height: 500, BitDepth: 32, BackColor: backColor);// Insert the empty image into the second-to-last position of the TIFF image.gdpictureImaging.TiffInsertPageFromGdPictureImage(imageID, gdpictureImaging.GetPageCount(imageID), imageBlankID);gdpictureImaging.TiffSaveMultiPageToFile(imageID, @"output.tif", TiffCompression.TiffCompressionAUTO);gdpictureImaging.ReleaseGdPictureImage(imageBlankID);gdpictureImaging.ReleaseGdPictureImage(imageID);}Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim backColor As GdPictureColor = GdPictureColor.White Dim imageID As Integer = gdpictureImaging.TiffCreateMultiPageFromFile("multipage-tiff.tif") ' Create an empty image. Dim imageBlankID As Integer = gdpictureImaging.CreateNewGdPictureImage(Width:=500, Height:=500, BitDepth:=32, BackColor:=backColor) ' Insert the empty image into the second-to-last position of the TIFF image. gdpictureImaging.TiffInsertPageFromGdPictureImage(imageID, gdpictureImaging.GetPageCount(imageID), imageBlankID) gdpictureImaging.TiffSaveMultiPageToFile(imageID, "output.tif", TiffCompression.TiffCompressionAUTO) gdpictureImaging.ReleaseGdPictureImage(imageBlankID) gdpictureImaging.ReleaseGdPictureImage(imageID)End UsingThe GetPageCount method runs before the page is inserted, so it returns the original number of pages. Inserting the new page at that position moves the original last page one position down, and the new page becomes the second-to-last page.
Creating an empty image
The CreateNewGdPictureImage method enables you to create an empty image. It requires the following parameters:
Width— Sets the image width in pixels.Height— Sets the image height in pixels.BitDepthorPixelFormat— Sets the image bit depth or thePixelFormatenumeration defining the pixel format.BackColor— Sets the image background color defined by either aGdPictureColorobject or using theARGBmethod.
Use the following code to create an empty image:
{using GdPictureImaging gdpictureImaging = new GdPictureImaging();GdPictureColor backColor = GdPictureColor.White;// Create an empty image with a white background and the size of 500×500 pixels.int imageID = gdpictureImaging.CreateNewGdPictureImage(Width: 500, Height: 500, BitDepth: 32, BackColor: backColor);gdpictureImaging.SaveAsJPEG(imageID, @"output.jpg");gdpictureImaging.ReleaseGdPictureImage(imageID);}Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim backColor As GdPictureColor = GdPictureColor.White ' Create an empty image with a white background and the size of 500×500 pixels. Dim imageID As Integer = gdpictureImaging.CreateNewGdPictureImage(Width:=500, Height:=500, BitDepth:=32, BackColor:=backColor) gdpictureImaging.SaveAsJPEG(imageID, "output.jpg") gdpictureImaging.ReleaseGdPictureImage(imageID)End UsingAdding an existing page to a TIFF
To add an existing page to a TIFF image, follow the steps outlined below.
- Create a
GdPictureImagingobject. - Extract the page you want to add with the
TiffExtractPagemethod. - Add the extracted page to the TIFF image in one of the following ways:
- Append the image to the end of the TIFF file with the
TiffAppendPageFromFilemethod or theTiffAppendPageFromGdPictureImagemethod. - Insert the image at the desired location with the
TiffInsertPageFromFilemethod or theTiffInsertPageFromGdPictureImagemethod.
- Append the image to the end of the TIFF file with the
{using GdPictureImaging gdpictureImaging = new GdPictureImaging();int tiffID = gdpictureImaging.TiffCreateMultiPageFromFile("multipage-tiff.tif");// Extract the last TIFF page to a temp.tif file.gdpictureImaging.TiffExtractPage(tiffID, gdpictureImaging.GetPageCount(tiffID), @"temp.tif");// Append the extracted page to the end of the TIFF image.gdpictureImaging.TiffAppendPageFromFile(tiffID, @"temp.tif");gdpictureImaging.TiffSaveMultiPageToFile(tiffID, @"output.tif", TiffCompression.TiffCompressionAUTO);gdpictureImaging.ReleaseGdPictureImage(tiffID);}Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim tiffID As Integer = gdpictureImaging.TiffCreateMultiPageFromFile("multipage-tiff.tif") ' Extract the last TIFF page to a temp.tif file. gdpictureImaging.TiffExtractPage(tiffID, gdpictureImaging.GetPageCount(tiffID), "temp.tif") ' Append the extracted page to the end of the TIFF image. gdpictureImaging.TiffAppendPageFromFile(tiffID, "temp.tif") gdpictureImaging.TiffSaveMultiPageToFile(tiffID, "output.tif", TiffCompression.TiffCompressionAUTO) gdpictureImaging.ReleaseGdPictureImage(tiffID)End Using