-3

I am developing a windows application in .Net, in which I want to select a pdf from my computer and display it in the form. User can select some part of this pdf in the application and an image will be generated of the selected area.

I do not have any idea how to do this.

How do I read and display pdf and take screenshot of its content?

I have tried using the com component Acrobat Reader to read the pdf but it does not allow me to capture selected area using mouse.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Twix
  • 412
  • 1
  • 6
  • 17

1 Answers1

0

The easiest way wouldbe to convert pdf into a bitmap (oseries of bitmaps if the pdf is multi-paged) then display it. And when user selects some area - just cut the bitmap and save into the file.

You can find many examples how to convert pdf to bitmap:

and many more

EDIT:

This article seems to be very close to your problem: http://www.codeproject.com/Articles/37637/View-PDF-files-in-C-using-the-Xpdf-and-muPDF-libra

OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Portable Document Format (*.pdf)|*.pdf";
if (dlg.ShowDialog() == DialogResult.OK)
{
    _pdfDoc = new PDFLibNet.PDFWrapper();
    _pdfDoc.LoadPDF(dlg.FileName);
    _pdfDoc.CurrentPage = 1;

   PictureBox pic =new PictureBox();
   pic.Width=800;
   pic.Height=1024;
   _pdfDoc.FitToWidth(pic.Handle);
   pic.Height = _pdfDoc.PageHeight;
   _pdfDoc.RenderPage(pic.Handle);

   Bitmap _backbuffer = new Bitmap(_pdfDoc.PageWidth, _pdfDoc.PageHeight);
   using (Graphics g = Graphics.FromImage(_backbuffer))
   {
       _pdfDoc.RenderHDC(g.GetHdc);
       g.ReleaseHdc();
   }
   pic.Image = _backbuffer;
}  

Having the bitmap painted you can then draw on it (ie. for select range), cut and save to file as you wish.

Community
  • 1
  • 1
cyberhubert
  • 203
  • 1
  • 9
  • if the pdf contain so many pages then this will not be feasible solution i think – Twix Dec 22 '14 at 10:59
  • you can get page count from pdf, and then provide "pagination" feature in your UI - convert and display just current page or let's say 3 pages in advance – cyberhubert Dec 22 '14 at 11:06
  • These bitmaps will not be saved on system right? Can you provide me any example of this? – Twix Dec 22 '14 at 11:13
  • It depends which library you choose to perform the conversion. Most people use GhostScript callable exe, which produces bitmaps as files. For me there's nothing wrong to store some temp file if you free the space after use. Here is some complete working example: http://www.codeproject.com/Articles/41933/ASP-NET-PDF-Viewer-User-Control-Without-Acrobat-Re – cyberhubert Dec 22 '14 at 11:26
  • and here is sample even closer to your problem: http://www.codeproject.com/Articles/37637/View-PDF-files-in-C-using-the-Xpdf-and-muPDF-libra – cyberhubert Dec 22 '14 at 11:28
  • RenderHDC() does not exist in PDFLibNet library – Twix Dec 22 '14 at 11:51