强制链接在新窗口中打开

时间:2011-03-03 09:23:55

标签: javascript pdf hyperlink

在我们的网站中,我们嵌入了PDF,我们希望确保当用户点击PDF中的链接时,它会在新的标签页或窗口中打开。我们无法控制PDF,因此我们无法对链接本身做任何事情。

是否可以以某种方式拦截请求,例如使用onbeforeunload,并强制在单独的窗口中打开新页面?

4 个答案:

答案 0 :(得分:4)

不好意思,没有办法做到这一点。

这是设计的。

如果可能的话,这将是一次重大的安全漏洞。

答案 1 :(得分:1)

检查this其他问题。它说如果不修改PDF阅读器就无法实现这一点。遗憾!

答案 2 :(得分: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://stackoverflow.com/a/8141831/596758

答案 3 :(得分:0)

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

相关问题