Tecnología

Inicio

Cómo convertir HTML a PDF utilizando iTextSharp

Cómo convertir HTML a PDF utilizando iTextSharp


iTextSharp es parte del código abierto biblioteca de Java iText para la generación de PDF escrito en C # para la plataforma .NET. Archivos PDF son documentos que se utilizan con frecuencia para proporcionar sitio web de contenido imprimible de tamaño fijo.

Instrucciones

1 Abra un editor de C #.

2 Crear un archivo de C # y agregue el código siguiente.

3 Utilizar un espacio de nombres para llamar a la biblioteca iTextSharp:

utilizando iTextSharp.text;

utilizando iTextSharp.text.pdf;

4 Llamar a una clase A integrado en iTextSharp y establecer el StringBuilder para vaciar:

documento Documento nuevo Documento = (PageSize.A4, 80, 50, 30, 65);

StringBuilder strData = new StringBuilder (String.Empty);

5 Añadir un camino para el HTML que se genera a partir de contenidos GridView:

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

6 Establecer la ruta para el archivo PDF para construir:

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

7 Llame a los datos del archivo HTML y procesar el archivo:

StringWriter sw = new StringWriter ();

sw.WriteLine (Environment.NewLine);

sw.WriteLine (Environment.NewLine);

sw.WriteLine (Environment.NewLine);

sw.WriteLine (Environment.NewLine);

HtmlTextWriter htw = new HtmlTextWriter (SW);

gvSerchResult.AllowPaging = false;

gvSerchResult.AllowSorting = false;

BindGridView ();

gvSerchResult.RenderControl (HTW);

StreamWriter strWriter = nuevo StreamWriter (strHTMLpath, falsa, Encoding.UTF8);

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

strWriter.Close ();

strWriter.Dispose ();

8 Utilice el analizador para convertir el contenido HTML a PDF:

iTextSharp.text.html.simpleparser.

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

styles.LoadTagStyle ( "ol", "líder", "16,0");

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

document.Open ();

9 Establecer los estilos de fuente para los elementos de la página y añadir los elementos de página:

objetos ArrayList;

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

styles.LoadTagStyle ( "span", "tamaño", "8 píxeles");

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

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

document.NewPage ();

objetos = iTextSharp.text.html.simpleparser.

HTMLWorker.ParseToList (nuevo StreamReader (strHTMLpath, Encoding.Default), estilos);

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

{

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

}

10 Borrar todas las variables utilizadas en la memoria y cierre:

{

document.close ();

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

Response.ClearContent ();

Response.ClearHeaders ();

Response.AddHeader ( "Content-Disposition", "apego; nombre de archivo =" + strPDFpath);

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

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

Response.Flush ();

Response.Close ();

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

{

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

}

}

11 Ejecute el archivo de C # para crear el archivo PDF en el archivo HTML.