Call Merger class Save method and specify the filename for the merged PDF file as parameter.
// Load the source PDF fileusing(Mergermerger=newMerger(@"c:\sample1.pdf")){// Add another PDF file to mergemerger.Join(@"c:\sample2.pdf");// Merge PDF files and save resultmerger.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.
stringfilePath1="c:/sample1.docx";stringfilePath2="c:/sample2.docx";FileStreamfileStream1=File.OpenRead(filePath1);FileStreamfileStream2=File.OpenRead(filePath2);// Load the source DOCX file streamusing(Mergermerger=newMerger(fileStream1)){// Add another DOCX file stream to mergemerger.Join(fileStream2);// Merge DOCX file streams and save resultmerger.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.
stringfilePath1="c:/sample1.pptx";stringfilePath2="c:/sample2.pptx";FileTypefileType1=FileType.FromExtension(Path.GetExtension(filePath1));FileTypefileType2=FileType.FromExtension(Path.GetExtension(filePath2));MemoryStreammemoryStream1=newMemoryStream();using(FileStreamfileStream1=File.OpenRead(filePath1)){fileStream1.CopyTo(memoryStream1);}MemoryStreammemoryStream2=newMemoryStream();using(FileStreamfileStream2=File.OpenRead(filePath2)){fileStream2.CopyTo(memoryStream2);}// Init Load options with defined FileTypeLoadOptionsloadOptions=newLoadOptions(fileType1);// Load the source PPTX streamusing(Mergermerger=newMerger(memoryStream1,loadOptions)){// Define join options with PPTX file typeJoinOptionsjoinOptions=newJoinOptions(fileType2);// Add another PPTX stream to mergemerger.Join(memoryStream2,joinOptions);// Merge PPTX streams and save resultmerger.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.
stringfilePath1="c:/sample1.docx";stringfilePath2="c:/sample2.pptx";MemoryStreammemoryStream1=newMemoryStream();using(FileStreamfileStream1=File.OpenRead(filePath1)){fileStream1.CopyTo(memoryStream1);}MemoryStreammemoryStream2=newMemoryStream();using(FileStreamfileStream2=File.OpenRead(filePath2)){fileStream2.CopyTo(memoryStream2);}// Init Load options with defined FileTypesLoadOptionsloadOptions=newLoadOptions(FileType.DOCX,FileType.PDF);// Load the source DOCX stream as PDFusing(Mergermerger=newMerger(memoryStream1,loadOptions)){// Define join options with PPTX file typeJoinOptionsjoinOptions=newJoinOptions(FileType.PPTX);// Add another PPTX stream to mergemerger.Join(memoryStream2,joinOptions);// Merge document streams and save PDF resultmerger.Save(@"c:\merged.pdf");}
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.