Convert folder of HTML files to single PDF file in C# and VB.Net

Convert folder of HTML files to single PDF file
in C# and VB.Net.


Complete code

using System;
using System.IO;
using System.Collections.Generic;

namespace Sample
{
    class Test
    {
        static void Main(string[] args)
        {
            // Convert folder of HTML files to single PDF file.
            SautinSoft.PdfVision v = new SautinSoft.PdfVision();

            // Set "Edge mode" to support all modern CSS.
            SautinSoft.PdfVision.TrySetBrowserModeEdgeInRegistry();

            v.PageStyle.PageOrientation.Portrait();
            v.PageStyle.PageSize.Letter();
            v.PageStyle.PageMarginLeft.Mm(20);
            v.PageStyle.PageMarginBottom.Mm(20);
            v.PageStyle.PageMarginTop.Mm(15);

            string directoryWithHTMLs = Path.GetFullPath(@"..\..\");
            FileInfo singlePdf = new FileInfo(@"Result.pdf");

            // Convert each HTML file from directory to a PDF file.
            string[] htmlFiles = Directory.GetFiles(directoryWithHTMLs, "*.htm*");
            List<byte[]> pdfInventory = new List<byte[]>();

            foreach (string htmlFile in htmlFiles)
            {
                pdfInventory.Add(v.ConvertHtmlFileToPDFStream(htmlFile));
            }

            // Merge all PDFs into a single PDF file.
            try
            {
                File.WriteAllBytes(singlePdf.FullName, v.MergePDFStreamArrayToPDFStream(pdfInventory));
                // Open the resulting PDF document in a default PDF Viewer.
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(singlePdf.FullName) { UseShellExecute = true });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        }
    }
}

Download.

        
            Imports System
Imports System.IO
Imports System.Collections.Generic

Namespace Sample
    Friend Class Test
        Shared Sub Main(ByVal args() As String)
            ' Convert folder of HTML files to single PDF file.
            Dim v As New SautinSoft.PdfVision()

            ' Set "Edge mode" to support all modern CSS.
            SautinSoft.PdfVision.TrySetBrowserModeEdgeInRegistry()

            v.PageStyle.PageOrientation.Portrait()
            v.PageStyle.PageSize.Letter()
            v.PageStyle.PageMarginLeft.Mm(20)
            v.PageStyle.PageMarginBottom.Mm(20)
            v.PageStyle.PageMarginTop.Mm(15)

            Dim directoryWithHTMLs As String = Path.GetFullPath("..\..\")
            Dim singlePdf As New FileInfo("Result.pdf")

            ' Convert each HTML file from directory to a PDF file.
            Dim htmlFiles() As String = Directory.GetFiles(directoryWithHTMLs, "*.htm*")
            Dim pdfInventory As New List(Of Byte())()

            For Each htmlFile As String In htmlFiles
                pdfInventory.Add(v.ConvertHtmlFileToPDFStream(htmlFile))
            Next htmlFile

            ' Merge all PDFs into a single PDF file.
            Try
                File.WriteAllBytes(singlePdf.FullName, v.MergePDFStreamArrayToPDFStream(pdfInventory))
                ' Open the resulting PDF document in a default PDF Viewer.
                System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(singlePdf.FullName) With {.UseShellExecute = True})
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Console.ReadLine()
            End Try
        End Sub
    End Class
End Namespace

Download.

If you are looking also for a .Net solution to Convert HTML to PDF documents, see our Document .Net.


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.