Hướng dẫn convert html to pdf using vb net - chuyển đổi html sang pdf bằng vb net

Mẫu này cho thấy cách sử dụng Trình chuyển đổi chọn lọc HTML sang PDF để chuyển đổi mã HTML thô thành PDF, cũng đặt một vài thuộc tính.

Kích thước trang PDF:

Định hướng trang PDF:

Web Width: PX
px

Trang web Chiều cao: PX (để trống để phát hiện tự động)
px
(leave empty to auto detect)


Mã mẫu vb.net

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports SelectPdf

Namespace SelectPdf.Samples
    Partial Public Class convert_html_code_to_pdf
        Inherits System.Web.UI.Page

        Protected Sub Page_Load(sender As Object, e As EventArgs)
            If Not IsPostBack Then
                TxtHtmlCode.Text = "<html>" & vbCr & vbLf & " <body>" & vbCr & vbLf & _
                    "  Hello World from selectpdf.com." & vbCr & vbLf & " </body>" & _
                    vbCr & vbLf & "</html>" & vbCr & vbLf
            End If
        End Sub

        Protected Sub BtnCreatePdf_Click(sender As Object, e As EventArgs)
            ' read parameters from the webpage
            Dim htmlString As String = TxtHtmlCode.Text
            Dim baseUrl As String = TxtBaseUrl.Text

            Dim pdf_page_size As String = DdlPageSize.SelectedValue
            Dim pageSize As PdfPageSize = DirectCast([Enum].Parse(GetType(PdfPageSize), _
                    pdf_page_size, True), PdfPageSize)

            Dim pdf_orientation As String = DdlPageOrientation.SelectedValue
            Dim pdfOrientation As PdfPageOrientation = DirectCast( _
                    [Enum].Parse(GetType(PdfPageOrientation), _
                    pdf_orientation, True), PdfPageOrientation)

            Dim webPageWidth As Integer = 1024
            Try
                webPageWidth = Convert.ToInt32(TxtWidth.Text)
            Catch
            End Try

            Dim webPageHeight As Integer = 0
            Try
                webPageHeight = Convert.ToInt32(TxtHeight.Text)
            Catch
            End Try

            ' instantiate a html to pdf converter object
            Dim converter As New HtmlToPdf()

            ' set converter options
            converter.Options.PdfPageSize = pageSize
            converter.Options.PdfPageOrientation = pdfOrientation
            converter.Options.WebPageWidth = webPageWidth
            converter.Options.WebPageHeight = webPageHeight

            ' create a new pdf document converting an url
            Dim doc As PdfDocument = converter.ConvertHtmlString(htmlString, baseUrl)

            ' save pdf document
            doc.Save(Response, False, "Sample.pdf")

            ' close pdf document
            doc.Close()
        End Sub
    End Class
End Namespace

Với Gembox.document, bạn có thể dễ dàng chuyển đổi các tệp từ định dạng này sang định dạng khác, chỉ sử dụng mã C# hoặc VB.NET. Bạn có thể tải hoặc đọc bất kỳ định dạng tệp đầu vào được hỗ trợ nào và lưu hoặc ghi nó dưới dạng bất kỳ định dạng tệp đầu ra được hỗ trợ nào. Danh sách đầy đủ các định dạng có sẵn trên trang trợ giúp định dạng tệp được hỗ trợ.C# or VB.NET code. You can load or read any supported input file format and save or write it as any supported output file format. The complete list of formats is available on the Supported File Formats help page.

Đối với cả đọc và viết, bạn có thể cung cấp đường dẫn của tệp hoặc luồng bằng cách sử dụng một trong các phương thức DocumentModel.Load hoặc DocumentModel.Save. Ngoài ra, để tiện lợi, Gembox.Document hỗ trợ đọc các tệp đầu vào từ URL, để bạn có thể tải tệp HTML của mình từ đường dẫn cục bộ hoặc từ xa.

Ví dụ sau đây cho thấy cách bạn có thể chuyển đổi tệp HTML thành PDF và chỉ định một số tùy chọn trang.convert an HTML file to a PDF and specify some page options.

Hướng dẫn convert html to pdf using vb net - chuyển đổi html sang pdf bằng vb net
Ảnh chụp màn hình HTML đầu vào và PDF đầu ra được chuyển đổi

