HTML to Image Basic

Pdf

HTML to Image Basic

With just a few lines of code, your application is now ready to save web pages into Image.

What is HTML?

  • HTML (short for HyperText Markup Language or "Hypertext Markup Language") is a markup language designed to create web pages. Together with CSS and JavaScript, HTML creates a technical trilogy for the World Wide Web.
  • HTML is defined as a simple application of SGML and is used in organizations that need complex publishing requirements. HTML has become an Internet standard maintained by the World Wide Web Consortium (W3C).
  • The latest official version of HTML is HTML 4.01 (1999). Later, the developers replaced it with XHTML. Currently, HTML is being developed with the latest HTML5 version and promises to bring a new interface to the Web.
  • By using dynamic HTML or Ajax, developers can be created and processed by a large number of tools, from a simple text editor - which we can import from the very first line. - for complex WYSIWYG publishing tools. HTML document) is connected. And so, the link on the Web is called Hypertext. As the name suggests, HTML is a markup language, meaning you use HTML to mark text documents with Tags to tell the browser how to structure it to display on the screen.
  • Currently, HTML is a must for the programmers around the world and almost holds the unique position of the most used format in the world. This problem is not too surprising because it's easy to see that HTML can easily be found on all websites today.

What is PDF?

  • PDF (short for Portable Document Format) is a reasonably standard text file format of Adobe Systems. Similar to the Word (.doc) format, PDF supports raw text (text) along with fonts, graphic images, sounds, and many other effects. However, displaying PDF documents does not depend on the working environment of the user (machine configuration, software, and operating system).

Introducing PdfProLib - A fast, accurate library that easy to integrate into any .NET application

Designed by hundreds of C# experts, PdfProLib HTML to PDF offers a vast ecosystem of highly customizable, serving all software development needs.

  • Flexibility is the name of the game, PdfProLib HTML to PDF is adaptable and supports a huge number of platforms, compatible with every project: MVC, Windows Forms, ASP.NET, and WPF applications or Windows Azure Cloud services to convert URLs, HTML strings and streams, SVG vector graphics to a PDF document, raster image or SVG vector image.
  • This tool is the best option for converting HTML websites into PDF files or images. We can also use it to create reports in PDF format created from partner HTML versions, making it easy to maintain a report template while some forms are available to end-users.

Advanced Features

PdfProLib HTML to PDF brings you a diverse and advanced feature library, maximum support for all operations.

  • Support latest HTML5 standards
  • Supports CSS3
  • Convert to raster and SVG images
  • Recognizes page breaks in attributes and styles
  • Supports internal and external links
  • Supports Unicode, Right-to-left direction
  • Merges converted PDF documents into a single file
  • Optimized for both single and multi-thread conversions
  • Support for .NET Full Framework, Xamarin iOS, Mac, Android, Mono, .NET CF, and .NET Core, Standard (will be available soon)
  • Royalty-free redistribution
  • Supports .NET 2.0, 3.5, 4.0, 4.5, 4.6, and 4.7.

Demo

To add to your .NET application, the ability to convert from HTML to Image, first, make sure you have installed and imported the Office Component PDF library.

  • C# Version:
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.IO;
    using OfficeComponent.HtmlToPdf;
  • VB Version:
    Imports System.ComponentModel
    Imports System.Drawing.Imaging
    Imports System.Drawing.Printing
    Imports System.IO
    Imports OfficeComponent.HtmlToPdf

For C# applications running on the web, you will need to use the following libraries.

  • C# Version:
    using System.Web.UI;
    using System.Web.UI.WebControls;
  • VB Version:
    Imports System.Web.UI
    Imports System.Web.UI.WebControls

You will need to declare some variables for future use.

  • C# Version:
        public bool ConvertUrl { get; set; }
        public string Url { get; set; }
        public string HtmlString { get; set; }
        public string HtmlStringBaseUrl { get; set; }

        public int Width { get; set; }
        public int Height { get; set; }

        public Rectangle Crop { get; set; }
        public string ImageFormat { get; set; }
        public int Quality { get; set; }

        public int ConversionDelay { get; set; }
        public int Timeout { get; set; }

        public EventHandler<ConverterProgressEventArgs> Progress;
  • VB Version:
        Private privateConvertUrl As Boolean
        Public Property ConvertUrl() As Boolean
            Get
                Return privateConvertUrl
            End Get
            Set(ByVal value As Boolean)
                privateConvertUrl = value
            End Set
        End Property
        Private privateUrl As String
        Public Property Url() As String
            Get
                Return privateUrl
            End Get
            Set(ByVal value As String)
                privateUrl = value
            End Set
        End Property
        Private privateHtmlString As String
        Public Property HtmlString() As String
            Get
                Return privateHtmlString
            End Get
            Set(ByVal value As String)
                privateHtmlString = value
            End Set
        End Property
        Private privateHtmlStringBaseUrl As String
        Public Property HtmlStringBaseUrl() As String
            Get
                Return privateHtmlStringBaseUrl
            End Get
            Set(ByVal value As String)
                privateHtmlStringBaseUrl = value
            End Set
        End Property

        Private privateWidth As Integer
        Public Property Width() As Integer
            Get
                Return privateWidth
            End Get
            Set(ByVal value As Integer)
                privateWidth = value
            End Set
        End Property
        Private privateHeight As Integer
        Public Property Height() As Integer
            Get
                Return privateHeight
            End Get
            Set(ByVal value As Integer)
                privateHeight = value
            End Set
        End Property

        Private privateCrop As Rectangle
        Public Property Crop() As Rectangle
            Get
                Return privateCrop
            End Get
            Set(ByVal value As Rectangle)
                privateCrop = value
            End Set
        End Property
        Private privateImageFormat As String
        Public Property ImageFormat() As String
            Get
                Return privateImageFormat
            End Get
            Set(ByVal value As String)
                privateImageFormat = value
            End Set
        End Property
        Private privateQuality As Integer
        Public Property Quality() As Integer
            Get
                Return privateQuality
            End Get
            Set(ByVal value As Integer)
                privateQuality = value
            End Set
        End Property

        Private privateConversionDelay As Integer
        Public Property ConversionDelay() As Integer
            Get
                Return privateConversionDelay
            End Get
            Set(ByVal value As Integer)
                privateConversionDelay = value
            End Set
        End Property
        Private privateTimeout As Integer
        Public Property Timeout() As Integer
            Get
                Return privateTimeout
            End Get
            Set(ByVal value As Integer)
                privateTimeout = value
            End Set
        End Property

        Public Progress As EventHandler(Of ConverterProgressEventArgs)

