Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Created April 24, 2026 15:59
Show Gist options
  • Select an option

  • Save GroupDocsGists/ba22cae3ecfb2de4ab50ff73282513d9 to your computer and use it in GitHub Desktop.

Select an option

Save GroupDocsGists/ba22cae3ecfb2de4ab50ff73282513d9 to your computer and use it in GitHub Desktop.
Examples of merging multiple document formats into a PDF binder, extracting pages, and validating page count.

πŸ“¦ Prerequisites

πŸ’» Key Capabilities

  • Merge Word, Excel, and PDF files into a single PDF binder
  • Extract specific pages from an existing PDF binder
  • Validate merged output by retrieving total page count
  • Scan a folder and automatically merge all supported documents

πŸ“Ž Code Example

See the following example files for each operation:

πŸ›  How to Use

  1. Install GroupDocs.Merger via NuGet.
  2. Prepare your source files (e.g., .docx, .xlsx, .pdf).
  3. Use Merger to join files with JoinOptions (PDF must be the primary file for cross-format conversion).
  4. For extraction, use ExtractOptions with desired page numbers.
  5. Call Save() to persist the result.
  6. Use GetDocumentInfo() to verify output integrity.

πŸ“Ž Related Articles

  • (none provided)
// Folder containing source documents
string folderPath = "./input_documents";
string outputPath = "folder_binder.pdf";
var extensions = new[] { ".pdf", ".docx", ".xlsx" };
var files = Directory
.EnumerateFiles(folderPath)
.Where(f => extensions.Contains(
Path.GetExtension(f).ToLowerInvariant()))
.OrderBy(f => Path.GetExtension(f).ToLowerInvariant() == ".pdf"
? 0 : 1)
.ThenBy(f => f)
.ToArray();
if (files.Length == 0)
{
throw new InvalidOperationException(
$"No supported documents found in '{folderPath}'.");
}
Console.WriteLine(
$"Folder binder: discovered {files.Length} files in {folderPath}");
return MergeMultipleFormatsIntoPdf(files, outputPath);
// Path to the source PDF binder
string binderPath = "output_binder.pdf";
string outputPath = "extracted_section.pdf";
// Define page numbers to extract (1-based indexing)
int[] pages = { 1, 2, 5 };
using var merger = new Merger(binderPath);
merger.ExtractPages(new ExtractOptions(pages));
merger.Save(outputPath);
Console.WriteLine(
$"Extracted pages [{string.Join(",", pages)}] to "
+ Path.GetFullPath(outputPath));
// Path to the merged PDF
string pdfPath = "output_binder.pdf";
using var merger = new Merger(pdfPath);
var info = merger.GetDocumentInfo();
return info.PageCount;
// Paths to the source documents
string[] sourcePaths = {
"source.docx",
"source.xlsx",
"source.pdf"
};
string outputPath = "output_binder.pdf";
Console.WriteLine($"Primary source: {sourcePaths[0]}");
using var merger = new Merger(sourcePaths[0]);
var joinOptions = new JoinOptions();
for (int i = 1; i < sourcePaths.Length; i++)
{
Console.WriteLine($"Joining: {sourcePaths[i]}");
merger.Join(sourcePaths[i], joinOptions);
}
merger.Save(outputPath);
Console.WriteLine(
$"Unified PDF binder saved to: {Path.GetFullPath(outputPath)}");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment