How to merge two PDF files
using C# and VB.Net.


Complete code.

using System;
using System.IO;
using SautinSoft;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            // Merge two PDF files.
            // 1. Let's get a PDF from .xlsx.
            // 2. Let's merge this PDF with itself.
            ExcelToPdf x = new ExcelToPdf();
            x.PageStyle.PageSize.Letter();
            x.PageStyle.PageMarginTop.mm(5);

            // Set PDF as output format.
            x.OutputFormat = SautinSoft.ExcelToPdf.eOutputFormat.Pdf;

            // Let's convert only 3rd page.
            x.Sheets.Custom(new int[]{3});

            string excelFile = Path.GetFullPath(@"..\..\test.xlsx");
            FileInfo pdfFile = new FileInfo(Path.ChangeExtension(excelFile, ".pdf"));
            string singlePdf = Path.Combine(pdfFile.Directory.FullName, "Single.pdf");

            try
            {
                // 1. Convert Excel to PDF.
                x.ConvertFile(excelFile, pdfFile.FullName);
                
                // 2. Merge the PDF file with itself.
                x.MergePDFFileArrayToPDFFile(new string[] { pdfFile.FullName, pdfFile.FullName },
                    singlePdf);

                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(singlePdf) { UseShellExecute = true });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        }
    }
}

Download.

        
            Imports System
Imports System.IO
Imports SautinSoft

Module Sample

    Sub Main()
        ' Merge two PDF files.
        ' 1. Let's get a PDF from .xlsx.
        ' 2. Let's merge this PDF with itself.
        Dim x As New ExcelToPdf()
        x.PageStyle.PageSize.Letter()
        x.PageStyle.PageMarginTop.mm(5)

        ' Set PDF as output format.
        x.OutputFormat = SautinSoft.ExcelToPdf.eOutputFormat.Pdf

        ' Let's convert only 3rd page.
        x.Sheets.Custom(New Integer() {3})

        Dim excelFile As String = Path.GetFullPath("..\test.xlsx")
        Dim pdfFile As New FileInfo(Path.ChangeExtension(excelFile, ".pdf"))
        Dim singlePdf As String = Path.Combine(pdfFile.Directory.FullName, "Single.pdf")

        Try
            ' 1. Convert Excel to PDF.
            x.ConvertFile(excelFile, pdfFile.FullName)

            ' 2. Merge the PDF file with itself.
            x.MergePDFFileArrayToPDFFile(New String() {pdfFile.FullName, pdfFile.FullName}, singlePdf)

            System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(singlePdf) With {.UseShellExecute = True})
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.ReadLine()
        End Try
    End Sub
End Module

Download.


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.