And constructors with the different number of input parameters:

  • C# Version:
        public HtmlToImageBasicExample()
            : base(null, null)
        {

        }

        public HtmlToImageBasicExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)
        {

        }

        public HtmlToImageBasicExample(string commonDataPath, string outputDir, string xmlFile)
            : base(commonDataPath, outputDir, xmlFile)
        {

        }
  • VB Version:
        Public Sub New()
            MyBase.New(Nothing, Nothing)

        End Sub

        Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
            MyBase.New(commonDataPath, outputDir)

        End Sub

        Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
            MyBase.New(commonDataPath, outputDir, xmlFile)

        End Sub

The OfficeComponent PDF allows you to add the capability to convert any HTML webpage to Image by just adding a few lines of code. The HtmlToPdfConverter class allows you to set several advanced conversion options. We take advantage of that class with the following code snippet:

  • C# Version:
    var c = new HtmlToPdfConverter()
    {
        ViewportWidth = Width,
        ViewportHeight = Height,
                
        ImageFormat = ImageFormat,
        ImageCrop = Crop,
        ImageQuality = Quality,

        Delay = ConversionDelay,
        Timeout = Timeout
    };
  • VB Version:
    Dim c = New HtmlToPdfConverter() With {
        .ViewportWidth = Width,
        .ViewportHeight = Height,
        .ImageFormat = ImageFormat,
        .ImageCrop = Crop,
        .ImageQuality = Quality,
        .Delay = ConversionDelay,
        .Timeout = Timeout
    }

The snippet above can be placed in your executable function to get the job done.

  • C# Version:
            if (Width <= 0 || Height < 0)
            {
                ShowError("Invalid Width or Height");
                return null;
            }

            if (ConversionDelay < 0)
            {
                ShowError("Invalid conversion delay");
                return null;
            }

            // Set output path.
            var outputPath = Path.Combine(OutputDir, this.GetType().Name + "." + ImageFormat);
            if (File.Exists(outputPath))
                File.Delete(outputPath);

            // Create a new converter.
            var c = new HtmlToPdfConverter()
            {
                ViewportWidth = Width,
                ViewportHeight = Height,

                ImageFormat = ImageFormat,
                ImageCrop = Crop,
                ImageQuality = Quality,

                Delay = ConversionDelay,
                Timeout = Timeout
            };

            if (Progress != null)
                c.Progress += Progress;

            try
            {
                if (ConvertUrl)
                {
                    if (string.IsNullOrWhiteSpace(Url))
                    {
                        ShowError("Please specify the URL to convert.");
                        return null;
                    }

                    // Convert URL
                    c.ConvertToImage(Url, outputPath);
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(HtmlString))
                    {
                        ShowError("Please specify the HTML string to convert.");
                        return null;
                    }

                    // Convert the specified HTML string.
                    c.ConvertStringToImage(HtmlString, HtmlStringBaseUrl, outputPath);
                }
            }
            catch (Exception ex)
            {
                ShowError(ex.Message);
            }

            return outputPath;
  • VB Version:
        If Width <= 0 OrElse Height < 0 Then
                ShowError("Invalid Width or Height")
                Return Nothing
            End If

            If ConversionDelay < 0 Then
                ShowError("Invalid conversion delay")
                Return Nothing
            End If

            ' Set the output path.
            Dim outputPath = Path.Combine(OutputDir, Me.GetType().Name & "." & ImageFormat)
            If File.Exists(outputPath) Then
                File.Delete(outputPath)
            End If

            ' Create a new converter.
            Dim c = New HtmlToPdfConverter() With {.ViewportWidth = Width, .ViewportHeight = Height, .ImageFormat = ImageFormat, .ImageCrop = Crop, .ImageQuality = Quality, .Delay = ConversionDelay, .Timeout = Timeout}

            If Progress IsNot Nothing Then
                AddHandler c.Progress, Progress
            End If

            Try
                If ConvertUrl Then
                    If String.IsNullOrWhiteSpace(Url) Then
                        ShowError("Please specify the URL to convert.")
                        Return Nothing
                    End If

                    ' Convert URL
                    c.ConvertToImage(Url, outputPath)
                Else
                    If String.IsNullOrWhiteSpace(HtmlString) Then
                        ShowError("Please specify the HTML string to convert.")
                        Return Nothing
                    End If

                    ' Convert the specified HTML string.
                    c.ConvertStringToImage(HtmlString, HtmlStringBaseUrl, outputPath)
                End If
            Catch ex As Exception
                ShowError(ex.Message)
            End Try

            Return outputPath

And finally, the full source code of this example:

The complete source code of this example is also available in our PDF package.

45-Day Money Back Guarantee

We will refund your full money in 45 days
if you are not satisfied with our products

Buy Now
You have successfully subcribed to our mailing list.
Dont miss out Get update on new articles and other opportunities Subscribe