Free Support Forum - aspose.com

Merge PDF Documents in C#.NET using Aspose.PDF

Hi,
I am generating 2 different PDFs(file1.pdf and file2.pdf) with 4 and 3 pages respectively using my .NET application. Now i want to add both PDFs in a blank PDF document(Final.pdf). But Page number 2 of file1.pdf and file2.pdf should not be added in Final.pdf.
Is there any way to add pdfs in a blank PDF document?
Thanks
Pallavi.

@spallavi1

Thanks for contacting support.

You may please put a check over page number before adding it to the PageCollection of final document. Please check following code snippet to achieve your requirement:

Merge PDF Documents in C#.NET

Document document1 = new Document(dataDir + "file1.pdf");
Document document2 = new Document(dataDir + "file2.pdf");
Document finalDocument = new Document();
foreach(Page page in document1.Pages)
{
    if(page.Number != 2)
    {
      finalDocument.Pages.Add(page);
    }
}
foreach (Page page in document2.Pages)
{
  if (page.Number != 2)
  {
    finalDocument.Pages.Add(page);
  }
}
finalDocument.Save(dataDir + "finalDoc.pdf");

In case of any further assistance, please feel free to let us know.

Thanks It solve my problem. :slightly_smiling_face: