pdf文件中的相对文件链接

时间:2013-07-23 02:37:45

标签: pdf abcpdf html-to-pdf

我正在创建一个单独的pdf文件,我想链接到与pdf相同的目录中的其他文件。

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

我希望main.pdf有链接,这会导致pdf上的默认程序打开其他pdf文件。

当我在服务器上生成这些文件然后将它们下载到客户端时,我无法使用绝对链接,因为客户端PC上不存在这些链接。

首先,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中打开,其中url“file:///test.pdf”无法解析为任何内容。

有关如何使其工作的任何想法,或者甚至可以在pdfs中进行这种链接吗?

2 个答案:

答案 0 :(得分:2)

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

是的,确实如此。我用main.pdf创建了一个小测试,它有两个链接到同一文件夹中的另外两个PDF文档。我使用Acrobat手动创建了链接,并将启动操作与链接注释相关联。请参阅此处的内部结构:

enter image description here

这是带有主要加上两个辅助PDF的zip。请注意,您可以将它们复制到任何位置,相关链接仍然有效。 https://www.dropbox.com/s/021tvynkuvr63lv/main.zip

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

答案 1 :(得分:0)

所以我最终得到了它,感谢@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上与其关联的查看器中打开每个链接。

相关问题