2013-09-23 275 views
0

我知道這個問題看起來很簡單,但它讓我頭痛。 我有一個程序,我想從哪裏顯示一個PDF,這工作正常和花花公子 ,但是當我想改變PDF時,它不會工作。 該應用程序有兩個兩個tabpages,一個用於PDF,另一個用於設計等。 我可以點擊打印並創建PDF並加載。 當更改pdf時,因爲我在第一個選項卡中更改了某些內容,所以它不會刪除舊的,並且不會加載新的pdf文件。C#在Webbrowser中打開PDF

這裏是我的代碼:

//The PdfFile variable is the path to the old pdf file 
//and file variable is the path to the new pdf file 
if (wbPdfViewer != null) 
{ 
    wbPdfViewer.AllowNavigation = true; 
    wbPdfViewer.Navigate(new Uri("http://www.google.de")); //this should navigate 
    wbPdfViewer.Url = new Uri("http://www.google.de"); //this is just a try 
    bool isallowed = wbPdfViewer.AllowNavigation; //check during debbuging if it is set 
    string url = wbPdfViewer.Url.ToString(); //see if it works during debbuging 
} 

if (pdfViewer.Document != null) //this is an optional viewer ... nevermind that 
{ 
    pdfViewer.Document.Close(); 
    pdfViewer.Document.Dispose(); 
} 

try 
{ 
    DHIO.File.Delete(PdfFile); 
} 
catch(Exception ex) 
{ 
    #if DEBUG 
     MessageBox.Show("PdfViewer 02002: Couldn't delete PDF file"); 
    #endif 
} 

if (wbPdfViewer != null) 
{ 
    GC.Collect(); 
    if (string.IsNullOrEmpty(file) || !DHIO.File.Exists(file)) 
     return; 

    wbPdfViewer.Navigate(new Uri(file)); 
    ShowNavigationControls(); 
    return; 
} 

正如你可以看到我想要刪除的PdfFile,它不應該是訪問,因爲我改變了頁面(在以後的版本google.de被替換約:空白或其他)。

所以我的問題是如何更改網址,以便我的程序不再訪問PDF文件? 我嘗試了與navigationComplete事件但這不會提前

+0

我忘了提及PDF在Adobe Acrobat Reader中打開。 – Bongo

+1

當您從PDF導航到www.google.de時,導航不會立即發生。在嘗試刪除PDF之前,請等待[導航](http://msdn.microsoft.com/zh-cn/library/system.windows.forms.webbrowser.navigated.aspx)事件。 – Noseratio

回答

1

,並一如既往地感謝設置wbPdfViewer.Urlnull

+0

這沒有奏效。該URL仍然設置爲我的臨時目錄中的pdf – Bongo