public bool OpenPdfFile(Stream stream)
        {
            //if (this.FileName == fileName)
            //{
            //    return true;
            //}

            CloseFile();

            pdfFile = PDFFile.Open(stream);
            if (pdfFile == null)
            {
                return(false);
            }

            if (pdfFile.PageCount == 0)
            {
                pdfFile.Dispose();
                pdfFile = null;
                return(false);
            }

            //this.FileName = fileName;

            double s = 96.0 / 72.0;

            for (int i = 0; i < pdfFile.PageCount; i++)
            {
                PDFSize     size = pdfFile.GetPageSize(i);
                PdfPageInfo page = new PdfPageInfo(i, (int)(size.Width * s), (int)(size.Height * s));
                this.Pages.Add(page);
            }

            double scale    = (double)this.Height / (double)this.Width;
            double pdfScale = this.Pages[0].PdfHeight / this.Pages[0].PdfWidth;

            if (scale < pdfScale)
            {
                SetScale((double)this.Height / this.Pages[0].PdfHeight);
            }
            else
            {
                SetScale((double)this.Width / this.Pages[0].PdfWidth);
            }

            MoveToPage(0);

            return(true);
        }
        void CreateControl(PdfPageInfo page, int scroll)
        {
            int top = page.Top - scroll;

            if (page.PageControl == null)
            {
                page.CreateControl(top);
                page.SetScale(this.PageScale);
                this.panelContent.Controls.Add(page.PageControl);
                page.PageControl.OnRender = this.RenderPage;
            }
            else
            {
                page.PageControl.Top  = top;
                page.PageControl.Size = page.Size;
            }
        }
        private void AddPages_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            try
            {
                _pdfRasterizer = new PdfRasterizer(_curFileNames[_curBackgroundLoadingFileIdx], POINTS_PER_INCH);
            }
            catch(Exception excp)
            {
                logger.Error("PDF Editor requires Ghostscript {0}", excp.Message);
                MessageBox.Show("PDF Editor requires Ghostscript to be installed");
                return;
            }

            try
            {
            int startNewPageNum = 1;
            int startNewFileNum = 1;
            int pageTotal = 0;
            GetFileAndPageOfLastOutDoc(out startNewFileNum, out startNewPageNum, out pageTotal);

                // Extract page images
            for (int i = 0; i < _pdfRasterizer.NumPages(); i++)
            {
                if ((worker.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }

                    System.Drawing.Image pageImg = _pdfRasterizer.GetPageImage(i + 1, false);

                    object[] args = new object[4];
                    args[0] = i;
                    args[1] = startNewPageNum;
                    args[2] = startNewFileNum;
                    args[3] = pageImg;

                    this.Dispatcher.BeginInvoke((Action<int, int, int, System.Drawing.Image>)delegate(int pageIdx, int startNewPgNum, int startNewFilNum, System.Drawing.Image pagImg)
                {
                        BitmapImage bitmap = ConvertToBitmap(pagImg);
                    PdfPageInfo pgInfo = new PdfPageInfo();
                        pgInfo.PageNum = pageIdx + 1;
                    pgInfo.FileIndex = _curBackgroundLoadingFileIdx;
                    pgInfo.ThumbBitmap = bitmap;
                    pgInfo.SplitAfter = false;
                    pgInfo.DeletePage = false;
                    pgInfo.PageRotation = 0;
                    pgInfo.ShowFileNum = (_curBackgroundLoadingFileIdx > 0);
                        pgInfo.NewDocPageNum = pageIdx + startNewPgNum;
                        pgInfo.NewDocFileNum = startNewFilNum;
                    _pdfPageList.Add(pgInfo);
                    }, args);
                Thread.Sleep(50);
                (sender as BackgroundWorker).ReportProgress(i * 100 / _pdfRasterizer.NumPages(), null);
            }
            }
            catch(Exception excp)
            {
                logger.Error("PDF Editor AddPages_DoWork failed excp {0}", excp.Message);
            }
            finally
            {
                // Close file
                _pdfRasterizer.Close();
                _pdfRasterizer = null;
            }
        }