Convert and Merge Office Files to One PDF File in C#


In this article, you'll learn how to convert and combine multiple office files into a single PDF file programmatically using Spire.Office in C#. And you can determine whether to adjust PDF size while converting.

Introduction

Portable Document Format (PDF) developed in the early 1990s has become a popular file format in business because PDF files:

•Are easy to be shared across platforms and devices
•Can be password-protected to prevent copying and editing
•Preserve all source file information, even when text, graphics, audio, 3D maps, and more are combined in a single file

Therefore, in some occasions, you may need to convert other format files such as Word, Excel and PowerPoint into PDF file for above reasons. In this article, an easy solution based on Spire.Office is provided to convert and combine MS Office files into a single PDF file.

Brief introduction about Spire.Office

Spire.Office is a .NET class library, which enables developers to manipulate MS Word, Excel, PowerPoint and PDF documents in any .NET platform. Before we start to code, please download it from Nuget and reference the related dlls to Visual Studio.

reference

Now let's see how to implement the desired function with this tool in our own application.

Using the code

Step 1: Create a WinForm Application, design the Form1 as below.

form

Step 2: Add following code to btnAdd click event. The code under this button enables users to select target files via OpenFileDialog and add the file paths to listbox.


private void btnAdd_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;
listBox1.Items.AddRange(files);
}
}

Step 3: Double click Merge button to write code. In this part, we need firstly convert different file formats to PDF file through MemoryStream, then merge the PDF files into one PDF.

It is worth mention that when you merge different files types to one PDF document, you'll find the sizes varies from one type to another type, for instance, the page size of PowerPoint is quite different from that of Word or Excel. If you would like to keep the merged document neat, you can adjust the PDF page size by creating another PDF document with specified page size and copy the contents of merged file to the new PDF document.

private void btnMerge_Click(object sender, EventArgs e)
{
//Convert other file formats to PDF file
string ext=string.Empty;
foreach (object item in listBox1.Items)
{
ext=Path.GetExtension(item.ToString());
switch (ext)
{
case ".docx":
using (MemoryStream ms = new MemoryStream())
{
Document doc = new Document(item.ToString());
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.Auto);
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());
xls.SaveToStream(ms, Spire.Xls.FileFormat.PDF);
PdfFiles.Add(new PdfDocument(ms));
}
break;
default:
break;
}
}
//Merge the PDF files into one PDF
PdfDocument newPdf1 = new PdfDocument();
foreach (PdfDocument doc in PdfFiles)
{
newPdf1.AppendPage(doc);
}
//Create a new PDF with specified page size, copy the content of merged file to new PDF file
PdfDocument newPdf2 = new PdfDocument();
foreach (PdfPageBase page in newPdf1.Pages)
{
PdfPageBase newPage = newPdf2.Pages.Add(PdfPageSize.A4, new PdfMargins(0));
PdfTextLayout loLayout = new PdfTextLayout();
loLayout.Layout = PdfLayoutType.OnePage;
page.CreateTemplate().Draw(newPage, new PointF(0, 0), loLayout);
}
//Save the destination PDF file
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Pdf files(*.pdf)|*.pdf";
if (DialogResult.OK == sfd.ShowDialog())
{
newPdf2.SaveToFile(sfd.FileName);
}
}

Step 4: Run the program. Click "Add Files" button to add selected files to listbox.

addFile

Step 5: Click "Merge" button to convert and merge different file formats in a single PDF file, and save the destination PDF file.

save

Entire code

using System.Drawing;
using System.Windows.Forms;
using Spire.Pdf;
using Spire.Doc;
using Spire.Xls;
using Spire.Presentation;
using System.IO;
using Spire.Pdf.Graphics;

namespace ConvertAndMerge
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
PdfFiles = new List();
}
public List PdfFiles { get; set;}

//Add files to listbox
private void btnAdd_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;
listBox1.Items.AddRange(files);
}
}

private void btnMerge_Click(object sender, EventArgs e)
{
//Convert other file formats to PDF file
string ext=string.Empty;
foreach (object item in listBox1.Items)
{
ext=Path.GetExtension(item.ToString());
switch (ext)
{
case ".docx":
using (MemoryStream ms = new MemoryStream())
{
Document doc = new Document(item.ToString());
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.Auto);
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());
xls.SaveToStream(ms, Spire.Xls.FileFormat.PDF);
PdfFiles.Add(new PdfDocument(ms));
}
break;
default:
break;
}
}
//Merge the PDF files into one PDF
PdfDocument newPdf1 = new PdfDocument();
foreach (PdfDocument doc in PdfFiles)
{
newPdf1.AppendPage(doc);
}
//Create a new PDF with specified page size, copy the content of merged file to new PDF file
PdfDocument newPdf2 = new PdfDocument();
foreach (PdfPageBase page in newPdf1.Pages)
{
PdfPageBase newPage = newPdf2.Pages.Add(PdfPageSize.A4, new PdfMargins(0));
PdfTextLayout loLayout = new PdfTextLayout();
loLayout.Layout = PdfLayoutType.OnePage;
page.CreateTemplate().Draw(newPage, new PointF(0, 0), loLayout);
}
//Save the destination PDF file
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Pdf files(*.pdf)|*.pdf";
if (DialogResult.OK == sfd.ShowDialog())
{
newPdf2.SaveToFile(sfd.FileName);
}
}
}
}

Note

Using this method to combine Office files into PDF file, the layout may change because all the content from different files are forced to display on a fixed page size (A4 for this sample). If you would like to keep the original layout, just convert them to PDF and merge together.


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: