ABCpdf 转换 html 锚点以跳转到同一 PDF 中的另一个页面

发布于 2024-08-19 09:58:31 字数 795 浏览 8 评论 0原文

我使用 ABCpdf 动态生成 PDF,其中包含一个目录,该目录将链接到同一 PDF 中的其他页面。问题是 HTML 中锚标记的路径更改为临时文件的绝对路径。

链接的 href: 呈现

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

,ABCpdf 会将PDF 中的

例如 为:file:///C:/Users/Aaron/AppData/Local/Temp/ABCpdf/pdfCMMYPSF.htm#elementId我生成 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();
}

有什么想法可以让锚链接正确呈现,以便单击它会跳转到另一个页面吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清眉祭 2024-08-26 09:58:31

Websupergoo 回复了我,我能够从他们提供的示例项目中调试我的问题。我的问题的解决方案非常简单,我将在这里发布答案,以防其他人遇到同样的问题:

我的 HTML 设置如下:

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

我只需将其更改为:

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

您需要使用锚标记指定的名称,以便 ABCpdf 使链接跳转到同一 PDF 中的另一个页面。

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.

薯片软お妹 2024-08-26 09:58:31

无论如何,我通过 AddImageHtml 关于锚定书签的结果不一致。实现此目的最可靠的方法是通过 AddBookMark 方法,但这会涉及更多的工作,因为您需要手动重建 PDF 内容,并且这种方式的 HTML 支持是有限的。有关此方法的更多信息,请访问:

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

也许他们的最新版本 8 解决了这个问题。书签似乎总是解析为绝对位置,而不是像您通过传统 HTML 在浏览器中看到的相对位置。

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.

薄暮涼年 2024-08-26 09:58:31

在最新的 ABCpdf 版本 (9) 中对我有用的另一种方法是向文档中的每个页面添加书签:

For i = 1 to pdf.PageCount

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

Next

然后在要插入链接的位置可以引用书签 - 在本例中我们创建一个目录通过循环浏览我们创建的每个书签:

For Each bm As Bookmark In pdf.Bookmark

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

Next

pdf.AddHtml(toc)

Websupergoo 团队为我提供了一些示例代码,这就是本文的基础 - 所以感谢他们!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文