|
Rank: Newbie Groups: Member
Joined: 7/4/2013 Posts: 9
|
I am currently evaluating the user of EO.Pdf. I am collating the HTML for a table of contents (TOC) as I go about converting multiple HTML parts into a PdfDocument using the ConvertHtml method. I create an empty page (except for a page heading) that is effectively a placeholder for where I will insert my table of contents. Then prior to saving the PdfDocument I insert the collated TOC HTML into the TOC page using the following code.
Code: C#
var _tocOutputArea = new RectangleF(1f, _tocStartPosition, 8.27f - 1f, 11.69f - 0.25f - _tocStartPosition);
var options = new HtmlToPdfOptions { OutputArea = _tocOutputArea, StartPosition = _tocStartPosition };
SetPageOptions();
var tocHtmlToPdfResult = HtmlToPdf.ConvertHtml(_tocHtml, tocPage, options);
This all works nicely, except when the TOC HTML does not fit onto the TOC page. In this case the TOC HTML just gets truncated at the end of the page. What I would like to happen is for the truncated TOC HTML to go onto a new page, similar to what happens if I use the ConvertHtml method on a PdfDocument with the HtmlToPdf.Options.Follow option set. How should I go about this?
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,195
|
Hi,
When you pass a PdfPage object to ConvertHtml, it only output on that page. In order for it to go onto a new page, you will need to pass a PdfDocument object to your ConvertHtml.
What you can do is, instead of creating an empty page for the TOC page, do not create any page for it at all. You would create a new PdfDocument object, then call ConvertHtml to write the TOC to the new PdfDocument object. Now you have two PdfDocument object: the TOC object and the main content object. You can then call PdfDocument.Merge to merge them into a single PdfDocument.
Please let us know if this works for you.
Thanks!
|
|
Rank: Newbie Groups: Member
Joined: 7/4/2013 Posts: 9
|
Your response set me on the right track. I ended up still creating the blank page, because I needed that for other reasons. Then I used split and merge to get the result I needed. See code below. I still have some more work to do now to fix up the page numbers and links in my TOC. Thanks for your help.
Code: C#
...........
// split the document
var splitDocs = _pdfDocument.Split(_tocPageIndex + 1);
// convert the TOC
SetHtmlToPdfOptions(_tocPageIndex, _tocStartPosition);
var tocHtmlToPdfResult = HtmlToPdf.ConvertHtml(_tocHtml, splitDocs[0]);
// merge the documents back together
SetHtmlToPdfOptions();
_pdfDocument = PdfDocument.Merge(splitDocs[0], splitDocs[1]);
...........
private void SetHtmlToPdfOptions(int startPageIndex, float startPosition)
{
HtmlToPdf.Options.StartPageIndex = startPageIndex;
HtmlToPdf.Options.StartPosition = startPosition;
SetHtmlToPdfOptions();
}
private void SetHtmlToPdfOptions()
{
HtmlToPdf.Options.PageSize = EO.Pdf.PdfPageSizes.A4;
HtmlToPdf.Options.AutoAdjustForDPI = false;
if (_lastHtmlToPdfResult != null) // this is not the first page
{
HtmlToPdf.Options.HeaderHtmlFormat = string.Format("<div style='text-align:center; font-size:small'>{0}</div><hr />", _document.Title);
}
}
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,195
|
Hi,
Glad that helped. If you want to do links, you have to use a single conversion because the converter won't create link from one PdfDocument to another PdfDocument. The basic steps are:
1. Create the full TOC html including all links, but WITHOUT the target page number (since you do not know the target page number at this point); 2. Add the full TOC html and your main content HTML together to create a single big HTML block; 3. Run the converter, this will produce a PdfDocument object that contains both the TOC and the main contents. The links in the TOC will be automatically handled. As such the only thing missing is the page numbers in the TOC; 4. Step 3 returns a HtmlToPdfResult object, from that object you can get a HtmlDocument object, you can then call one of those GetElementByXXX method to get the target elements. For example, if your "Chapter1" in your TOC should link to to a DIV with ID="Chatper1", then you can use GetElementById("Chatper1") to get that DIV element. If necessary, you will need to add "marker" elements in step 2 so that you can find the target element for each TOC link; 5. GetElementByXXX method returns a HtmlElement object. Use that object's Location property to get the page number; 6. Use this page number information to regenerate your TOC. This time the TOC has the page number; 7. Repeat step 2 and step 3. Now it will give you the final PdfDocument object with TOC links and page numbers;
Hope this makes sense to you. Please let us know if you still have any questions.
Thanks!
|
|