0

Tengo una plantilla generada en html a la cual, mediante el uso de c# y replace voy reemplazando unos valores. Esta plantilla ya tiene sus márgenes y su estilo el cual como página html se ve bien, pero al convertirlo a pdf tengo el problema de que no me respeta el margen superior e inferior cuando tengo dos o más hojas.

Voy a poner una imagen para que quede más clarointroducir la descripción de la imagen aquí:

El código que estoy usando es uno que ya estaba en el proyecto y es este:

HtmlToPdf convertir = new HtmlToPdf();
PdfDocument doc_resumen = convertir.ConvertHtmlString(plantilla_resultado,"");

doc_resumen.DocumentInformation.Title = "Hoja Resumen";
doc_resumen.DocumentInformation.Author = "smiranda";
byte[] pdf = doc_resumen.Save()

Existe otra opción para generar un pdf o me está faltando algo?

Muchas Gracias

1 respuesta 1

0

Podrías utilizar la biblioteca iTextSharp de la siguiente manera:

string codeHTML = '<html>...';
int marginLeft = 10;
int marginRight = 10;
int marginTop = 10;
int marginBottom = 10;

iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, marginLeft, marginRight, marginTop, marginBottom);
iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);
pdfDoc.Open();
string strContent = codeHTML;
var parsedHtmlElements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(strContent), null);
foreach (var htmlElement in parsedHtmlElements)
    pdfDoc.Add(htmlElement as iTextSharp.text.IElement);
pdfDoc.Close();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=file.pdf");
System.Web.HttpContext.Current.Response.Write(pdfDoc);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.