←Select platform

InsertPagesFrom Method

Summary
Inserts one or more pages from an existing PDF file into the file associated with this PDFFile object.
Syntax
C#
C++/CLI
Java
Python
public void InsertPagesFrom( 
   int insertPageNumber, 
   PDFFile sourceFile, 
   int firstPageNumber, 
   int lastPageNumber 
) 
public void insertPagesFrom( 
   int insertPageNumber, 
   PDFFile sourceFile, 
   int firstPageNumber, 
   int lastPageNumber 
); 
public: 
void InsertPagesFrom(  
   int insertPageNumber, 
   PDFFile^ sourceFile, 
   int firstPageNumber, 
   int lastPageNumber 
)  

Parameters

insertPageNumber
The 1-based number of the insertion point in this PDFFile object. Use a value of 0 to insert the new pages at the beginning of the file and -1 to insert the new pages at the end of the file (append).

sourceFile
A PDFFile object associated with an existing PDF file that contain the source file to insert the pages from.

firstPageNumber
The 1-based number of the first page in the source file to be inserted. Must be a value greater than or equal to 1.

lastPageNumber
The 1-based number of the last page in the source file to be inserted. Must be a value greater than or equal to  firstPageNumber and less than or equal to the total number of pages in the file. Use the special value of -1 to represent "last page in the file".

Remarks

Both files in this PDFFile (the destination) and  sourceFile must exist and contain valid PDF files prior to calling this method.

To use this method, associate this PDFFile object and the source PDFFile with a valid PDF file and optional password. You can achieve this by either using the PDFFile(string fileName) or PDFFile(string fileName, string password) constructors or set the filename and optional password directly into the FileName and Password properties. You do not need to call Load before using this method.

This method will use the following properties of this PDFFile object:

  • DocumentProperties. If the value of this property is null, then default properties will be used

  • SecurityOptions. If the value of this property is not null, then the destination file will be encrypted using the properties of this property. If the value of this property is null, the result file will not be encrypted

  • CompatibilityLevel. The version of the generated PDF file

Example

This example will insert pages from an existing PDF file to a different one in various locations.

C#
Java
using Leadtools.WinForms; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Controls; 
using Leadtools.Drawing; 
using Leadtools.ImageProcessing; 
using Leadtools.Pdf; 
using Leadtools.Svg; 
 
 
public void PDFFileInsertPagesFromExample() 
{ 
   string originalFileName = Path.Combine(LEAD_VARS.ImagesDir, @"Leadtools.pdf"); 
   string sourceFileName = Path.Combine(LEAD_VARS.ImagesDir, @"PDFSegmentation.pdf"); 
 
   string destinationFileName1 = Path.Combine(LEAD_VARS.ImagesDir, @"InsertAtBeginning.pdf"); 
   string destinationFileName2 = Path.Combine(LEAD_VARS.ImagesDir, @"InsertInMiddle.pdf"); 
   string destinationFileName3 = Path.Combine(LEAD_VARS.ImagesDir, @"InsertAtEnd.pdf"); 
 
   // Make a copy of the original file 
   File.Copy(originalFileName, destinationFileName1, true); 
   File.SetAttributes(destinationFileName1, FileAttributes.Normal); 
   File.Copy(originalFileName, destinationFileName2, true); 
   File.SetAttributes(destinationFileName2, FileAttributes.Normal); 
   File.Copy(originalFileName, destinationFileName3, true); 
   File.SetAttributes(destinationFileName3, FileAttributes.Normal); 
 
   // This is the source PDF file to insert into the destination 
   PDFFile sourceFile = new PDFFile(sourceFileName); 
 
   // Insert sourceFileName2 into destinationFileName1 at the beginning 
   PDFFile file = new PDFFile(destinationFileName1); 
   file.InsertPagesFrom(0, sourceFile, 1, -1); 
 
   // Insert sourceFileName2 into destinationFileName2 in the middle 
   file = new PDFFile(destinationFileName2); 
   file.InsertPagesFrom(file.GetPageCount() / 2, sourceFile, 1, -1); 
 
   // Insert sourceFileName2 into destinationFileName3 at the end (append) 
   file = new PDFFile(destinationFileName3); 
   file.InsertPagesFrom(-1, sourceFile, 1, -1); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.ByteArrayInputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.nio.file.StandardCopyOption; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 
import java.util.Scanner; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.pdf.*; 
 
 
public void pdfFileInsertPagesFromExample() throws IOException { 
 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   String originalFileName = combine(LEAD_VARS_IMAGES_DIR, "Leadtools.pdf"); 
   String sourceFileName = combine(LEAD_VARS_IMAGES_DIR, "PDFSegmentation.pdf"); 
   String destinationFileName1 = combine(LEAD_VARS_IMAGES_DIR, "InsertAtBeginning.pdf"); 
   String destinationFileName2 = combine(LEAD_VARS_IMAGES_DIR, "InsertInMiddle.pdf"); 
   String destinationFileName3 = combine(LEAD_VARS_IMAGES_DIR, "InsertAtEnd.pdf"); 
 
   // Make a copy of the original file 
   Files.copy(Paths.get(originalFileName), Paths.get(destinationFileName1), StandardCopyOption.REPLACE_EXISTING); 
   Files.copy(Paths.get(originalFileName), Paths.get(destinationFileName2), StandardCopyOption.REPLACE_EXISTING); 
   Files.copy(Paths.get(originalFileName), Paths.get(destinationFileName3), StandardCopyOption.REPLACE_EXISTING); 
 
   // This is the source PDF file to insert into the destination 
   PDFFile sourceFile = new PDFFile(sourceFileName); 
 
   // Insert sourceFileName2 into destinationFileName1 at the beginning 
   PDFFile file = new PDFFile(destinationFileName1); 
   file.insertPagesFrom(0, sourceFile, 1, -1); 
 
   // Insert sourceFileName2 into destinationFileName2 in the middle 
   file = new PDFFile(destinationFileName2); 
   file.insertPagesFrom(file.getPageCount() / 2, sourceFile, 1, -1); 
 
   // Insert sourceFileName2 into destinationFileName3 at the end (append) 
   file = new PDFFile(destinationFileName3); 
   file.insertPagesFrom(-1, sourceFile, 1, -1); 
 
   assertTrue("Check that each destination file increased in size by one", 
         (new PDFFile(destinationFileName1).getPageCount() == 6 
               && new PDFFile(destinationFileName2).getPageCount() == 6 
               && new PDFFile(destinationFileName1).getPageCount() == 6)); 
 
} 
Requirements

Target Platforms

See Also

Reference

PDFFile Class

PDFFile Members

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Pdf Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.