Move or copy PDF pages and TIFF images in C#

Nutrient .NET SDK (formerly GdPicture.NET) enables you to move and copy pages in PDF and TIFF files.

Moving a PDF page

To move a single PDF page, use the MovePage method. Set the page number of the page you want to move (PageNo) and its destination (Destination). Use the GetPageCount method to get the last page number.

To move the last page of a PDF to the second page, use the following code:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Move the last page to the second page.
gdpicturePDF.MovePage(PageNo: gdpicturePDF.GetPageCount(), Destination: 2);
gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");

Copying a PDF page

To copy a single PDF page, use the ClonePage method. Specify the page you want to copy (PageNo). The copied page is always added to the end of the file. If you have multiple GdPicturePDF objects declared, specify the FromPDF parameter to select the PDF where you want to perform the copy operation.

You can reference the same PDF file in the FromPDF parameter.

To copy the second page of a PDF to the end of a file, use the following code:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Copy the second page.
gdpicturePDF.ClonePage(FromPDF: gdpicturePDF, PageNo: 2);
gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");

Copying a PDF page to a specified location

To copy a single PDF page to a specific location of a file, follow these steps:

  1. Copy the PDF page.
  2. Move the copied PDF page to the desired location.

To copy the third page of a PDF and move it to the first position, use the following code:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Copy the third page.
gdpicturePDF.ClonePage(gdpicturePDF, 3);
// Move the copied page to the first page.
gdpicturePDF.MovePage(gdpicturePDF.GetPageCount(), 1);
gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");

Copying multiple PDF pages

To copy multiple PDF pages, use the ClonePages method. Specify the PDF file you want to copy pages from (FromPDF) and the pages you want to copy (PageRange). To specify multiple pages or page ranges, separate each declaration with a semicolon. To copy all pages of a PDF, use the "*" syntax.

To copy the second page of a PDF to the end of a file, use the following code:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
using GdPicturePDF gdpictureDestPDF = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
gdpictureDestPDF.NewPDF();
for (int i = 0; i < 3; i++)
{
gdpictureDestPDF.ClonePages(FromPDF: gdpicturePDF, PageRange: "2; 4-5");
}
gdpictureDestPDF.SaveToFile(@"C:\temp\output.pdf");

Moving a TIFF page

To move a TIFF page, use the TiffMovePage method. Set the image ID, the page number of the page you want to move, and its destination.

The following methods are used only for editable, multipage TIFF files. To check if a file meets the requirements, use the TiffIsEditableMultiPage method. It returns a true value if a file is editable and multipage.

To move the second-to-last page of a PDF to the first page, use the following code:

using GdPictureImaging gdpictureImaging = new GdPictureImaging();
int imageID = gdpictureImaging.TiffCreateMultiPageFromFile(@"C:\temp\source.tif");
// Move the second-to-last page to the first page.
gdpictureImaging.TiffMovePage(imageID, gdpictureImaging.GetPageCount(imageID) - 1, 1);
gdpictureImaging.TiffSaveMultiPageToFile(imageID, @"C:\temp\output.tif",
TiffCompression.TiffCompressionAUTO);
gdpictureImaging.ReleaseGdPictureImage(imageID);

Copying a TIFF page

To copy a TIFF page, you have to extract the desired page from the TIFF file and either save it to a file or as a GdPicture image. After that, either:

The following methods are used only for editable, multipage TIFF files. To check if a file meets the requirements, use the TiffIsEditableMultiPage method. It returns a true value if a file is editable and multipage.

Copying a TIFF page to the end of a file

To copy the last page of a TIFF image to the end of a file, which is done by saving it to a file first, use the following code:

using GdPictureImaging gdpictureImaging = new GdPictureImaging();
int tiffID = gdpictureImaging.TiffCreateMultiPageFromFile("C:\temp\source.tif");
// Extract the last TIFF page to a temp.jpg file.
gdpictureImaging.TiffExtractPage(tiffID, gdpictureImaging.GetPageCount(tiffID),
@"C:\temp\temp.jpg");
// Open a GdPicture image from the temp.jpg file.
int imageTempID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\temp.jpg");
// Append the GdPicture image to a TIFF file.
gdpictureImaging.TiffAppendPageFromFile(tiffID, @"C:\temp\temp.jpg");
gdpictureImaging.TiffSaveMultiPageToFile(tiffID, @"C:\temp\output.tif",
TiffCompression.TiffCompressionAUTO);
gdpictureImaging.ReleaseGdPictureImage(imageTempID);

Copying a TIFF page to a desired location

To copy the last page of a TIFF image and insert it to the first position of a file, which is done by saving it to a stream, use the following code:

using GdPictureImaging gdpictureImaging = new GdPictureImaging();
int imageID = gdpictureImaging.TiffCreateMultiPageFromFile(@"C:\temp\source.tif");
Stream pStream = new MemoryStream();
// Save the last page to a stream.
gdpictureImaging.TiffExtractPage(imageID, gdpictureImaging.GetPageCount(imageID), pStream);
// Create a new `imageID` from the stream.
int imageTempID = gdpictureImaging.CreateGdPictureImageFromStream(pStream);
// Add the `imageID` to the TIFF file and place it in the first position.
gdpictureImaging.TiffInsertPageFromGdPictureImage(imageID, 1, imageTempID);
gdpictureImaging.TiffSaveMultiPageToFile(imageID, @"C:\temp\output.tif",
TiffCompression.TiffCompressionAUTO);
gdpictureImaging.ReleaseGdPictureImage(imageTempID);

Copying an image to the clipboard

To copy an image or part of it to the clipboard, use the CopyToClipboard method or the CopyRegionToClipboard method. You can save the copied area to a new GdPicture image and perform any other operation.

When using the CopyRegionToClipboard method, specify the part of the image you want to copy with the following parameters:

  • SrcLeft is the X coordinate of the top-left corner.
  • SrcTop is the Y coordinate of the top-left corner.
  • Width is the area width.
  • Height is the area height.

The origin of the coordinate system is the top-left corner of the original image.

To copy an image to the clipboard and save it as a new image, use the following code:

using GdPictureImaging gdpictureImaging = new GdPictureImaging();
int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");
// Copy the image to the clipboard.
gdpictureImaging.CopyToClipboard(imageID);
int imageDestID = gdpictureImaging.CreateGdPictureImageFromClipboard();
gdpictureImaging.SaveAsJPEG(imageDestID, @"C:\temp\output.jpg");
gdpictureImaging.ReleaseGdPictureImage(imageID);
gdpictureImaging.ReleaseGdPictureImage(imageDestID);

To copy the left side of an image, blur it, and save the output to a new image, use the following code:

using GdPictureImaging gdpictureImaging = new GdPictureImaging();
int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");
// Copy the left side of an image to the clipboard.
gdpictureImaging.CopyRegionToClipboard(imageID, 0, 0,
gdpictureImaging.GetWidth(imageID) / 2, gdpictureImaging.GetHeight(imageID));
int imageDestID = gdpictureImaging.CreateGdPictureImageFromClipboard();
gdpictureImaging.FxBlur(imageDestID);
gdpictureImaging.SaveAsJPEG(imageDestID, @"C:\temp\output.jpg");
gdpictureImaging.ReleaseGdPictureImage(imageID);
gdpictureImaging.ReleaseGdPictureImage(imageDestID);