【问题标题】:Relative file links in pdf filespdf文件中的相对文件链接
【发布时间】:2013-07-23 02:37:44
【问题描述】:

我正在创建一个 pdf 文件,我想将它链接到与 pdf 位于同一目录中的其他文件。

即。

MyFolder
        |
        |-main.pdf
        |-myotherpdf.pdf
        |-myotherotherpdf.pdf

我希望 main.pdf 中的链接会导致 pdf 上的默认程序打开其他 pdf。

当我在服务器上生成这些文件,然后将它们提供给客户端下载时,我不能使用绝对链接,因为这些在客户端电脑上不存在。

所以首先 pdf 文件是否真的支持像这样的相对文件链接,我没有找到太多说明它们支持的任何一种方式。

另外,为了生成我的 pdf,我使用 abcpdf 并为其提供 html 以转换为 pdf。

为了尝试在 html 中生成正确的正确 url,我尝试了以下方法

<a href='test.pdf'>test pdf link to local file</a>
<a href='#test.pdf'>test pdf link to local file</a>
<a href='/test.pdf'>test pdf link to local file</a>
<a href='file:///test.pdf'>test pdf link to local file</a>
<a href='file://test.pdf'>test pdf link to local file</a>

它们中的大多数要么直接指向我生成 pdf 文档的位置(临时文件路径),要么链接悬停在 acrobat 中显示“file:///test.pdf”,但单击它会弹出一个警告对话框要求允许/拒绝,单击“允许”后会在 Firefox 中打开,网址为“file:///test.pdf”,但无法解析任何内容。

关于如何使其工作或这种链接是否可以在 pdf 中实现的任何想法?

【问题讨论】:

    标签: pdf abcpdf html-to-pdf


    【解决方案1】:

    我只能回答你的问题:PDF文件真的支持这样的相对文件链接吗?

    是的,确实如此。我用 main.pdf 创建了一个小测试,它有两个指向同一文件夹中其他两个 PDF 文档的链接。我使用 Acrobat 手动创建了链接,并将启动操作与链接注释相关联。看这里的内部结构:

    这是包含主要 PDF 和两个辅助 PDF 的 zip。请注意,您可以将它们复制到任何地方,并且相关链接仍然有效。 https://www.dropbox.com/s/021tvynkuvr63lv/main.zip

    我不确定您将如何使用 abcpdf 完成此操作,尤其是因为您正在从 HTML 转换,这可能会限制可用的 PDF 功能。

    【讨论】:

    • 这看起来很有希望,我一直在与 abcpdf 开发人员联系,并且有一个用于获取这种低级设置的 api,我只需要弄清楚如何使用它,我想您在此处显示的 pdf 信息将有很大帮助,作为实验让您知道
    • 你用什么app看结构,好像找不到
    • 它是一个内部工具(TallComponents)。
    • 链接失效
    • @Draex_ 我更新了链接。 (该文件仍然存在,但保管箱似乎已更改 URL。)
    【解决方案2】:

    感谢@Frank Rem 和 abcpdf 人员的帮助,我终于成功了

    代码如下

        foreach (var page in Enumerable.Range(0, doc.PageCount))
        {
            doc.PageNumber = page;
    
            var annotEnd = doc.GetInfoInt(doc.Page, "Annot Count");
    
            for (var i = 0; i <= annotEnd; ++i)
            {
                var annotId = doc.GetInfoInt(doc.Page, "Annot " + (i + 1));
    
                if (annotId > 0)
                {
                    var linkText = doc.GetInfo(annotId, "/A*/URI*:Text");
    
                    if (!string.IsNullOrWhiteSpace(linkText))
                    {
                        var annotationUri = new Uri(linkText);
    
                        if (annotationUri.IsFile)
                        {
                            // Note abcpdf temp path can be changed in registry so if this changes
                            // will need to rewrite this to look at the registry
                            // http://www.websupergoo.com/helppdfnet/source/3-concepts/d-registrykeys.htm
                            var abcPdfTempPath = Path.GetTempPath() + @"AbcPdf\";
    
                            var relativePath = annotationUri.LocalPath.ToLower().Replace(abcPdfTempPath.ToLower(), string.Empty);
    
                            // Only consider files that are not directly in the temp path to be valid files
                            // This is because abcpdf will render the document as html to the temp path
                            // with a temporary file called something like {GUID}.html
                            // so it would be difficult to tell which files are the document
                            // and which are actual file links when trying to do the processing afterwards
                            // if this becomes and issue this could be swapped out and do a regex on {GUID}.html
                            // then the only restriction would be that referenced documents cannot be {GUID}.html
                            if (relativePath.Contains("\\"))
                            {
                                doc.SetInfo(annotId, "/A*/S:Name", "Launch");
                                doc.SetInfo(annotId, "/A*/URI:Del", "");
                                doc.SetInfo(annotId, "/A*/F:Text", relativePath);
                                doc.SetInfo(annotId, "/A*/NewWindow:Bool", "true");
                            }
                        }
                    }
                }
            }
        }
    

    这将允许在 PC 上与其关联的查看器中打开每个链接。

    【讨论】:

      猜你喜欢
      • 2014-02-15
      • 2018-12-21
      • 2022-06-15
      • 2011-11-30
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多