public byte[] Render(string htmlText, string pageTitle, string linkCss, bool horizontal) { byte[] renderedBuffer; using (var outputMemoryStream = new MemoryStream()) { using (var pdfDocument = new Document(PageSize.A4, HorizontalMargin, HorizontalMargin, VerticalMargin, VerticalMargin)) { if (horizontal) { pdfDocument.SetPageSize(PageSize.A4.Rotate()); } PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, outputMemoryStream); pdfWriter.CloseStream = false; pdfWriter.PageEvent = new PrintHeaderFooter { Title = pageTitle }; pdfDocument.Open(); // register all fonts in current computer FontFactory.RegisterDirectories(); // Set factories var htmlContext = new HtmlPipelineContext(null); htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory()); // Set css ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false); cssResolver.AddCssFile(HttpContext.Current.Server.MapPath(linkCss), true); cssResolver.AddCss(".shadow {background-color: #ebdddd; }", true); //Export IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDocument, pdfWriter))); using (var xmlString = new StringReader(htmlText)) { var worker = new XMLWorker(pipeline, true); var xmlParse = new XMLParser(true, worker); xmlParse.Parse(xmlString); xmlParse.Flush(); } } renderedBuffer = new byte[outputMemoryStream.Position]; outputMemoryStream.Position = 0; outputMemoryStream.Read(renderedBuffer, 0, renderedBuffer.Length); } return(renderedBuffer); }
/* * (non-Javadoc) * * @see * com.itextpdf.tool.xml.ITagProcessor#content(com.itextpdf.tool.xml.Tag, * java.lang.String) */ public override IList <IElement> Content(IWorkerContext ctx, Tag tag, String content) { try { ICSSResolver cssResolver = GetCSSResolver(ctx); cssResolver.AddCss(content, false); } catch (CssResolverException e) { LOG.Error(LocaleMessages.GetInstance().GetMessage(LocaleMessages.STYLE_NOTPARSED), e); if (LOG.IsLogging(Level.TRACE)) { LOG.Trace(content); } } catch (NoCustomContextException) { LOG.Warn(String.Format(LocaleMessages.GetInstance().GetMessage(LocaleMessages.CUSTOMCONTEXT_404_CONTINUE), typeof(CssResolverPipeline).FullName)); } return(new List <IElement>(0)); }
public void print() { string rpthtml = bindheader(); // string rpthtml = ""; string filename = System.DateTime.UtcNow.ToFileTimeUtc() + ".pdf"; string html = readHTML(); html = rpthtml + html; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ContentType = "application/pdf"; ////define pdf filename HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + filename); //Generate PDF using (var document = new Document(iTextSharp.text.PageSize.B4)) { //define output control HTML var memStream = new MemoryStream(); TextReader xmlString = new StringReader(html); PdfWriter writer = PdfWriter.GetInstance(document, memStream); //open doc document.Open(); // register all fonts in current computer FontFactory.RegisterDirectories(); // Set factories var htmlContext = new HtmlPipelineContext(null); htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory()); // Set css ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false); //cssResolver.AddCss("th{background-color:#1caf9a;color:#ffffff;font-size:12px;padding:4px;}", true); cssResolver.AddCss("th{font-size:12px;padding:4px;color:#1caf9a;}", true); cssResolver.AddCss("td{font-size:12px;padding:4px;}", true); // cssResolver.AddCss("table{border-left: solid 1px #dadada;border-top:solid 1px #ffffff;}", true); cssResolver.AddCss("img{display:none;}", true); cssResolver.AddCss("a{color:#1caf9a;font-size:12px;}", true); cssResolver.AddCss("h3{font-size:15px;float:right;text-align:right;width:20%;font-waight:bold;border:2px solid #1caf9a;}", true); cssResolver.AddCss("h2{font-size:16px;text-align:left;font-waight:bold;color:#1caf9a;}", true); cssResolver.AddCss("h5{font-size:12px;}", true); if (Session["css"] != null) { if (Session["css"].ToString() == "invlist") { cssResolver.AddCss(".odd td{border-bottom:solid 1px #e0e0e0;padding:4px;}", true); cssResolver.AddCss(".gridheader th{border-bottom:solid 1px #e0e0e0;}", true); cssResolver.AddCss(".even td{border-bottom:solid 1px #e0e0e0;padding:4px;}", true); cssResolver.AddCss(".bordercss{border-left:solid 1px #e0e0e0;}", true); cssResolver.AddCss(".bordercss1{border-right:solid 1px #e0e0e0;}", true); } if (Session["css"].ToString() == "timeexp") { // cssResolver.AddCss(".aright{text-align:right;}", true); } } // Export IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer))); var worker = new XMLWorker(pipeline, true); var xmlParse = new XMLParser(true, worker); xmlParse.Parse(xmlString); xmlParse.Flush(); document.Close(); document.Dispose(); HttpContext.Current.Response.BinaryWrite(memStream.ToArray()); } HttpContext.Current.Response.End(); HttpContext.Current.Response.Flush(); }