Merge Multiple File Types into One PDF in C#, VB.NET

Maybe you have met this case in your work: You receive a lot of files that are in different file types, some are Word, some are PowerPoint slides, or some are Excel, etc, and you need to combine these files to one PDF for easy sharing. In this article, I’ll introduce you how to convert each file type into an Adobe PDF and then simultaneously merge them into a single PDF document using Spire.Office.

In this sample, I get four types of file (.doc, .docx, .xls, .pdf) prepared at first. Within the Spire.Office, it provides SaveToStream() method which allows us to save Word and Excel documents into stream, then these streams can be converted to PDF documents by calling the method of PdfDocument(Stream stream). At last, we could merge these PDF files to one file with the method PdfDocument.AppendPage(). More details would be as follows:

Code Snippet for Merge Multiple File Types to One PDF

Step 1: Create four new PDF documents.

PdfDocument[] documents = new PdfDocument[4];

Step 2: Load the .doc file, save it into stream and generate new PDF document from the stream.

using (MemoryStream ms1 = new MemoryStream())
{
      Document doc = new Document("01.doc", Spire.Doc.FileFormat.Doc);
      doc.SaveToStream(ms1, Spire.Doc.FileFormat.PDF);
      documents[0] = new PdfDocument(ms1);
}

Step 3: Repeat Step 2 to generate two PDF documents from .docx file and .xls file.

using (MemoryStream ms2 = new MemoryStream())
{
      Document docx = new Document("02.docx", Spire.Doc.FileFormat.Docx2010);
      docx.SaveToStream(ms2, Spire.Doc.FileFormat.PDF);
      documents[1] = new PdfDocument(ms2);
}
using (MemoryStream ms3 = new MemoryStream())
{
      Workbook workbook = new Workbook();
      workbook.LoadFromFile("03.xls", ExcelVersion.Version97to2003);
      workbook.SaveToStream(ms3, Spire.Xls.FileFormat.PDF);
      documents[2] = new PdfDocument(ms3);
}

Step 4: Load .pdf file and save it to documents[3].

documents[3] = new PdfDocument("04.pdf");

Step 5: Append the documents[0],[1],[2] to documents[3] and save as a new PDF document.

for (int i = 2; i > -1; i--)
{
      documents[3].AppendPage(documents[i]);
}
documents[3].SaveToFile("Result.pdf");

Screenshot of the Effect:

Merge Multiple File Types into One PDF

Full Code:

[C#]
using Spire.Doc;
using Spire.Xls;
using Spire.Pdf;

namespace MergeMultiTypestoOnePDF
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument[] documents = new PdfDocument[4];
            using (MemoryStream ms1 = new MemoryStream())
            {
                Document doc = new Document("01.doc", Spire.Doc.FileFormat.Doc);
                doc.SaveToStream(ms1, Spire.Doc.FileFormat.PDF);
                documents[0] = new PdfDocument(ms1);
            }
            using (MemoryStream ms2 = new MemoryStream())
            {
                Document docx = new Document("02.docx", Spire.Doc.FileFormat.Docx2010);
                docx.SaveToStream(ms2, Spire.Doc.FileFormat.PDF);
                documents[1] = new PdfDocument(ms2);
            }
            using (MemoryStream ms3 = new MemoryStream())
            {
                Workbook workbook = new Workbook();
                workbook.LoadFromFile("03.xls", ExcelVersion.Version97to2003);
                workbook.SaveToStream(ms3, Spire.Xls.FileFormat.PDF);
                documents[2] = new PdfDocument(ms3);
            }
            documents[3] = new PdfDocument("04.pdf");

            for (int i = 2; i > -1; i--)
            {
                documents[3].AppendPage(documents[i]);
            }

            documents[3].SaveToFile("Result.pdf");

        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Xls
Imports Spire.Pdf

Namespace MergeMultiTypestoOnePDF
	Class Program
		Private Shared Sub Main(args As String())
			Dim documents As PdfDocument() = New PdfDocument(3) {}
			Using ms1 As New MemoryStream()
				Dim doc As New Document("01.doc", Spire.Doc.FileFormat.Doc)
				doc.SaveToStream(ms1, Spire.Doc.FileFormat.PDF)
				documents(0) = New PdfDocument(ms1)
			End Using
			Using ms2 As New MemoryStream()
				Dim docx As New Document("02.docx", Spire.Doc.FileFormat.Docx2010)
				docx.SaveToStream(ms2, Spire.Doc.FileFormat.PDF)
				documents(1) = New PdfDocument(ms2)
			End Using
			Using ms3 As New MemoryStream()
				Dim workbook As New Workbook()
				workbook.LoadFromFile("03.xls", ExcelVersion.Version97to2003)
				workbook.SaveToStream(ms3, Spire.Xls.FileFormat.PDF)
				documents(2) = New PdfDocument(ms3)
			End Using
			documents(3) = New PdfDocument("04.pdf")

			For i As Integer = 2 To -1 + 1 Step -1
				documents(3).AppendPage(documents(i))
			Next

			documents(3).SaveToFile("Result.pdf")

		End Sub
	End Class
End Namespace