using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Load input HTML file.
        DocumentModel document = DocumentModel.Load("%InputFileName%");

        // When reading any HTML content a single Section element is created.
        // We can use that Section element to specify various page options.
        Section section = document.Sections[0];
        PageSetup pageSetup = section.PageSetup;
        PageMargins pageMargins = pageSetup.PageMargins;
        pageMargins.Top = pageMargins.Bottom = pageMargins.Left = pageMargins.Right = 0;

        // Save output PDF file.
        document.Save("Output.%OutputFileType%");
    }
}

Imports GemBox.Document

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        ' Load input HTML file.
        Dim document As DocumentModel = DocumentModel.Load("%InputFileName%")

        ' When reading any HTML content a single Section element is created.
        ' We can use that Section element to specify various page options.
        Dim section As Section = document.Sections(0)
        Dim pageSetup As PageSetup = section.PageSetup
        Dim pageMargins As PageMargins = pageSetup.PageMargins
        With pageMargins
            .Left = 0
            .Right = 0
            .Top = 0
            .Bottom = 0
        End With

        ' Save output PDF file.
        document.Save("Output.%OutputFileType%")

    End Sub
End Module

Kiểu HTML và phông chữ trong PDF

Gembox.Document hỗ trợ kiểu dáng nội tuyến, biểu định kiểu bên trong và bên ngoài. Nó sử dụng một tập hợp con của các thuộc tính CSS và một số thuộc tính tùy ý bổ sung từ Microsoft Word (như mso-paginationmso-rotate). Nó cũng sử dụng quy tắc truyền thông loại in (ví dụ: @media print { ... }).

Để có được chuyển đổi PDF chính xác nhất, bạn nên cung cấp các trang HTML thân thiện với Gembox.Document. Nói cách khác, nội dung và cấu trúc của trang web của bạn nên được tối ưu hóa lý tưởng để in.

Thường có sự khác biệt khi nhắm mục tiêu phương tiện loại ____10 hoặc

using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Load input HTML file.
        DocumentModel document = DocumentModel.Load("%InputFileName%");

        // When reading any HTML content a single Section element is created.
        // We can use that Section element to specify various page options.
        Section section = document.Sections[0];
        PageSetup pageSetup = section.PageSetup;
        PageMargins pageMargins = pageSetup.PageMargins;
        pageMargins.Top = pageMargins.Bottom = pageMargins.Left = pageMargins.Right = 0;

        // Save output PDF file.
        document.Save("Output.%OutputFileType%");
    }
}
1, đó là lý do tại sao thông thường là thêm một kiểu dáng in riêng vào HTML sau bảng kiểu tiêu chuẩn (ví dụ:
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Load input HTML file.
        DocumentModel document = DocumentModel.Load("%InputFileName%");

        // When reading any HTML content a single Section element is created.
        // We can use that Section element to specify various page options.
        Section section = document.Sections[0];
        PageSetup pageSetup = section.PageSetup;
        PageMargins pageMargins = pageSetup.PageMargins;
        pageMargins.Top = pageMargins.Bottom = pageMargins.Left = pageMargins.Right = 0;

        // Save output PDF file.
        document.Save("Output.%OutputFileType%");
    }
}
2). Ngoài ra, bạn có thể sử dụng quy tắc truyền thông loại in trong bảng kiểu hiện tại của bạn.

Lưu ý, khi chuyển đổi trang HTML thành tài liệu PDF, máy thực thi mã sẽ có các phông chữ được sử dụng trên trang web được cài đặt trên đó. Nếu không, bạn có thể cung cấp chúng dưới dạng phông chữ riêng hoặc là phông chữ nhúng.

Gembox.Document hỗ trợ đọc các tùy chọn trang khác nhau (như lề, kích thước và định hướng) và các kiểu trang (như đường viền và màu sắc) từ chính nội dung HTML, thông qua các thuộc tính

using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Load input HTML file.
        DocumentModel document = DocumentModel.Load("%InputFileName%");

        // When reading any HTML content a single Section element is created.
        // We can use that Section element to specify various page options.
        Section section = document.Sections[0];
        PageSetup pageSetup = section.PageSetup;
        PageMargins pageMargins = pageSetup.PageMargins;
        pageMargins.Top = pageMargins.Bottom = pageMargins.Left = pageMargins.Right = 0;

        // Save output PDF file.
        document.Save("Output.%OutputFileType%");
    }
}
3 hoặc
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Load input HTML file.
        DocumentModel document = DocumentModel.Load("%InputFileName%");

        // When reading any HTML content a single Section element is created.
        // We can use that Section element to specify various page options.
        Section section = document.Sections[0];
        PageSetup pageSetup = section.PageSetup;
        PageMargins pageMargins = pageSetup.PageMargins;
        pageMargins.Top = pageMargins.Bottom = pageMargins.Left = pageMargins.Right = 0;

        // Save output PDF file.
        document.Save("Output.%OutputFileType%");
    }
}
4 CSS.

Ngoài ra, gembox.document hỗ trợ tạo các yếu tố

using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Load input HTML file.
        DocumentModel document = DocumentModel.Load("%InputFileName%");

        // When reading any HTML content a single Section element is created.
        // We can use that Section element to specify various page options.
        Section section = document.Sections[0];
        PageSetup pageSetup = section.PageSetup;
        PageMargins pageMargins = pageSetup.PageMargins;
        pageMargins.Top = pageMargins.Bottom = pageMargins.Left = pageMargins.Right = 0;

        // Save output PDF file.
        document.Save("Output.%OutputFileType%");
    }
}
5 từ nội dung HTML. Nếu
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Load input HTML file.
        DocumentModel document = DocumentModel.Load("%InputFileName%");

        // When reading any HTML content a single Section element is created.
        // We can use that Section element to specify various page options.
        Section section = document.Sections[0];
        PageSetup pageSetup = section.PageSetup;
        PageMargins pageMargins = pageSetup.PageMargins;
        pageMargins.Top = pageMargins.Bottom = pageMargins.Left = pageMargins.Right = 0;

        // Save output PDF file.
        document.Save("Output.%OutputFileType%");
    }
}
6 là phần tử đầu tiên trong tệp HTML, thì nội dung của nó sẽ được đọc dưới dạng tiêu đề mặc định của tài liệu; Nếu
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Load input HTML file.
        DocumentModel document = DocumentModel.Load("%InputFileName%");

        // When reading any HTML content a single Section element is created.
        // We can use that Section element to specify various page options.
        Section section = document.Sections[0];
        PageSetup pageSetup = section.PageSetup;
        PageMargins pageMargins = pageSetup.PageMargins;
        pageMargins.Top = pageMargins.Bottom = pageMargins.Left = pageMargins.Right = 0;

        // Save output PDF file.
        document.Save("Output.%OutputFileType%");
    }
}
7 là phần tử cuối cùng trong tệp HTML, thì nội dung của nó sẽ được đọc dưới dạng chân trang mặc định của tài liệu.

Ví dụ sau đây cho thấy cách bạn có thể tạo tệp PDF từ văn bản HTML, với các trang có hướng cảnh quan và các tiêu đề và chân trang lặp lại.

Hướng dẫn convert html to pdf using vb net - chuyển đổi html sang pdf bằng vb net
Ảnh chụp màn hình HTML được chuyển đổi thành PDF với các tiêu đề, chân trang và cảnh quan

using System.IO;
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var html = @"
<html>
<style>
  @page {
    size: A5 landscape;
    margin: 6cm 1cm 1cm;
    mso-header-margin: 1cm;
    mso-footer-margin: 1cm;
  }

  body {
    background: #EDEDED;
    border: 1pt solid black;
    padding: 20pt;
  }

  br {
    page-break-before: always;
  }

  p { margin: 0; }
  header { color: #FF0000; text-align: center; }
  main { color: #00B050; }
  footer { color: #0070C0; text-align: right; }
</style>

<body>
  <header>
    <p>Header text.</p>
  </header>
  <main>
    <p>First page.</p>
    <br>
    <p>Second page.</p>
    <br>
    <p>Third page.</p>
    <br>
    <p>Fourth page.</p>
  </main>
  <footer>
    <p>Footer text.</p>
    <p>Page <span style='mso-field-code:PAGE'>1</span> of <span style='mso-field-code:NUMPAGES'>1</span></p>
  </footer>
</body>
</html>";

        var htmlLoadOptions = new HtmlLoadOptions();
        using (var htmlStream = new MemoryStream(htmlLoadOptions.Encoding.GetBytes(html)))
        {
            // Load input HTML text as stream.
            var document = DocumentModel.Load(htmlStream, htmlLoadOptions);
            // Save output PDF file.
            document.Save("Output.%OutputFileType%");
        }
    }
}

Imports System.IO
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim html = "
<html>
<style>
  @page {
    size: A5 landscape;
    margin: 6cm 1cm 1cm;
    mso-header-margin: 1cm;
    mso-footer-margin: 1cm;
  }

  body {
    background: #EDEDED;
    border: 1pt solid black;
    padding: 20pt;
  }

  br {
    page-break-before: always;
  }

  p { margin: 0; }
  header { color: #FF0000; text-align: center; }
  main { color: #00B050; }
  footer { color: #0070C0; text-align: right; }
</style>

<body>
  <header>
    <p>Header text.</p>
  </header>
  <main>
    <p>First page.</p>
    <br>
    <p>Second page.</p>
    <br>
    <p>Third page.</p>
    <br>
    <p>Fourth page.</p>
  </main>
  <footer>
    <p>Footer text.</p>
    <p>Page <span style='mso-field-code:PAGE'>1</span> of <span style='mso-field-code:NUMPAGES'>1</span></p>
  </footer>
</body>
</html>"

        Dim htmlLoadOptions As New HtmlLoadOptions()
        Using htmlStream As New MemoryStream(htmlLoadOptions.Encoding.GetBytes(html))

            ' Load input HTML text as stream.
            Dim document = DocumentModel.Load(htmlStream, htmlLoadOptions)
            ' Save output PDF file.
            document.Save("Output.%OutputFileType%")

        End Using

    End Sub
End Module

Gembox.Document là một thành phần .NET cho phép bạn đọc, ghi, chỉnh sửa, chuyển đổi và in các tệp tài liệu từ các ứng dụng .NET của bạn bằng một API đơn giản. Làm thế nào về việc thử nghiệm nó ngày hôm nay?

Tải xuống Mua

Làm cách nào để tự động chuyển đổi HTML thành PDF?

Dưới đây chúng tôi chỉ ra cách chuyển đổi các trang web sang tài liệu PDF..
Bước 1: Dán URL trang web của bạn. Nhiều trang web có thể được chuyển đổi tại một thời điểm. Dán từng URL trên một dòng riêng ..
Bước 2: Lưu kết quả PDF. Nhấp vào Chuyển đổi HTML sang PDF và đợi cho đến khi xử lý hoàn tất. Sau đó nhấn tải xuống và lưu tài liệu PDF của bạn ..

Làm cách nào để lưu tệp VB Net dưới dạng PDF?

Cách lưu tài liệu ở định dạng PDF C# và VB.NET..
Lưu vào một tệp: // Định dạng tệp sẽ được phát hiện tự động từ phần mở rộng tệp: ".pdf".Lưu (@"d: \ book.pdf");....
Lưu vào luồng: // Hãy lưu tài liệu của chúng tôi vào bộ nhớ.Sử dụng (MemoryStream MS = new MemoryStream ()) {dc ..

Làm cách nào để chuyển đổi HTML thành PDF?

Chuyển đổi HTML thành PDF..
Tạo một thể hiện của lớp HTMLLoadOptions ..
Khởi tạo đối tượng tài liệu ..
Lưu tài liệu PDF đầu ra bằng cách gọi tài liệu.Phương thức lưu (chuỗi) ..

Làm cách nào để chuyển đổi HTML thành PDF trong Adobe Acrobat?

Cách chuyển đổi HTML thành PDF một cách nhanh chóng bằng cách sử dụng tiện ích mở rộng Acrobat cho trình duyệt web của bạn:..
Mở trang web HTML trong trình duyệt Microsoft, Google hoặc Mozilla của bạn ..
Chọn Chuyển đổi sang PDF trong thanh công cụ Adobe PDF ..
Đặt tên cho tệp và lưu tệp PDF mới trong vị trí mong muốn của bạn ..