“Conocimientos software>Formato de Documento Portátil

Cómo convertir HTML a PDF utilizando iTextSharp

2013/7/4
iTextSharp es parte de la biblioteca de Java de código abierto iText para la generación de PDF escrito en C # para la plataforma. . Los archivos PDF son documentos de tamaño fijo que se utilizan con frecuencia para proporcionar sitio web de contenido imprimible. Cosas que necesitará
C # editor
Mostrar más instrucciones
1

Abra un editor de C # .
2

Crear un archivo de C # y añadir . el siguiente código
3

Utilice un espacio de nombres para llamar a la biblioteca iTextSharp :

usando iTextSharp.text ;

usando iTextSharp.text.pdf ;
4

Llame una clase incorporada en iTextSharp y establecer el StringBuilder al vacío :

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

StringBuilder strData = new StringBuilder ( String.Empty ) ;
5

Añade una ruta de acceso para el código HTML que se generen a partir del contenido GridView :

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

defina la ruta para el archivo PDF para construir :

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

Llama a los datos desde el archivo HTML y hacer el archivo:

StringWriter sw = new htw = new HtmlTextWriter ( sw) ;

gvSerchResult.AllowPaging = false;

gvSerchResult.AllowSorting = false;

BindGridView ();

gvSerchResult . RenderControl ( HTW ) ;

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

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

strWriter.Close ();

strWriter.Dispose ( ) ;
8

Utilice el analizador de convertir el contenido HTML a PDF :

iTextSharp.text.html.simpleparser

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

styles.LoadTagStyle ( " mujeres de negocios" , "llevar ", " 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 la página :
< p > objetos ArrayList ;

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

styles.LoadTagStyle ( "span" , "tamaño" , " 8px ");

styles.LoadTagStyle ( "cuerpo", " font-family ", " times new roman" ) ;

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

document.NewPage ( ) .
objetos

= iTextSharp.text.html.simpleparser

HTMLWorker.ParseToList ( new 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 ( "~ /" + "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

Ejecute el archivo de C # para crear el archivo PDF desde el archivo HTML.

Formato de Documento Portátil
Cómo hacer un archivo PDF de varias páginas
Cómo convertir un mapa de bits a un PDF Vector PDF
Cómo desbloquear un PDF para imprimir
PDF a Mobi conversión
¿Cómo se imprime a un archivo Adobe PDF
La mejor manera de reducir el tamaño de un archivo PDF
¿Cómo se escribe un archivo de PDF
Cómo crear archivos PDF con PDFMaker MS Word
Conocimientos Informáticos © http://www.ordenador.online