AVAILABLE NOW: Spring 2025 Release

Convert HTML to PDF in C# and Java

By Apryse | 2025 Apr 03

Sanity Image
Read time

8 min

You might have heard about how Apryse has a world-class document converter. Our Apryse SDK can easily transform HTML to PDF. The portability of a PDF allows you to access content from webpages across multiple devices both on- and off-line.

Below are a few code snippets to convert HTML to PDF.

Convert HTML to PDF in C#

using pdftron;  

using pdftron.Common;  

using pdftron.PDF;  

using pdftron.SDF;  

  

namespace ConsoleApp1  

{  

    internal class Program  

    {  

        static void Main(string[] args)  

        {  

            try {  

  

                PDFNet.Initialize(PDFTronLicense.License);  

                HTML2PDF.SetModulePath(PDFTronLicense.ModulePath);  

  

                using (PDFDoc doc = new PDFDoc())  

                {  

  

                    HTML2PDF converter = new HTML2PDF();  

  

                    // Our HTML data  

                    string html = "<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>";  

  

                    // Add html data  

                    converter.InsertFromHtmlString(html);  

                    // Note, InsertFromHtmlString can be mixed with the other Insert methods.  

  

                    converter.Convert(doc);  

                    doc.Save("output.pdf", SDFDoc.SaveOptions.e_linearized);  

  

                }  

  

            }  

            catch (PDFNetException e)  

            {  

                Console.WriteLine(e.Message);  

            }  

  

        }  

    }  

} 

The above code could easily be modified to convert the input file to one of Apryse’s supported document formats by changing the document format enumeration.

Convert HTML to PDF in Java

import com.pdftron.common.PDFNetException;  

import com.pdftron.pdf.*;  

import com.pdftron.sdf.*;  

  

public class App {  

public static void main(String[] args) {  

  try{  

    PDFNet.initialize(PDFTronLicense.LICENSE);  

    HTML2PDF.setModulePath(PDFTronLicense.MODULE_PATH);  

  

    PDFDoc doc = new PDFDoc();  

    HTML2PDF converter = new HTML2PDF();  

  

    // Our HTML data  

    String html = "<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>";  

  

    // Add HTML data  

    converter.insertFromHtmlString(html);  

    // Note, insertFromHtmlString can be mixed with the other insert methods.  

  

    converter.convert(doc);  

    doc.save("src\\samples\\output.pdf", SDFDoc.SaveMode.LINEARIZED, null);  

  

    doc.close();  

  

  } catch (PDFNetException e) {  

    System.out.println(e.getMessage());  

  }  

}  

}  

Try for Free

Download the Apryse SDK for free. It’s fully functional and comes with free chat and email support.

Apryse is Here for You

Stay tuned for more conversion examples to see how the Apryse SDK will easily fit into any workflow converting PDF files into other document files or images and back again. Need help in the meantime? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team.

Sanity Image

Apryse

Share this post

email
linkedIn
twitter