Steps to Render a PDF Page Image

  1. Import the trial Developer License:

    License.Import(new StreamReader(@”C:\Users\Joe\license.xml”));

  2. Locate the PDF files using OpenFileDialog:

    License.Import(new StreamReader(@”C:\Users\Joe\license.xml”));

  3. Define 2 variables to manage navigation of pages and 1 for a PdfDocument Object:

    int currentPage;
    IList pages;
    PdfDocument document;

  4. Create an OpenFileDialog that manages selection of files:

    OpenFileDialog OpFile = new OpenFileDialog();
    //show only PDF Files
    OpFile.Filter = “PDF Files (*.pdf)|*.pdf”;

    if (OpFile.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;

  5. Create a new PdfDocument Object:

    //Create a new PDFDocument object add the selected file.
    document = new PdfDocument();
    document.Path = OpFile.FileName;

    //Open the selected file
    document.Open();

    //Set the current page to 0
    currentPage = 0;

    // GetPages() traverses the PDF page tree so you really only want to do it once
    pages = document.GetPages();

    //Update the thumbnail area with whatever the current page is.
    UpdateThumbnail(currentPage);

  6. Create a UpdateThumbnail Method to manage User Interaction with the form:

    private void UpdateThumbnail(int index)
    {
    if ((index >= 0) && (index < pages.Count)) { currentPage = index; } //Only enable the buttons if there is a page to go to previous.Enabled = currentPage > 0;
    next.Enabled = currentPage < pages.Count – 1;

    //Call Invalidate to capture when the image changes
    pictureBox.Invalidate();

  7. Define the page navigation when the user clicks on the buttons:

    private void previous_Click(object sender, EventArgs e)
    {
    UpdateThumbnail(currentPage – 1);
    }

    private void next_Click(object sender, EventArgs e)
    {
    UpdateThumbnail(currentPage + 1);
    }

  8. Set up the Picture Box to display an image of the page that is selected:

    if (pages != null)
    {
    PdfPage page = pages[currentPage];
    // draw PDF page to bitmap in real time at 40 dpi
    Bitmap bitmap = page.DrawBitmap(40);

    // 40 dpi works because pages are typically 8.5 x 11 inches and our pictureBox is 512 x 512
    // 8.5 * 40 = 340 pixels
    // 11 * 40 = 440 pixels
    // both of these are less than 512
    }

  9. Create keyboard shortcut keys for the next and previous button:

    switch (e.KeyCode)
    {
    case Keys.PageUp:
    UpdateThumbnail(currentPage + 1); // next
    break;

    case Keys.PageDown:
    UpdateThumbnail(currentPage – 1); // previous
    break;

    case Keys.Home:
    UpdateThumbnail(0); // first
    break;

    case Keys.End:
    UpdateThumbnail(pages.Count – 1); // last
    break;
    }