We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Adding Javascript source link in footer of pdf.

Hi,

I am using asp.net 3.5 and C#. I am using Datatable to Pdf convertion functionality. I have a Url which I executes using Javascript "src" url and need to put this in footer of each page. This url randomly displays images on footer using javascript.

Can you please let me know how can we embedd javascript on footers of each page.

I am using Back Office | Essential PDF for ASP.NET

Early reply is appriciated as we need to finalize the product in 1-2 days.

Many Thanks
Raveesh

5 Replies

SS Sri Subhashini M Syncfusion Team July 16, 2009 01:24 PM UTC

Hi Raveesh,

Here is the answers for your questions,

Adding Footer to the PDF page

We can draw header for each PDF page by using the Top property of the PdfTemplate class. Kindly use the below code to draw the page template at top of the PDF page.

//Create a PDF document
PdfDocument doc = new PdfDocument();
// Add a new section
PdfSection sec = doc.Sections.Add();
//Create page template
PdfPageTemplateElement header = new PdfPageTemplateElement(rect);
//Add header template at the top.
sec.Template.Top = header;

Showing Table column header
We can show the table column by enabling the ShowHeader property of the PdfLightTable. Kindly look into the below code snippet to enable the ShowHeader property,

//Create a new table.
PdfLightTable table = new PdfLightTable();
table.Style.ShowHeader = true;

Javascript in Footer

I was able to understand your requirement which you mentioned here. We can execute the javascript using HtmlConverter by specifying the URL as like,

[HTML Code]
script language='javascript' type='text/javascript' src='http://www.beckett.com/banmanpro/Sequence2.aspx?ZoneID=226&CountImpressions=True&Total=4&SiteID=1&Horizontal=True&MaxColumns=4'

Please do find the below sample which demonstrates the above mentioned features and let me know if this helps,
http://files.syncfusion.com/samples/PDF.Web/57911/main.htm

Regards,
Suba


RK Raveesh Kumar July 17, 2009 06:50 AM UTC

Hi,
Thanks for the example.
The JavaScript link provided in page “HtmlTable.html”, if executed repeatedly, displays different set of images each time.
For example if we open the html page “HtmlTable.htm” provided in link “http://files.syncfusion.com/samples/PDF.Web/57911/main.htm”, in browser and hit refresh button, it will display new set of htperlinked images each time. What I want to accomplish is, that link provided in “HtmlTable.html” should be processed on each page so that it will display different images on each page footer.

As per example you have provided you have executed this.footer() function twice and hence both pages are displaying different set of images. But in my application numbers of pages are determined at run time only. Also all these images are hyperlinks which let users to click on this footer images to go to specified Url. This url is the main requirement for footer images.

So can you please help me, how can we put this link in each of the page footers, so that all pages will show different set of clickable image links.


SS Sri Subhashini M Syncfusion Team July 17, 2009 12:44 PM UTC

Hi Raveesh,

Drawing Different footers on PDF page

We can draw the generated images of the “HtmlTable.html” in every page dynamically by using the Pages_PageAdded event handler. The event Pages_PageAdded raised on adding a new page to the PDF document. Kindly refer the below code snippet which demonstrates drawing the different footers on each page,

void Pages_PageAdded(object sender, PageAddedEventArgs args)
{
PdfTemplate footer;
using (HtmlConverter html = new HtmlConverter())
{
// set Javascript
html.EnableJavaScript = true;
using (HtmlToPdfResult result = html.Convert(Server.MapPath('~/Data/HtmlTable.htm'), ImageType.Metafile, (int)doc.Pages[0].GetClientSize().Width, (int)220, AspectRatio.KeepWidth))
{
if (result.RenderedImage == null)
return;
PdfMetafile mf = new PdfMetafile(result.RenderedImage as Metafile);
//Create a page template
footer = new PdfTemplate(new SizeF(doc.Pages[0].GetClientSize().Width, 100));
footer.Graphics.DrawImage(PdfImage.FromImage(result.RenderedImage), new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 200));
}
}
args.Page.Graphics.DrawPdfTemplate(footer, new PointF(0, 670));
}

Images with Hyperlinks

We can achieve the behavior by using PdfUriAnnotation. We have to draw the URL in the specified page location. Kindly refer the below code snippet to draw the URL in the specified page location,

PdfUriAnnotation url2 = new PdfUriAnnotation(new RectangleF(240, 670, 170, 45));
url2.Uri = 'http://www.Yahoo.com';
url2.Border = new PdfAnnotationBorder(0, 0, 0);
url2.Color = new PdfColor(Color.White);
args.Page.Annotations.Add(url2);

Please do find the sample from the below specified location and let me know if you need any information,
http://files.syncfusion.com/samples/PDF.Web/57911_Modified/main.htm

Regards,
Suba


RK Raveesh Kumar July 21, 2009 02:55 PM UTC

Hi,
Thanks for your help.
One thing which i have noticed that, if I use PageAddedEventArgs to create new footers on each page, then pdf size incresed approximately 30 times the pdf, which was created without page PageAddedEventArgs.

Before using PageAddedEventArgs handler i was using
Template.OddBottom and Template.OddBottom.

I have also included
"doc.Compression = Syncfusion.Pdf.PdfCompressionLevel.Best;"
Still its size is 60 mb. which otherwise was of 2.6 mb.

Can you please let me know what could be the possible cause.

Many Thanks
Raveesh


SS Sri Subhashini M Syncfusion Team July 22, 2009 12:41 PM UTC

Hi Raveesh,

File size increased on drawing custom footer

Basically, the PdfPageTemplateElement is an common area which is shared by all the pages within the document. If we assign the footer to a PDF document using PdfPageTemplateElement, the footer information will be shared by all the pages of the PDF document. PDF document`s pages doesn`t contain information about its footer elements.

But according to your requirement, We are drawing our own footer kind of thing for each page externally, which is not an actual footer.

Since we have several images placed on the footers on each page, we could see this much difference in file size. This is not an issue. We can resolve this issue by making use of the document footers, but your specific requirement of having different footer on each page could not be achieved if we switch to this approach.

Please let me know if you need any information.

Regards,
Suba

Loader.
Live Chat Icon For mobile
Up arrow icon