简体   繁体   中英

How do I display a large pdf in a c# application?

I have a windows c# application and I want to display a pdf file, located on a webserver, in an acrobat com object added to my form.

pdf.loadfile(@"http://somewhere.com/nowwhere.pdf")

As my pdf is large, the application seems to hang till the entire file is loaded.

I want to read the large file without the user being under the perception that the application is hung.

I would do the following:

  1. Create another thread to import the pdf.
  2. Display some kind of a progress bar to the user. perhaps with a cancel button.

Sorry, don't have any code, but this article on asynchronous file I/O might be of help to you:

http://www.devsource.com/c/a/Using-VS/Making-Asynchronous-File-IO-Work-in-NET-A-Survival-Guide/

Three suggestions.

the first would be to break it and load a page at a time like some online books do. This is a bit annoying but would save you the loading time. I think ItextSharp has some functionality to do this.

Second try compression. again itextsharp has tools that allow for this

My third suggestion would be to check out This thread . choose a few nerdy loading phrases and use an animated gif to distract your client from the long loading time. Obviously this is a last resort, but could useful.

Use a Worker Thread. (BackgroundWorker for example)
MSDN Link to BackgroundWorker

如果必须从UI线程运行PDF.LoadFile,则可以使用HttpWebRequest将文件下载到BackgroundWorker中,将其保存在本地,然后在调用(UI线程)函数中调用pdf.loadfile()。

Obviously, if you can use a background thread to do the loading - you'd be all set:

Pdf pdf;

void ShowPdf() {
   if (this.InvokeRequired) {
     this.Invoke(() => this.ShowPdf());
   }
   // give pdf a window...
}

void LoadPdf() {
   System.Threading.ThreadPool.QueueUserWorkItem(() => {
      pdf.LoadFile("http://example.com/somelarge.pdf");
      this.ShowPdf();
   });
}

The issue that may come up there (I've never worked with Acrobat's COM PDF viewer) is that the PDF object expects to be on the UI thread. In that case, you'll end up with issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM