Come convertire HTML a PDF Utilizzando iTextSharp

July 24

Come convertire HTML a PDF Utilizzando iTextSharp


iTextSharp è parte della libreria open source iText Java per la generazione di PDF scritto in C # per la piattaforma .NET. PDF sono documenti che sono spesso utilizzati per fornire sito web contenuto stampabile di dimensione fissa.

istruzione

1 Aprire un editor di C #.

2 Creare un file C # e aggiungere il seguente codice.

3 Utilizzare uno spazio dei nomi per chiamare la biblioteca iTextSharp:

utilizzando iTextSharp.text;

utilizzando iTextSharp.text.pdf;

4 Chiamare una classe integrato in iTextSharp e impostare il StringBuilder a vuoto:

documento Document = nuovo documento (PageSize.A4, 80, 50, 30, 65);

StringBuilder strData = new StringBuilder (string.Empty);

5 Aggiungere un percorso per il codice HTML da generare dal contenuto GridView:

stringa strHTMLpath = Server.MapPath ( "MyHTML.html");

6 Impostare il percorso per il file PDF di costruire:

stringa strPDFpath = Server.MapPath ( "MyPDF.pdf");

7 Chiamare i dati dal file HTML e rendere il file:

StringWriter sw = new StringWriter ();

sw.WriteLine (Environment.NewLine);

sw.WriteLine (Environment.NewLine);

sw.WriteLine (Environment.NewLine);

sw.WriteLine (Environment.NewLine);

HtmlTextWriter htw = new HtmlTextWriter (SO);

gvSerchResult.AllowPaging = false;

gvSerchResult.AllowSorting = false;

BindGridView ();

gvSerchResult.RenderControl (HTW);

StreamWriter strWriter = new StreamWriter (strHTMLpath, falso, Encoding.UTF8);

strWriter.Write ( "<html> <head> </ head> <body>" + htw.InnerWriter.ToString () + "</ body> </ html>");

strWriter.Close ();

strWriter.Dispose ();

8 Utilizzare il parser per convertire il contenuto HTML in un file PDF:

iTextSharp.text.html.simpleparser.

stili StyleSheet = new iTextSharp.text.html.simpleparser.StyleSheet ();

styles.LoadTagStyle ( "OL", "leader", "16,0");

PdfWriter.GetInstance (documento, new FileStream (strPDFpath, FileMode.Create));

document.Open ();

9 Impostare gli stili di carattere per gli elementi a pagina e aggiungere gli elementi della pagina:

oggetti ArrayList;

styles.LoadTagStyle ( "li", "faccia", "Garamond");

styles.LoadTagStyle ( "span", "dimensione", "8px");

styles.LoadTagStyle ( "corpo", "font-family", "Times New Roman");

styles.LoadTagStyle ( "corpo", "font-size", "12px");

document.NewPage ();

oggetti = iTextSharp.text.html.simpleparser.

HTMLWorker.ParseToList (new StreamReader (strHTMLpath, Encoding.Default), stili);

for (int k = 0; k <objects.Count; k ++)

{

document.Add ((IElement) oggetti [k]);

}

10 Cancella tutte le variabili utilizzate dalla memoria e vicino:

{

document.close ();

Response.Write (Server.MapPath ( "~ /" + strPDFpath));

Response.ClearContent ();

Response.ClearHeaders ();

Response.AddHeader ( "Content-Disposition", "attachment; filename =" + strPDFpath);

Response.ContentType = "application / octet-stream";

Response.WriteFile (Server.MapPath ( "~ /" + strPDFpath));

Response.Flush ();

Response.Close ();

if (File.Exists (Server.MapPath ( "~ /" + strPDFpath)))

{

File.Delete (Server.MapPath ( "~ /" + strPDFpath));

}

}

11 Eseguire il file C # per creare il file PDF dal file HTML.