设置现有Pdf文档的BaseUrl [英] Set BaseUrl of an existing Pdf Document

查看:191
本文介绍了设置现有Pdf文档的BaseUrl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在使用iTextSharp设置BaseUrl时遇到问题。我们过去曾使用过Adobes Implementation,但是我们遇到了一些严重的性能问题。所以我们切换到了iTextSharp,它的速度提高了10倍。
Adob​​e使我们能够为每个文档设置基本URL。我们确实需要这个,以便在不同的服务器上部署我们的文档。但我们似乎找不到合适的代码来执行此操作。

We're having trouble setting a BaseUrl using iTextSharp. We have used Adobes Implementation for this in the past, but we got some severe performance issues. So we switched to iTextSharp, which is aprox 10 times faster. Adobe enabled us to set a base url for each document. We really need this in order to deploy our documents on different servers. But we cant seem to find the right code to do this.

此代码是我们与Adobe一起使用的代码:

This code is what we used with Adobe:

public bool SetBaseUrl(object jso, string baseUrl)
{
    try
    {
        object result = jso.GetType().InvokeMember("baseURL", BindingFlags.SetProperty, null, jso, new Object[] {baseUrl });
        return result != null;
    }
    catch
    {
        return false;
    }
}

许多解决方案描述了如何插入链接新的或空的文件。但是我们的文档已经存在,并且包含的​​不仅仅是文本。我们希望使用指向一个或多个其他文档的链接覆盖特定单词。因此,对我们来说非常重要的是我们可以在不访问文本本身的情况下插入链接。也许在这些单词的顶部放置一个框并设置其位置(因为我们知道单词在文档中的位置)

A lot of solutions describe how you can insert links in new or empty documents. But our documents already exist and do contain more than just text. We want to overlay specific words with a link that leads to one or more other documents. Therefore, its really important to us that we can insert a link without accessing the text itself. Maybe lay a box ontop of these words and set its position (since we know where the words are located in the document)

我们尝试了不同的实现,使用 setAction 方法,但它似乎无法正常工作。结果在大多数情况下,我们看到了框,但内部没有链接或与之关联。 (当我在框内点击时,光标没有改变也没有发生任何事情)

We have tried different implementations, using the setAction method, but it doesnt seem to work properly. The result was in most cases, that we saw out box, but there was no link inside or associated with it. (the cursor didn't change and nothing happend, when i clicked inside the box)

感谢任何帮助。

推荐答案

我给你举了几个例子。

首先,让我们来看看 BaseURL1 。在您的评论中,您提到了JavaScript,因此我创建了一个文档,我在其中添加了一个文档级JavaScript代码段:

First, let's take a look at BaseURL1. In your comment, you referred to JavaScript, so I created a document to which I added a snippet of document-level JavaScript:

writer.addJavaScript("this.baseURL = \"http://itextpdf.com/\";");

这在Adobe Acrobat中完美运行,但是当您在Adobe Reader中尝试此操作时,会出现以下错误:

This works perfectly in Adobe Acrobat, but when you try this in Adobe Reader, you get the following error:


NotAllowedError:安全设置阻止访问此属性或
方法。 Doc.baseURL:1:文档级别:0000000000000000

NotAllowedError: Security settings prevent access to this property or method. Doc.baseURL:1:Document-Level:0000000000000000

这与Acrobat的JavaScript参考一致,其中明确指出特殊需要权限来更改基本URL。

This is consistent with the JavaScript reference for Acrobat where it is clearly indicated that special permissions are needed to change the base URL.

因此,我没有按照建议的路径进行操作,而是咨询了ISO-32000-1(这是我要求你做的,但是......我在速度上打败了你。

So instead of following your suggested path, I consulted ISO-32000-1 (which was what I asked you to do, but... I've beaten you in speed).

我发现你可以添加一个 URI 字典到带有 Base 条目的目录。所以我写了第二个例子, BaseURL2 ,我将这个词典添加到PDF的根词典中:

I discovered that you can add a URI dictionary to the catalog with a Base entry. So I wrote a second example, BaseURL2, where I add this dictionary to the root dictionary of the PDF:

PdfDictionary uri = new PdfDictionary(PdfName.URI);
uri.put(new PdfName("Base"), new PdfString("http://itextpdf.com/"));
writer.getExtraCatalog().put(PdfName.URI, uri);

现在BaseURL适用于Acrobat和Reader。

Now the BaseURL works in both Acrobat and Reader.

假设您要将BaseURL添加到现有文档中,我写了 BaseURL3 。在此示例中,我们将相同的字典添加到现有PDF的根字典中:

Assuming that you want to add a BaseURL to existing documents, I wrote BaseURL3. In this example, we add the same dictionary to the root dictionary of an existing PDF:

PdfReader reader = new PdfReader(src);
PdfDictionary uri = new PdfDictionary(PdfName.URI);
uri.put(new PdfName("Base"), new PdfString("http://itextpdf.com/"));
reader.getCatalog().put(PdfName.URI, uri);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.close();

使用此代码,您可以更改指向index.php的链接( base_url.pdf )指向 http://itextpdf.com/index.php base_url_3.pdf )。

Using this code, you can change a link that points to "index.php" (base_url.pdf) into a link that points to "http://itextpdf.com/index.php" (base_url_3.pdf).

现在您可以用较便宜的iTextSharp许可证替换您的Adobe许可证;-)

Now you can replace your Adobe license with a less expensive iTextSharp license ;-)

这篇关于设置现有Pdf文档的BaseUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