2011-03-03 62 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

相關問題