How to correctly merge documents using C#

with File Paths

The easiest and most reliable way to merge PDF files, using the “GroupDocs.Merger for .NET” product, is to use the full paths to these files.

The following example demonstrates how to merge PDF files using file paths in C# code:

  • Create an instance of Merger class and pass the full PDF file path as a constructor parameter.
  • Add another PDF file to merge with Join method.
  • Call Merger class Save method and specify the filename for the merged PDF file as parameter.
// Load the source PDF file
using (Merger merger = new Merger(@"c:\sample1.pdf"))
{
    // Add another PDF file to merge
    merger.Join(@"c:\sample2.pdf");
    // Merge PDF files and save result
    merger.Save(@"c:\merged.pdf");
}

with File Streams

There are also several ways to work with streams of files that need to be merged using the GroupDocs.Merger product. The most reliable way to combine files without additional parameters is to use the System.FileStream class.

The following example demonstrates how to merge DOCX file streams in C# code:

  • Create an instance of Merger class and pass the DOCX file stream as a constructor parameter.
  • Add another DOCX file stream to merge with Join method.
  • Call Merger class Save method and specify the filename for the merged DOCX file as parameter.
string filePath1 = "c:/sample1.docx";
string filePath2 = "c:/sample2.docx";

FileStream fileStream1 = File.OpenRead(filePath1);
FileStream fileStream2 = File.OpenRead(filePath2);

// Load the source DOCX file stream
using (Merger merger = new Merger(fileStream1))
{
    // Add another DOCX file stream to merge
    merger.Join(fileStream2);
    // Merge DOCX file streams and save result
    merger.Save(@"c:\merged.docx");
}

with Memory Streams

There are situations, and quite often, when it is necessary to work with other classes inherited from System.Stream, for example with MemoryStream or others. In this case, it is necessary to use additional parameters to specify the exact file type that is used in this case. Because the “GroupDocs.Merger for .NET” product cannot always accurately determine the file type by its stream. Therefore, to avoid exceptions when merging PDF and other files, see the example below:

  • Create an instance of Merger class and pass instance of LoadOptions with PPTX file type as a constructor parameter.
  • Create an instance of JoinOptions class with PPTX file type as a constructor parameter.
  • Add another PPTX stream to merge with Join method and pass instance of JoinOptions class as a method parameter.
  • Call Merger class Save method and specify the filename for the merged PPTX file as parameter.

string filePath1 = "c:/sample1.pptx";
string filePath2 = "c:/sample2.pptx";

FileType fileType1 = FileType.FromExtension(Path.GetExtension(filePath1));
FileType fileType2 = FileType.FromExtension(Path.GetExtension(filePath2));

MemoryStream memoryStream1 = new MemoryStream();
using (FileStream fileStream1 = File.OpenRead(filePath1))
{
    fileStream1.CopyTo(memoryStream1);
}

MemoryStream memoryStream2 = new MemoryStream();
using (FileStream fileStream2 = File.OpenRead(filePath2))
{
    fileStream2.CopyTo(memoryStream2);
}

// Init Load options with defined FileType
LoadOptions loadOptions = new LoadOptions(fileType1);

// Load the source PPTX stream
using (Merger merger = new Merger(memoryStream1, loadOptions))
{
    // Define join options with PPTX file type
    JoinOptions joinOptions = new JoinOptions(fileType2);
    // Add another PPTX stream to merge
    merger.Join(memoryStream2, joinOptions);
    // Merge PPTX streams and save result
    merger.Save(@"c:\merged.pptx");
}

with Memory Streams for cross-merging

There may also be situations when it is necessary to combine different types of streams into PDF. In this case, you can use the example below:

  • Create an instance of Merger class and pass instance of LoadOptions with ini DOCX file type and out PDF one as a constructor parameters.
  • Create an instance of JoinOptions class with PPTX file type as a constructor parameter.
  • Add another PPTX stream to merge with Join method and pass instance of JoinOptions class as a method parameter.
  • Call Merger class Save method and specify the filename for the merged PDF file as parameter.

string filePath1 = "c:/sample1.docx";
string filePath2 = "c:/sample2.pptx";

MemoryStream memoryStream1 = new MemoryStream();
using (FileStream fileStream1 = File.OpenRead(filePath1))
{
    fileStream1.CopyTo(memoryStream1);
}

MemoryStream memoryStream2 = new MemoryStream();
using (FileStream fileStream2 = File.OpenRead(filePath2))
{
    fileStream2.CopyTo(memoryStream2);
}

// Init Load options with defined FileTypes
LoadOptions loadOptions = new LoadOptions(FileType.DOCX, FileType.PDF);

// Load the source DOCX stream as PDF
using (Merger merger = new Merger(memoryStream1, loadOptions))
{
    // Define join options with PPTX file type
    JoinOptions joinOptions = new JoinOptions(FileType.PPTX);
    // Add another PPTX stream to merge
    merger.Join(memoryStream2, joinOptions);
    // Merge document streams and save PDF result
    merger.Save(@"c:\merged.pdf");
}