Το iTextSharp είναι μέρος της βιβλιοθήκης iText open source Java για δημιουργία PDF γραμμένης σε C# για την πλατφόρμα .NET. Τα PDFs είναι έγγραφα σταθερού μεγέθους που χρησιμοποιούνται συχνά για την παροχή εκτυπώσιμου περιεχομένου ιστοσελίδας.
using iTextSharp.text;using iTextSharp.text.pdf;Document document = new Document(PageSize.A4, 80, 50, 30, 65);StringBuilder strData = new StringBuilder(string.Empty);string strHTMLpath = Server.MapPath("MyHTML.html");string strPDFpath = Server.MapPath("MyPDF.pdf");StringWriter sw = new StringWriter();sw.WriteLine(Environment.NewLine); (x4)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("<html><head></head><body>" + htw.InnerWriter.ToString() + "</body></html>");strWriter.Close();strWriter.Dispose();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();ArrayList objects;styles.LoadTagStyle("li", "face", "garamond");styles.LoadTagStyle("span", "size", "8px");styles.LoadTagStyle("body", "font-family", "times new roman");styles.LoadTagStyle("body", "font-size", "12px");document.NewPage();objects = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StreamReader(strHTMLpath, Encoding.Default), styles);for (int k = 0; k < objects.Count; k++){document.Add((IElement)objects[k]);}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));}Πνευματικά δικαιώματα © Γνώση Υπολογιστών Όλα τα δικαιώματα διατηρούνται.