To insert/copy/merge several PDF documents into one.
C# C++ Go Java JavaScript Obj-C PHP Python Ruby VB
1 PDFDoc new_doc = new PDFDoc ();
2 int page_num = 15 ;
3 for ( int i = 1 ; i <= page_num; ++ i)
4 {
5 PDFDoc in_doc = new PDFDoc (filename + " _split_page_ " + i + " .pdf " );
6 new_doc. InsertPages (i, in_doc, 1 , in_doc. GetPageCount (), PDFDoc.InsertFlag.e_none);
7 }
8 new_doc. Save (output_filename + " _merge_pages.pdf " , SDFDoc.SaveOptions.e_linearized);
1 PDFDoc new_doc;
2 int page_num = 15 ;
3 for ( int i = 1 ; i <= page_num; ++ i)
4 {
5 char fname[ 64 ];
6 sprintf (fname, " _split_page_ %d .pdf " , i);
7 string input_file (filename + fname);
8 PDFDoc doc (input_file);
9 new_doc. InsertPages (i, doc, 1 , doc. GetPageCount (), PDFDoc :: e_none);
10 }
11 new_doc. Save (output_filename + " _merge_pages.pdf " , SDFDoc :: e_linearized, 0 );
1 newDoc := NewPDFDoc ()
2
3 pageNum = 15
4 for i := 1 ; i <= pageNum; i ++ {
5 doc = NewPDFDoc (outputPath + " _split_page_ " + i + " .pdf " )
6 newDoc. InsertPages (i, doc, 1 , doc. GetPageCount (), PDFDocE_none)
7 doc. Close ()
8 }
9 newDoc. Save (outputPath + " _merge_pages.pdf " , uint (SDFDocE_linearized))
1 PDFDoc new_doc = new PDFDoc ();
2 int page_num = 15 ;
3 for ( int i = 1 ; i <= page_num; ++ i)
4 {
5 PDFDoc doc = new PDFDoc (filename + " _split_page_ " + i + " .pdf " );
6 new_doc. insertPages (i, doc, 1 , doc. getPageCount (), PDFDoc.InsertBookmarkMode.NONE, null );
7 doc. close ();
8 }
9 new_doc. save (output_filename + " _merge_pages.pdf " , SDFDoc.SaveMode.LINEARIZED, null );
1 async function main () {
2 const newDoc = await PDFNet.PDFDoc. create ();
3 const page_num = 15 ;
4 for ( let i = 1 ; i <= page_num; ++ i)
5 {
6 const doc = await PDFNet.PDFDoc. createFromURL (filename + " _split_page_ " + i + " .pdf " );
7 const pageCount = await doc. getPageCount ();
8 newDoc. insertPages (i, doc, 1 , pageCount, PDFNet.PDFDoc.InsertFlag.e_none);
9 }
10 const buf = await newDoc. saveMemoryBuffer (PDFNet.SDFDoc.SaveOptions.e_linearized);
11
12 //optionally save the blob to a file or upload to a server
13 const blob = new Blob ([buf], { type : ' application/pdf ' });
14 }
15 PDFNet. runWithCleanup (main);
1 PTPDFDoc * new_doc = [[PTPDFDoc alloc ] init ];
2 int page_num = 15 ;
3 for ( int i = 1 ; i <= page_num; ++ i)
4 {
5 NSString * input_file = [output_filename stringByAppendingFormat : @" _split_page_ %d .pdf " , i];
6 PTPDFDoc * doc = [[PTPDFDoc alloc ] initWithFilepath : input_file];
7 [new_doc InsertPages : i src_doc : doc start_page : 1 end_page : [doc GetPageCount ] flag : e_ptinsert_none];
8 }
9 [new_doc SaveToFile : [output_filename stringByAppendingString : @" _merge_pages.pdf " ] flags : e_ptlinearized];
1 $new_doc = new PDFDoc ();
2 $page_num = 15 ;
3 for ($i = 1 ; $i <= $page_num; ++ $i)
4 {
5 $doc = new PDFDoc ($filename . " _split_page_ " . $i . " .pdf " );
6 $new_doc -> InsertPages ($i, $doc, 1 , $doc -> GetPageCount (), PDFDoc :: e_none );
7 $doc -> Close ();
8 }
9 $new_doc -> Save ($output_filename . " _merge_pages.pdf " , SDFDoc :: e_linearized );
10 $doc -> Close ();
1 new_doc = PDFDoc()
2 page_num = 15
3 for i in range ( 1 , page_num):
4 doc = PDFDoc(filename + " _split_page_ " + str (i) + " .pdf " )
5 new_doc.InsertPages(i, doc, 1 , doc.GetPageCount(), PDFDoc.e_none)
6 doc.Close()
7 new_doc.Save(output_filename + " _merge_pages.pdf " , SDFDoc.e_linearized)
1 new_doc = PDFDoc . new ()
2 page_num = 15
3 for i in 1 ..page_num do
4 doc = PDFDoc . new (filename + " _split_page_ " + i.to_s + " .pdf " )
5 new_doc. InsertPages (i, doc, 1 , doc. GetPageCount , PDFDoc :: E_none )
6 doc. Close
7 end
8 new_doc. Save (output_filename + " _merge_pages.pdf " , SDFDoc :: E_linearized )
1 Dim new_doc As PDFDoc = New PDFDoc ()
2 Dim page_num As Integer = 15
3 For i = 1 To page_num
4 Dim doc As PDFDoc = New PDFDoc (filename + " _split_page_ " + CStr ( 1 ) + " .pdf " )
5 new_doc. InsertPages (i, doc, 1 , doc. GetPageCount (), PDFDoc.InsertFlag.e_none)
6 doc. Close ()
7 Next i
8 new_doc. Save (output_filename + " _merge_pages.pdf " , SDFDoc.SaveOptions.e_linearized)
Merge, copy, delete, and rearrange PDF pages Full code sample which illustrates how to copy pages from one document to another, how to delete, and rearrange pages and how to use ImportPages().
About page copying/merging The recommended way to copy pages from one document to another is with PDFDoc.InsertPages()
. Its arguments are:
insertBeforeThisPage : An integer specifying where the pages should be insertedsourceDoc : A PDFDoc from which the pages should be readstartPage : An integer specifying the first page number to insertendPage : An integer specifying the last page number to insertflag : A PDFDoc.InsertFlag valueFor example, suppose we want to insert the third page of one document after the first page of a second document. The following code snippet performs this with an insertBeforeThisPage value of 2 and startPage and endPage values of 3.
C# C++ Go Java JavaScript Obj-C PHP Python Ruby VB
1 dest_doc. InsertPages ( 2 , source_doc, 3 , 3 , PDFDoc.InsertFlag.e_none);
1 dest_doc. InsertPages ( 2 , source_doc, 3 , 3 , PDFDoc :: e_none);
1 destDoc. InsertPages ( 3 , sourceDoc, 3 , 3 , PDFDocE_none)
1 dest_doc. insertPages ( 2 , source_doc, 3 , 3 , PDFDoc.InsertBookmarkMode.NONE, null );
1 dest_doc. insertPages ( 2 , source_doc, 3 , 3 , PDFNet.PDFDoc.InsertFlag.e_none);
1 [dest_doc InsertPages : 2 src_doc : source_doc start_page : 3 end_page : 3 flag : e_ptinsert_none];
1 $dest_doc -> InsertPages ( 2 , $source_doc, 3 , 3 , PDFDoc :: e_none );
1 dest_doc.InsertPages( 2 , source_doc, 3 , 3 , PDFDoc.e_none)
1 dest_doc. InsertPages ( 2 , source_doc, 3 , 3 , PDFDoc :: E_none )
1 dest_doc. InsertPages ( 2 , source_doc, 3 , 3 , PDFDoc.InsertFlag.e_none)
We can also insert a range of pages. For example, the following code will insert the second, third, and fourth pages of one document into the end of the second document. We specify that we're inserting into the end of the document by using an insertBeforeThisPage value higher than the number of pages in the document:
C# C++ Go Java JavaScript Obj-C PHP Python Ruby VB
1 dest_doc. InsertPages (dest_doc. GetPageCount () + 1 , source_doc, 2 , 4 , PDFDoc.InsertFlag.e_none);
1 dest_doc. InsertPages (dest_doc. GetPageCount () + 1 , source_doc, 2 , 4 , PDFDoc :: e_none);
1 destDoc. InsertPages (destDoc. GetPageCount () + 1 , sourceDoc, 2 , 4 , PDFDocE_none)
1 dest_doc. insertPages (dest_doc. getPageCount () + 1 , source_doc, 2 , 4 , PDFDoc.InsertBookmarkMode.NONE, null );
1 dest_doc. insertPages (dest_doc. getPageCount () + 1 , source_doc, 2 , 4 , PDFNet.PDFDoc.InsertFlag.e_none);
1 [dest_doc InsertPages : [dest_doc GetPageCount ] + 1 src_doc : source_doc start_page : 2 end_page : 4 flag : e_ptinsert_none];
1 $dest_doc -> InsertPages ($dest_doc -> GetPageCount () + 1 , $source_doc, 2 , 4 , PDFDoc :: e_none );
1 dest_doc.InsertPages(dest_doc.GetPageCount() + 1 , source_doc, 2 , 4 , PDFDoc.e_none)
1 dest_doc. InsertPages (dest_doc. GetPageCount () + 1 , source_doc, 2 , 4 , PDFDoc :: E_none )
1 dest_doc. InsertPages (dest_doc. GetPageCount () + 1 , source_doc, 2 , 4 , PDFDoc.InsertFlag.e_none)