|
Rank: Newbie Groups: Member
Joined: 9/28/2011 Posts: 3
|
Hi there,
We have a web application that generates an HTML content which is passed to EO.Pdf to be converted to a booklet. The booklet has different chapters and chapters vary in length for each user. Each chapter is separated from the next chapter with the following tag:
<div style="page-break-after:always"></div>
We need a mechanism to create a table of content for each user with accurate page numbers. For example the booklet for user 1 has a table of contents like this:
Chapter 1 ........... 3 Chapter 2 ........... 10 Chapter 3 ........... 24
But for user 2, the table of contents will display like this, because each chapter that is generated for user 2 has more content that user 1:
Chapter 1 ........... 5 Chapter 2 ........... 17 Chapter 3 ........... 38
What we are looking for is some reference tag that we can put in the generated HTML, like this:
Chapter 1 ........... {Page: #chapter_1} Chapter 2 ........... {Page: #chapter_2} Chapter 3 ........... {Page: #chapter_3}
And then we put unique tags at the beginning of each chapter:
{#chapter_1} <div style="page-break-after:always"></div> <h1>Chapter 1</h1> Some content for chapter 1 ...
{#chapter_2} <div style="page-break-after:always"></div> <h1>Chapter 2</h1> Some content for chapter 2 ...
{#chapter_3} <div style="page-break-after:always"></div> <h1>Chapter 3</h1> Some content for chapter 3 ...
Can you please let us know how this can be done using EO.Pdf?
Many thanks,
Milad
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,194
|
Hi, Yes. You can do that. What you will need to do is to use valid HTML tags as reference tag. For example,
Code: HTML/ASPX
<div id="chapter_1" />
<div style="page-break-after:always"></div>
<h1>Chapter 1</h1>
Some content for chapter 1 ...
Note it uses a valid DIV tag as the reference tag. You would then pass the HTML to ConvertHtml. The function returns a HtmlToPdfResult object:
Code: C#
//Convert the html and get the result
HtmlToPdfResult result = HtmlToPdf.ConvertHtml(your_html, doc);
The result object would allow you to look for your reference element:
Code: C#
//Get the reference DIV element
HtmlElement refDiv = result.HtmlDocument.GetElementById("chapter_1");
Use this refDiv you can get the page number this div is on:
Code: C#
//Get the page number of the reference DIV element
int pageIndex = refDiv.Location.Page.Index;
You can then use this information to generate your table of content. Hope this helps. Please feel free to let us know if you have any more questions. Thanks!
|
|
Rank: Newbie Groups: Member
Joined: 9/28/2011 Posts: 3
|
Thanks for the quick reply!
I followed your instructions, but I still have one issue: I could not find a way to set the value of an element in the "result" object. Here is what I tried to do:
HtmlToPdfResult result = HtmlToPdf.ConvertUrl(url, ms); HtmlElement refDiv = result.HtmlDocument.GetElementById("chapter_1"); int pageIndex = refDiv.Location.Page.Index; HtmlElement refTocDiv = result.HtmlDocument.GetElementById("Toc_chapter_1").InnerText = pageIndex;
But the last line is not valid because InnerText is read only.
Does it mean that I have to create the Table of Contents separately and then somehow insert it inside the PDF document (not sure how)? In this case, a page numbering reset will be needed and I end up with the same issue of numbering pages again.
Any suggestion is highly appreciated.
Thanks,
Milad
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,194
|
Hi,
The HtmlElement is readonly to you, it's returned to you AFTER the conversion is done, so there is no point for you to change anything because the conversion is already completed at that point.
Creating and render table of contents separately is precisely what you need to do. It will be something like this:
1. Run the table of contents with "fake" page numbers (for example, all 0s) into one PdfDocument object (let's call it doc1); 2. Run your main contents into another PdfDocument object (let's call it doc2); 3. Get the total page numbers from doc1 (you can use doc1.Pages.Count); 4. Get the page numbers for each chapter from doc2, offset this number with the total page number you got in step 3; 5. Re-run the table of contents with real page numbers into doc3;
Step 3 is optionally depending on how you wish to adjust your page numbers.
Now you can merge doc3 (the table of contents with actual page numbers) and doc2 (your main contents) this way:
PdfDocument doc4 = PdfDocument.Merge(doc3, doc2);
doc4 will be your final document.
Hope this helps. Please feel free to let us know if you still have any questions.
Thanks!
|
|
Rank: Newbie Groups: Member
Joined: 9/28/2011 Posts: 3
|
It worked perfectly for us. Many thanks!
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,194
|
Great to hear that! Please feel free to let us know if you have any more questions.
|
|