2011-03-03 66 views
3

在我们的网站中,我们嵌入了PDF,并且我们希望确保当用户单击PDF内部的链接时,它会在新的选项卡或窗口中打开。我们无法控制PDF,所以我们无法对链接本身做任何事情。强制链接在新窗口中打开

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

回答

4

没有抱歉,没有办法做到这一点。

这是设计。

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

0

这可能是有趣的:JS to open pdf's in new window?我假设你至少可以在页面中添加JavaScript。您不必为了在新窗口中打开PDF而自行修改PDF,即使您可能无法修改网址本身,也应该可以自由地以您想要的方式打开这些链接。或者我完全误解你的问题? :)

+0

这不是我所期待的。 PDF嵌入在网页中,不应在其他窗口中打开。相反,如果PDF中存在链接并且用户单击该链接,则应在单独的窗口中打开目标网址。 – taral 2011-03-03 09:36:07

+0

@taral,啊,现在我明白了。感谢您澄清这个小小的误解(例如我缺乏阅读理解技巧)。 :) – Henric 2011-03-03 09:52:52

1

检查this其他SO问题。它说,如果不修改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://stackoverflow.com/a/8141831/596758

相关问题