|
Rank: Member Groups: Member
Joined: 8/27/2012 Posts: 11
|
Hello, I am creating a pdf consisting of many records. I want a header on each page that shows the record id and the current page of the record. Here's a simplified version of what I'm doing to accomplish this:
Code: C#
doc = new PdfDocument();
for (var i = 0; i < records.Count; i++)
{
Record r = records[i];
string html = r.html;
string head = r.id + " {page_number}/{total_pages}";
HtmlToPdf.Options.HeaderHtmlFormat = head;
HtmlToPdf.ConvertHtml(html, doc);
}
doc.Save(pdf_file_path);
Converting each record is far too slow. I've found doing it all at once is very fast, but I can't find a way to set the header I want doing it all at once. Is there a way to set the header in the html? Should I try wrapping each record in a table with a repeating header? What is the preferred method for something like this? Thanks for your help!
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,195
|
Hi, No. You should not wrap them into a table. The easiest way to do this like this: 1. Put some sort of "marker" elements in your HTML. For example, you can do it like this:
Code: HTML/ASPX
<div id="record_10"></div>
<div>
the 10th records html goes here
</div>
2. Convert all records at once
Code: C#
//Convert all records but keeps the result object
HtmlToPdfResult result = HtmlToPdf.ConvertHtml(html_with_all_records, doc);
3. Run a loop like this:
Code: C#
for (int i = 0; i < record_count; i++)
{
//Get the marker element
HtmlElement e = result.HtmlToPdfDocument.GetElementById("records_" + i);
//Get the PdfPage object this marker is on
PdfPage page = e.Location.Page;
//Use ACM interface to add headers. Do not use HtmlToPdf here
.....
}
Note in step 3 you will need to use ACM interface (PDF Creator interface) instead of the HTML to PDF interface to add page headers. This is basically like putting the paper back to the printer again to print some additional information. ACM interface is much less powerful but much faster, which makes it a better choice for generating simple output such as page headers. You can take a look of the documentation and samples to find out how to use the ACM interface. Hope this helps. Please feel free to let us know if you have any more questions. Thanks!
|
|
Rank: Member Groups: Member
Joined: 8/27/2012 Posts: 11
|
I had another question, but think I figured it out, thank you.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,195
|
Hi,
I am not sure what else I can tell you. Our previous reply has already clearly explained to you how to do this. If you do not understand our previous reply, you can tell us which part you do not understand and we will try to explain it further.
Thanks
|
|
Rank: Member Groups: Member
Joined: 8/27/2012 Posts: 11
|
The issue in my orginal post was that I could not set the headers I wanted without calling ConvertHtml() for each record. The header should show "[record_page_number] / [total_pages_in_record]", which I figured out how to do this using your example to start with. In case anyone else is looking to do this, here's what I had to do...
Code: C#
for (int i = 0; i < record_count; i++)
{
//Get the marker element
HtmlElement e = result.HtmlToPdfDocument.GetElementById("records_" + i);
//Get the PdfPage object this marker is on
PdfPage page = e.Location.Page;
// Get the number of pages in this record
if (i < record_count-1)
{
//Get the page index of the next record marker
HtmlElement e = result.HtmlToPdfDocument.GetElementById("records_" + (i+1));
int next_page_index = next_e.Location.Page.Index;
int pages_in_this_record = next_page_index - page.Index;
}
else
{
int pages_in_this_record = doc.Pages.Count - page.Index;
}
for (int j = 0; j < pages_in_this_record; j++)
{
PdfPage cur_page = doc.Pages[page.Index+j];
string head = string.Format("{0} / {1}", j+1, pages_in_this_record);
//Use ACM interface to add headers to cur_page
.....
}
}
|
|