Convert and Merge Different File Types to One PDF in C#

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, 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 C#.

In the classes of Spire.Office, it provides SaveToStream() method which allows us to save Word, Excel and PowerPoint documents into streams, 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(). Here, I’d like to create a WinForm project to explain the whole procedure.

Tools We Need:

Spire.Office for .NET (which contains Spire.Doc.dll, Spire.Pdf.dll, Spire.Presentation.dll, Spire.Xls.dll)
– Visual Studio 2005 or above

Detailed Steps:

Step 1: Create a Winform Application, design the Form1 as below. This Form includes a ListBox to display files we want to load in, an “Add Files” button and a “Merge” button.

1

Step 2: Double click “Add Files” button to write code.

    private void buttonAdd_Click(object sender, EventArgs e)

        {

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = “All files (*.docx, *.pdf, *.pptx, *.pdf)|*.docx;*.pdf;*.pptx;*.xlsx”;

            ofd.Multiselect=true;

            if (DialogResult.OK == ofd.ShowDialog())

            {

                string[] files = ofd.FileNames;

               listBoxFiles.Items.AddRange(files);

            }

        }

Step 3: Input below code to realize the Convert&Merge function in buttonMerge_Click event.

   private void buttonMerge_Click(object sender, EventArgs e)

        {

            string ext=string.Empty;

            foreach (object item in listBoxFiles.Items)

            {

                ext=Path.GetExtension(item.ToString());

                switch (ext)

                {

                    case “.docx”:

                        using (MemoryStream ms = new MemoryStream())

                        {

                            Document doc = new Document(item.ToString(),Spire.Doc.FileFormat.Docx2010);

                           doc.SaveToStream(ms, Spire.Doc.FileFormat.PDF);

                            PdfFiles.Add(new PdfDocument(ms));

                        }

                        break;

                    case “.pdf”:

                        PdfFiles.Add(new PdfDocument(item.ToString()));

                        break;

                    case “.pptx”:

                        using (MemoryStream ms = new MemoryStream())

                        {

                            Presentation ppt = new Presentation(item.ToString(), Spire.Presentation.FileFormat.Pptx2010);

                           ppt.SaveToFile(ms,Spire.Presentation.FileFormat.PDF);

                            PdfFiles.Add(new PdfDocument(ms));

                        }

                        break;

                    case “.xlsx”:

                        using (MemoryStream ms = new MemoryStream())

                        {

                            Workbook xls = new Workbook();

                            xls.LoadFromFile(item.ToString(),ExcelVersion.Version2010);

                           xls.SaveToStream(ms, Spire.Xls.FileFormat.PDF);

                            PdfFiles.Add(new PdfDocument(ms));

                        }

                        break;

                    default:

                        break;

                }              

            }

 

            PdfDocument newPdf = new PdfDocument();

            foreach (PdfDocument doc in PdfFiles)

            {

                newPdf.AppendPage(doc);

            }

 

            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Filter = “Pdf files(*.pdf)|*.pdf”;

            if (DialogResult.OK == sfd.ShowDialog())

            {

                newPdf.SaveToFile(sfd.FileName);

            }

        }

Step 4: Run the code, the designed dialog box will pop up, click “Add Files” button to select type of files to convert.

2

Step 5: Click “Merge” button to convert and generate a new file. You can choose where you want to save it and name the newly-created file.

3

Full Code of Form1.cs:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

 

using Spire.Doc;

using Spire.Pdf;

using Spire.Presentation;

using Spire.Xls;

 

namespace ConvertAndMerge

{

    public partial class FormMain : Form

    {

        public FormMain()

        {

            InitializeComponent();

            PdfFiles = new List<PdfDocument>();

        }

 

        public List<PdfDocument> PdfFiles { get; set;}

 

        private void buttonAdd_Click(object sender, EventArgs e)

        {

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = “All files (*.docx, *.pdf, *.pptx, *.pdf)|*.docx;*.pdf;*.pptx;*.xlsx”;

            ofd.Multiselect=true;

            if (DialogResult.OK == ofd.ShowDialog())

            {

                string[] files = ofd.FileNames;

                listBoxFiles.Items.AddRange(files);

            }

        }

 

        private void buttonMerge_Click(object sender, EventArgs e)

        {

            string ext=string.Empty;

            foreach (object item in listBoxFiles.Items)

            {

                ext=Path.GetExtension(item.ToString());

                switch (ext)

                {

                    case “.docx”:

                        using (MemoryStream ms = new MemoryStream())

                        {

                            Document doc = new Document(item.ToString(),Spire.Doc.FileFormat.Docx2010);

                            doc.SaveToStream(ms, Spire.Doc.FileFormat.PDF);

                            PdfFiles.Add(new PdfDocument(ms));

                        }

                        break;

                    case “.pdf”:

                        PdfFiles.Add(new PdfDocument(item.ToString()));

                        break;

                    case “.pptx”:

                        using (MemoryStream ms = new MemoryStream())

                        {

                            Presentation ppt = new Presentation(item.ToString(),Spire.Presentation.FileFormat.Pptx2010);

                            ppt.SaveToFile(ms,Spire.Presentation.FileFormat.PDF);

                            PdfFiles.Add(new PdfDocument(ms));

                        }

                        break;

                    case “.xlsx”:

                        using (MemoryStream ms = new MemoryStream())

                        {

                            Workbook xls = new Workbook();

                            xls.LoadFromFile(item.ToString(),ExcelVersion.Version2010);


xls.SaveToStream(ms, Spire.Xls.FileFormat.PDF);

                            PdfFiles.Add(new PdfDocument(ms));

                        }

                        break;

                    default:

                        break;

                }              

            }

 

            PdfDocument newPdf = new PdfDocument();

            foreach (PdfDocument doc in PdfFiles)

            {

                newPdf.AppendPage(doc);

            }

 

            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Filter = “Pdf files(*.pdf)|*.pdf”;

            if (DialogResult.OK == sfd.ShowDialog())

            {

            newPdf.SaveToFile(sfd.FileName);

            }

        }

 

        private void FormMain_Load(object sender, EventArgs e)

        {

 

        }

    }

}

Advertisements