强制链接在新窗口中打开

3
在我们的网站中,我们嵌入了PDF文件,并且希望确保当用户点击PDF内的链接时,它会在新标签页或窗口中打开。由于我们无法控制PDF文件本身,因此无法对链接进行任何更改。
是否有可能通过某种方式拦截请求,例如使用onbeforeunload,在新窗口中强制打开页面呢?
4个回答

4
抱歉,无法实现此操作。
这是设计上的限制。
如果可以实现,将会造成严重的安全漏洞。

1

根据Aaron Digulla的回答, 你不能在不修改PDF阅读器的情况下实现这一点。抱歉!

我认为你不能在不改变Acrobat Reader的情况下做到这一点...我建议使用一些不使用“单个文档”UI的其他PDF阅读器。[Foxit Reader]使用选项卡可以显示多个PDF文档。


1
你可以操作PDF文档中的链接,运行javascript以在新窗口/选项卡中打开链接。以下是我使用C#和iTextSharp实现的方法。
    public static MemoryStream OpenLinksInNewWindow(MemoryStream mySource)
    {
        PdfReader myReader = new PdfReader(mySource);

        int intPageCount = myReader.NumberOfPages;

        PdfDictionary myPageDictionary = default(PdfDictionary);
        PdfArray myLinks = default(PdfArray);

        //Loop through each page
        for (int i = 1; i <= intPageCount; i++)
        {
            //Get the current page
            myPageDictionary = myReader.GetPageN(i);

            //Get all of the annotations for the current page
            myLinks = myPageDictionary.GetAsArray(PdfName.ANNOTS);

            //Make sure we have something
            if ((myLinks == null) || (myLinks.Length == 0))
                continue;

            //Loop through each annotation

            foreach (PdfObject myLink in myLinks.ArrayList)
            {
                //Convert the itext-specific object as a generic PDF object
                PdfDictionary myLinkDictionary = (PdfDictionary)PdfReader.GetPdfObject(myLink);

                //Make sure this annotation has a link
                if (!myLinkDictionary.Get(PdfName.SUBTYPE).Equals(PdfName.LINK))
                    continue;

                //Make sure this annotation has an ACTION
                if (myLinkDictionary.Get(PdfName.A) == null)
                    continue;

                //Get the ACTION for the current annotation
                PdfDictionary myLinkAction = (PdfDictionary)myLinkDictionary.Get(PdfName.A);

                //Test if it is a URI action
                if (myLinkAction.Get(PdfName.S).Equals(PdfName.URI))
                {
                    //Replace the link to run a javascript function instead
                    myLinkAction.Remove(PdfName.F);
                    myLinkAction.Remove(PdfName.WIN);
                    myLinkAction.Put(PdfName.S, PdfName.JAVASCRIPT);
                    myLinkAction.Put(PdfName.JS, new PdfString(String.Format("OpenLink('{0}');", myLinkAction.Get(PdfName.URI))));
                }
            }
        }


        //Next we create a new document add import each page from the reader above
        MemoryStream myMemoryStream = new MemoryStream();

        using (Document myDocument = new Document())
        {
            using (PdfCopy myWriter = new PdfCopy(myDocument, myMemoryStream))
            {
                myDocument.Open();
                for (int i = 1; i <= myReader.NumberOfPages; i++)
                {
                    myWriter.AddPage(myWriter.GetImportedPage(myReader, i));
                }

                // Insert JavaScript function to open link
                string jsText = "function OpenLink(uri) { app.launchURL(uri, true); }";                 
                PdfAction js = PdfAction.JavaScript(jsText, myWriter);
                myWriter.AddJavaScript(js);

                myDocument.Close();
            }
        }                      

        return new MemoryStream(myMemoryStream.GetBuffer());
    }

我从这篇答案中得到了大部分代码:https://dev59.com/PGsz5IYBdhLWcg3wDjto#8141831


0

这个链接JS to open pdf's in new window是否对您有用?我假设您至少可以在页面上添加JavaScript。您不必修改PDF本身以便在新窗口中打开它们,即使您可能无法修改URL本身,您也应该可以自由地在页面上以任何方式打开这些链接。或者我完全误解了您的问题? :)


这不是我要找的。PDF应该嵌入网页中,不应在另一个窗口中打开。如果PDF中有链接且用户单击它,则应在单独的窗口中打开目标URL。 - taral
@taral,啊,现在我明白了。感谢您澄清这个小误解(例如我的阅读理解能力不足)。 :) - Henric

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接