Related Tags:
Asp.Net It was developed by Microsoft to allow programmers to build dynamic web sites, web applications and web services. It was first released in January 2002 with version 1.0 of the .NET Framework, and is the successor to Microsoft's Active Server Pages (ASP) technology. Learn More, C# C# (pronounced "C-sharp") is an object-oriented programming language from Microsoft that aims to combine the computing power of C++ with the programming ease of Visual Basic. C# is based on C++ and contains features similar to those of Java. Learn More, ASP.NET MVC The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc assembly. Learn More, MVC 4 It is a newer version of ASP.NET MVC,ASP.NET MVC 4 is a framework for building scalable, standards-based web applications using well-established design patterns and the power of ASP.NET and the .NET Framework. Learn More, iTextSharp iTextSharp is a free and open source assembly that helps to convert page output or HTML content in a PDF file. Learn More, pdf Portable Document Format (PDF) is an open standard for electronic document exchange maintained by the International Organization for Standardization (ISO). Learn More,

add header on every page while dynamically generate pdf from html using iTextSharp in asp.net(C#)?

Description:I have successfully generate the pdf from html using the following
Link
But now i need to generate header on every page of the dynamically generated pdf,I have seen some example to generate such header footer dynamically but most of the examples are in java, I need such implementation in C#.
Thanks in advance

Posted by: | Posted on: Sep 13, 2018

2 answers

Replies

4

For adding header on every page of the dynamically generated pdf you need to override "PdfPageEventHelper" class's "OnStartPage" method as shown in below code.(Note:if wanted to add footer as well you can similarly override "OnEndPage" method of the same class)

public class HeaderFooter : PdfPageEventHelper
{
public override void OnStartPage(PdfWriter writer, Document document)
{
var cssResolver = new StyleAttrCSSResolver();
XMLWorkerFontProvider fontProvider =
new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
//fontProvider.Register(HttpContext.Current.Server.MapPath("~/Tahoma Regular font.ttf"));
CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
// Pipelines
PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
HtmlPipeline html1 = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html1);
// XML Worker
XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(worker);
// you can place any Html header in the following line as string
p.Parse(new StringReader("<div style='font-size:30px;color:#ff0000;'>Page Header</div>"));
}
}

Now for your ease I have just updated the method you have used to generate PDF as follows.(Just updated two lines of code)

public MemoryStream GeneratePdf(string html)
{
var pdfDoc = new Document(PageSize.A3);
var memoryStream = new MemoryStream();
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, memoryStream);

pdfWriter.RgbTransparencyBlending = true;
//following two lines updated in the previous code
HeaderFooter myevent = new HeaderFooter();
pdfWriter.PageEvent = myevent;

pdfDoc.Open();
var cssResolver = new StyleAttrCSSResolver();
XMLWorkerFontProvider fontProvider =
new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
//Following line will be required in case of lanuguages other than english i.e I have added following for arabic font.
//fontProvider.Register(HttpContext.Server.MapPath("~/Tahoma Regular font.ttf"));
CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
// Pipelines
PdfWriterPipeline pdf = new PdfWriterPipeline(pdfDoc, pdfWriter);
HtmlPipeline html1 = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html1);
// XML Worker
XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(worker);
p.Parse(new StringReader(html));
pdfWriter.CloseStream = false;
pdfDoc.Close();
memoryStream.Position = 0;
return memoryStream;
}

Replied by: | Replied on: Sep 24, 2018



0

this code is not work

iTextSharp.tool.xml.exceptions.RuntimeWorkerException
HResult=0x80131500
Message=
Source=itextsharp.xmlworker
StackTrace:
at iTextSharp.tool.xml.XMLWorker.EndElement(String tag, String ns)
at iTextSharp.tool.xml.parser.XMLParser.EndElement()
at iTextSharp.tool.xml.parser.state.ClosingTagState.Process(Char character)
at iTextSharp.tool.xml.parser.XMLParser.ParseWithReader(TextReader reader)
at iTextSharp.tool.xml.parser.XMLParser.Parse(TextReader reader)
at iTextSharp.tool.xml.parser.XMLParser.Parse(Stream inp)
at ConsoleApp1.Program.Main(String[] args) in C:\Users\uC254621\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 54

This exception was originally thrown at this call stack:
[External Code]

Inner Exception 1:
PipelineException: iTextSharp.tool.xml.pipeline.html.HtmlPipeline cries, it cannot find it's own context.

Inner Exception 2:
NoCustomContextException: Exception of type 'iTextSharp.tool.xml.NoCustomContextException' was thrown.

Replied by: | Replied on: Sep 21, 2020



Reply
×

Code block Hyperlink bold Quotes block Upload Images

Preview