---
title: "Merge alternating pages in two PDFs in C#"
canonical_url: "https://www.nutrient.io/guides/dotnet/samples/merge-pdf-alternate-pages-cs/"
md_url: "https://www.nutrient.io/guides/dotnet/samples/merge-pdf-alternate-pages-cs.md"
last_updated: "2026-05-21T17:12:02.215Z"
description: "Learn to merge two PDF files by alternating pages using C#. This method is ideal for batch scanning without a duplex scanner option."
---

# Merging two PDF documents by alternating pages from both files in C#

This example shows how to merge two PDF documents, alternating a page from the first document and a page from the second document. This can be useful when you don’t have a duplex option on your scanner and want to scan a batch of documents, so you can acquire the frontsides first and then the backsides.

[Get Started](https://www.nutrient.io/sdk/dotnet/getting-started.md)

[All Samples](https://www.nutrient.io/guides/dotnet/samples.md)

[Download](https://www.nutrient.io/guides/dotnet/downloads.md)

[Launch Demo](https://www.nutrient.io/demo/)

---

### C#

[C#](https://www.nutrient.io/guides/dotnet/samples/merge-pdf-alternate-pages-cs.md)

### VB.NET

[VB.NET](https://www.nutrient.io/guides/dotnet/samples/merge-pdf-alternate-pages-vbnet.md)

```cs

//We assume that GdPicture has been correctly installed and unlocked.
GdPicturePDF PdfSource1 = new GdPicturePDF();
GdPicturePDF PdfSource2 = new GdPicturePDF();
GdPicturePDF PdfDest = new GdPicturePDF();
GdPictureStatus status = PdfSource1.LoadFromFile("input1.pdf", false);
if (status!= GdPictureStatus.OK)
{
   MessageBox.Show("The first PDF document can't be loaded. Error: " + status.ToString(), "Merging PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
   goto end;
}
status = PdfSource2.LoadFromFile("input2.pdf", false);
if (status!= GdPictureStatus.OK)
{
   MessageBox.Show("The second PDF document can't be loaded. Error: " + status.ToString(), "Merging PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
   goto end;
}
status = PdfDest.NewPDF();
if (status!= GdPictureStatus.OK)
{
   MessageBox.Show("The destination PDF document can't be created. Error: " + status.ToString(), "Merging PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
   goto end;
}
status = GdPictureStatus.OK;
int PageCountSource1 = PdfSource1.GetPageCount();
int PageCountSource2 = PdfSource1.GetPageCount();
int MaxPage = Math.Max(PageCountSource1, PageCountSource2);
for (int Page = 1; Page <= MaxPage; Page++)
{
   if (Page <= PageCountSource1)
	  status = PdfDest.ClonePage(PdfSource1, Page);
   if (status!= GdPictureStatus.OK)
	  break;
   if (Page <= PageCountSource2)
	  status = PdfDest.ClonePage(PdfSource2, Page);
   if (status!= GdPictureStatus.OK)
	  break;
}
if (status!= GdPictureStatus.OK)
{
   MessageBox.Show("Error occurred when cloning. Error: " + status.ToString(), "Merging PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
   status = PdfDest.SaveToFile("output.pdf");
   if (status == GdPictureStatus.OK)
   {
	  MessageBox.Show("Done!", "Merging PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Information);
   }
   else
   {
	  MessageBox.Show("The final PDF document can't be saved. Error: " + status.ToString(), "Merging PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
   }
}
end:
PdfSource1.Dispose();
PdfSource2.Dispose();
PdfDest.Dispose();

```

This code sample is an example that illustrates how to use our SDK. Please adapt it to your specific use case.

---

## Related pages

- [Saving images into folders based on their barcode](/guides/dotnet/samples/barcode-auto-sorting-vbnet.md)
- [Automatically rotate pages of a multipage TIFF file using OCR and C#](/guides/dotnet/samples/auto-rotate-tiff-cs.md)
- [Generate a multipage TIFF file using a compression mode per page in C#](/guides/dotnet/samples/create-multipage-tiff-cs.md)
- [Automatically rotate pages of a multipage TIFF file using OCR and VB.NET](/guides/dotnet/samples/auto-rotate-tiff-vbnet.md)
- [Saving images into folders based on their barcode](/guides/dotnet/samples/barcode-auto-sorting-cs.md)
- [Converting a Microsoft Word binary file document (DOC) to a Microsoft Word OpenXML document (DOCX) in C#](/guides/dotnet/samples/doc-to-docx-cs.md)
- [Convert PDFs, MS Office, and images to TIFF in C#](/guides/dotnet/samples/convert-to-tiff-cs.md)
- [Using an external OCR engine during PDF/OCR generation](/guides/dotnet/samples/integrate-omnipage-ocr-cs.md)
- [Merging two PDF documents by alternating pages from both files in VB.NET](/guides/dotnet/samples/merge-pdf-alternate-pages-vbnet.md)
- [Recompress an existing PDF using color detection in C#](/guides/dotnet/samples/recompress-pdf-cs.md)
- [Converting a multipage PDF to a multipage TIFF using color detection in C#](/guides/dotnet/samples/pdf-to-tiff-color-detection-cs.md)
- [Converting a multipage PDF to a multipage TIFF using color detection in VB.NET](/guides/dotnet/samples/pdf-to-tiff-color-detection-vbnet.md)
- [Recompress existing PDFs using MRC compression in C#](/guides/dotnet/samples/recompress-pdf-mrc-cs.md)
- [Recompress existing PDFs using MRC compression in VB.NET](/guides/dotnet/samples/recompress-pdf-mrc-vbnet.md)
- [Highlight and crop an image in C#](/guides/dotnet/samples/select-image-crop-area-cs.md)
- [Recompress an existing PDF using color detection in VB.NET](/guides/dotnet/samples/recompress-pdf-vbnet.md)
- [Highlight and crop an image in VB.NET](/guides/dotnet/samples/select-image-crop-area-vbnet.md)
- [Highlight a region in a PDF or an image in C#](/guides/dotnet/samples/select-pdf-image-region-cs.md)
- [Highlight a region in PDF or an image in VB.NET](/guides/dotnet/samples/select-pdf-image-region-vbnet.md)
- [Save bitonal TIFFs with VB.NET using photometric 0](/guides/dotnet/samples/tiff-photometric-vbnet.md)
- [Choosing the appropriate OCR zone in C#](/guides/dotnet/samples/select-ocr-zone-cs.md)
- [Save bitonal TIFF images with C# and CCITT4](/guides/dotnet/samples/tiff-photometric-cs.md)
- [Choosing the appropriate OCR zone in VB.NET](/guides/dotnet/samples/select-ocr-zone-vbnet.md)
- [Convert a multipage TIFF to a multipage JBIG2 in C#](/guides/dotnet/samples/tiff-to-jbig2-cs.md)
- [Convert a multipage TIFF to a multipage JBIG2 in VB.NET](/guides/dotnet/samples/tiff-to-jbig2-vbnet.md)

