In This Topic
    Merge specific pages together
    In This Topic

    Introduction

    The following example demonstrates how to merge specific pages from a PDF document into another PDF document.

    Merge specific pages from a PDF document
    Copy Code
    public static void MergeSpecificPagesInDocument()
    {
      Console.WriteLine( "=== MERGE SPECIFIC PAGES IN DOCUMENT===" );
    
      var file1 = "Tech_Companies.pdf";
      var file2 = "Animals.pdf";
      var outputFileName = "Merged_Specific_Pages.pdf";
      var outputPath = MergeSample.MergeOutputDirectory + outputFileName;
    
      // Load file1 and file2.
      using( var doc1 = PdfDocument.Load( MergeSample.MergeSampleResourceDirectory + file1 ) )
      {
        using( var doc2 = PdfDocument.Load( MergeSample.MergeSampleResourceDirectory + file2 ) )
        {
          // Make sure doc2 has at least 3 pages.
          if( doc2.Pages.Count > 3 )
          {
            // Merge page0 from doc2 at PageIndex 1 in doc1.
            doc1.Merge( new MergeOptions( doc2, 0, 0 ) { PageIndex = 1 } );
            // Merge page2 to page3 from doc2 at PageIndex 2 in doc1.
            doc1.Merge( new MergeOptions( doc2, 2, 3 ) { PageIndex = 2 } );
          }
          else
          {
            Console.WriteLine( "Warning: doc2 does not have enough pages to merge." );
          }
    
          // Save the output document.
          doc1.SaveAs( outputPath );
          Console.WriteLine( "Merged specific pages saved to: " + outputPath );
        }
      }
    }

    See Also