Cómo convertir HTML a PDF mediante iTextSharp

Autor: Peter Berry
Fecha De Creación: 11 Agosto 2021
Fecha De Actualización: 1 Mes De Julio 2024
Anonim
Cómo convertir HTML a PDF mediante iTextSharp - Artículos
Cómo convertir HTML a PDF mediante iTextSharp - Artículos

Contenido

ITextSharp es parte de iText, una biblioteca Java de código libre para la generación de PDF. Se ha desarrollado en C # para la plataforma .NET. Los documentos PDF son documentos de tamaño fijo que se utilizan frecuentemente para proporcionar contenido de impresión en sitios Web.


instrucciones

Convertir un documento HTML a PDF (archivo de imagen de Alex White de Fotolia.com)
  1. Abra un editor de C #.

  2. Cree un archivo C # y añada el código siguiente.

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

    using iTextShart.text; using iTextSharp.text.pdf;

  4. Llame a la clase embudida en iTextSharp y configure el StringBuilder como vacío:

    Document document = new Document (PageSize.A4, 80, 50, 30, 65); StringBuilder strData = new StringBuilder (string.Empty);

  5. Agregue una ruta de acceso al HTML que se generará del contenido de GridView:

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

  6. Establezca la ruta para la construcción del archivo PDF:

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


  7. Llame a los datos del archivo HTML y procese 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 = new StreamWriter (strHTMLpath, false, Encoding.UTF8); strWriter.Write ( "> "+ htw.InnerWriter.ToString () +" "); strWriter.Close (); strWriter.Dispose ();

  8. Utilice el "parser" para convertir el contenido del HTML a un PDF:

    iTextSharp.text.html.simpleparser. StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet (); styles.LoadTagStyle ("ol", "leading", "16,0"); PdfWriter.GetInstance (document, new FileStream (strPDFpath, FileMode.Create)); document.Open ();


  9. Configure las fuentes para los elementos de la página y añada elementos a ella:

    ArrayList objects; styles.LoadTagStyle ("li", "cara", "garamond"); style.LoadTagStyle ("span", "size", "8px"); style.LoadTagStyle ("body", "font-family", "times new roman"); style.LoadTagStyle ("body", "font-size", "12px"); document.NewPage (); objects = iTextSharp.text.html.simpleparser. HTMLWorker.ParseToList (new StreamReader (strHTMLpath, Encoding.Default), styles); (int k = 0; k <objects.Count; k ++) {document.Add ((IElement) objects [k]); }

  10. Limpie las variables de la memoria y cierre los archivos:

    {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 (); (File.Exists (Server.MapPath ("~ /" + strPDFpath))) {File.Delete (Server.MapPath ("~ /" + strPDFpath); }}}

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

Qué necesitas

  • Editor C #

Cómo quitar grapas de una silla

Louise Ward

Mes De Julio 2024

La grapa e utilizan a menudo para aegurar la tela a la bae de la illa tapizada. i etá penando en cambiar el revetimiento de la illa, neceitará aber cómo quitar eto clip in dañarlo....

Lo reloje de bolillo etán diponible en diferente tamaño y formato y alguno on antiguo. A lo largo de lo año, diferente relojero han inventado diferente mecanimo para ujetar la tapa trae...

Asegúrate De Leer