English 中文(简体)
ABCpdf converting html anchors to jump to another page in the same PDF
原标题:

I m dynamically generating a PDF using ABCpdf which contains a table of contents that would link to other pages within the same PDF. The problem is that the path of the anchor tags in the HTML get changed to an absolute path to a temporary file.

For example, ABCpdf would render the link s href:

<a href="#elementId">Link</a>

in the PDF as: file:///C:/Users/Aaron/AppData/Local/Temp/ABCpdf/pdfCMMYPSF.htm#elementId

This is how I generate the PDF:

Doc pdf = new Doc();
pdf.HtmlOptions.AddLinks = true;
pdf.Rect.Rectangle = new System.Drawing.Rectangle(20, 80, 572, 702);
int id = pdf.AddImageHtml(pdfHTML, true, pdf.HtmlOptions.BrowserWidth, true);

while (pdf.Chainable(id))
{
    pdf.Page = pdf.AddPage();
    id = pdf.AddImageToChain(id);
}

pdf.HtmlOptions.LinkPages();
for (int i = 0; i < pdf.PageCount; i++)
{
    pdf.PageNumber = i;
    pdf.Flatten();
}

Any ideas how I can get the anchor links to render properly so clicking it will jump to another page?

最佳回答

Websupergoo got back to me and I was able to debug my problem from a sample project they provided. The solution to my problem was pretty simple, I ll post the answer here in case anyone else is having the same issue:

My HTML was set up like this:

<a href="#elementId">Link to another page</a>
<div id="elementId">A div that s on another page</div>

I simply needed to change it to:

<a href="#elementId">Link to another page</a>
<div><a name="elementId">A div that s on another page</a></div>

You need to use an anchor tag with the name specified in order for ABCpdf to make the link jump to another page within the same PDF.

问题回答

For what it s worth, I ve had inconsistant results via the AddImageHtml regarding anchored bookmarks. The most reliable way to accomplish this is via the AddBookMark method but this would involve significantly more work as you d be working to manually reconstruct the PDF contents and HTML support in that fashion is limited. More information regarding this method can be found at:

http://www.websupergoo.com/helppdf7net/source/5-abcpdf6/doc/1-methods/addbookmark.htm

Perhaps their latest version 8 addresses this issue. The bookmarks always seem to resolve to an absolute location rather than relative as you see in your browser via conventional HTML.

Another method which has worked for me in the latest ABCpdf version (9) is to add a bookmark to each page in your document:

For i = 1 to pdf.PageCount

    pdf.PageNumber = i
    pdf.AddBookmark("Page " & i, True)

Next

Then where you want to insert a link you can reference the bookmark - in this case we create a table of contents by looping through each bookmark we ve created:

For Each bm As Bookmark In pdf.Bookmark

    toc &= "<Font annots= goto:" + bm.Page.PageNumber.ToString() + " >" & bm.Title & "</Font><br>"

Next

pdf.AddHtml(toc)

The Websupergoo team supplied me with some example code and that s what this is based off of - so thanks to them!





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签