Move or Copy PDF Pages and TIFF Images in C#
PSPDFKit GdPicture.NET Library allows 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");
Using gdpicturePDF As 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") End Using
Used Methods
Related Topics
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");
Using gdpicturePDF As 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") End Using
Used Methods
Copying a PDF Page to a Specified Location
To copy a single PDF page to a specific location of a file, follow these steps:
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");
Using gdpicturePDF As 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") End Using
Used Methods
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");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() Using gdpictureDestPDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") gdpictureDestPDF.NewPDF() For i = 0 To 2 gdpictureDestPDF.ClonePages(FromPDF:=gdpicturePDF, PageRange:="2; 4-5") Next gdpictureDestPDF.SaveToFile("C:\temp\output.pdf") End Using End Using
Used Methods
Related Topics
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 atrue
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);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = 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) End Using
Used Methods
Related Topics
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:
-
Append the copied image to the end of the file with the
TiffAppendPageFromFile
method or theTiffAppendPageFromGdPictureImage
method. -
Place the copied image at the desired location with the
TiffInsertPageFromFile
method or theTiffInsertPageFromGdPictureImage
method.
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 atrue
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);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim tiffID As Integer = 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. Dim imageTempID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\temp.jpg") ' Append the GdPicture image to the TIFF file. gdpictureImaging.TiffAppendPageFromFile(tiffID, "C:\temp\temp.jpg") gdpictureImaging.TiffSaveMultiPageToFile(tiffID, "C:\temp\output.tif", TiffCompression.TiffCompressionAUTO) gdpictureImaging.ReleaseGdPictureImage(imageTempID) End Using
Used Methods
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);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.TiffCreateMultiPageFromFile("C:\temp\source.tif") Dim pStream As Stream = New MemoryStream() ' Save the last page to a stream. gdpictureImaging.TiffExtractPage(imageID, gdpictureImaging.GetPageCount(imageID), pStream) ' Create a new `imageID` from the stream. Dim imageTempID As Integer = 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) End Using
Used Methods
Related Topics
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);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Copy the image to the clipboard. gdpictureImaging.CopyToClipboard(imageID) Dim imageDestID As Integer = gdpictureImaging.CreateGdPictureImageFromClipboard() gdpictureImaging.SaveAsJPEG(imageDestID, "C:\temp\output.jpg") gdpictureImaging.ReleaseGdPictureImage(imageID) gdpictureImaging.ReleaseGdPictureImage(imageDestID) End Using
Used Methods
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);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = 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)) Dim imageDestID As Integer = gdpictureImaging.CreateGdPictureImageFromClipboard() gdpictureImaging.FxBlur(imageDestID) gdpictureImaging.SaveAsJPEG(imageDestID, "C:\temp\output.jpg") gdpictureImaging.ReleaseGdPictureImage(imageID) gdpictureImaging.ReleaseGdPictureImage(imageDestID) End Using