Пример #1
0
        private void button4_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image != null)
            {
                pictureBox1.Image.Dispose();
            }

            PictureBox pic = new PictureBox();

            pic.Width  = 800;
            pic.Height = 1024;
            _pdfDoc.FitToWidth(pic.Handle);
            pic.Height = _pdfDoc.PageHeight;
            _pdfDoc.RenderPage(pic.Handle);
            /*Added since 1.0.6.2*/
            _pdfDoc.CurrentX     = 0;
            _pdfDoc.CurrentY     = 0;
            _pdfDoc.ClientBounds = new Rectangle(0, 0, _pdfDoc.PageWidth, _pdfDoc.PageHeight);

            _backbuffer = new Bitmap(_pdfDoc.PageWidth, _pdfDoc.PageHeight);

            using (Graphics g = Graphics.FromImage(_backbuffer))
            {
                /*New thread safe method*/
                _pdfDoc.DrawPageHDC(g.GetHdc());
                g.ReleaseHdc();
            }
            pic.Dispose();

            pictureBox1.Image = _backbuffer;
        }
Пример #2
0
 private void GetImageFromPDF(int page, PictureBox pictureBox)
 {
     if (pdfDoc != null)
     {
         pdfDoc.CurrentPage = page;
         pdfDoc.CurrentX    = pdfDoc.CurrentY = 0;
         pdfDoc.RenderPage(pictureBox.Handle);
         pictureBox.Image    = new Bitmap(pdfDoc.PageWidth, pdfDoc.PageHeight);
         pdfDoc.ClientBounds = new Rectangle(0, 0, pdfDoc.PageWidth, pdfDoc.PageHeight);
         using (Graphics g = Graphics.FromImage(pictureBox.Image))
         {
             pdfDoc.DrawPageHDC(g.GetHdc());
             g.ReleaseHdc();
         }
     }
 }
Пример #3
0
        public void Append(byte[] bytes)
        {
            using (var ms = new MemoryStream(bytes))
            {
                switch (LibPDFTools.GetImageFileType(bytes))
                {
                case ImageFileType.PDF:
                    using (var pdfDoc = new PDFLibNet.PDFWrapper(""))
                    {
                        pdfDoc.LoadPDF(ms);
                        for (var i = 1; i <= pdfDoc.PageCount; i++)
                        {
                            pdfDoc.CurrentPage = i;
                            pdfDoc.CurrentX    = 0;
                            pdfDoc.CurrentY    = 0;
                            pdfDoc.RenderDPI   = this.resolution;

                            Bitmap pageBuffer = null;
                            using (var oPictureBox = new PictureBox())
                            {
                                pdfDoc.RenderPage(oPictureBox.Handle);
                                pageBuffer          = new Bitmap(pdfDoc.PageWidth, pdfDoc.PageHeight);
                                pdfDoc.ClientBounds = new System.Drawing.Rectangle(0, 0, pdfDoc.PageWidth, pdfDoc.PageHeight);
                                using (var g = Graphics.FromImage(pageBuffer))
                                {
                                    var hdc = g.GetHdc();
                                    pdfDoc.DrawPageHDC(hdc);
                                    g.ReleaseHdc();
                                }
                            }
                            SaveAddTiffPage(pageBuffer);
                            pageBuffer.Dispose();

                            currPageNum++;
                        }
                    }
                    break;

                case ImageFileType.TIFF:
                {
                    using (var image = System.Drawing.Image.FromStream(ms))
                    {
                        var fd            = new System.Drawing.Imaging.FrameDimension(image.FrameDimensionsList[0]);
                        int numberOfPages = image.GetFrameCount(fd);

                        for (int pageNum = 0; pageNum < numberOfPages; pageNum++)
                        {
                            image.SelectActiveFrame(fd, pageNum);
                            if (currPageNum == 0)
                            {
                                backbuffer = (System.Drawing.Image)image.Clone();
                                backbuffer.Save(outStream, codec, encoderParameters_MultiFrame);
                            }
                            else
                            {
                                backbuffer.SaveAdd(image, encoderParameters_FrameDimensionPage);
                            }
                            currPageNum++;
                        }
                    }
                }
                break;

                default:
                {
                    var pageBuffer = System.Drawing.Image.FromStream(ms);
                    SaveAddTiffPage(pageBuffer);
                }
                    currPageNum++;
                    break;
                }
            }
        }
Пример #4
0
        public void LoadImage(string url, ref byte[] password)
        {
            if (currentUrlOfPdf == url)
            {
                return;
            }

            var bytes = LibPDFTools.GetBytes(url);
            var ms    = new MemoryStream(bytes);

            Image pageBuffer;

            switch (LibPDFTools.GetImageFileType(bytes))
            {
            case ImageFileType.PDF:
                byte[] newPassword = password;
L100:
                try
                {
                    if (newPassword != null)
                    {
                        pdfDoc.OwnerPassword = Encoding.ASCII.GetString(newPassword);
                    }
                    pdfDoc.LoadPDF(url);
                    password = newPassword;
                }
                catch (SecurityException se)
                {
                    if (QueryPassword == null)
                    {
                        throw se;
                    }
                    if (QueryPassword != null)
                    {
                        newPassword = QueryPassword(url);
                        if (newPassword == null)
                        {
                            throw se;
                        }

                        goto L100;
                    }
                }

                pdfDoc.CurrentPage = 1;
                pdfDoc.CurrentX    = 0;
                pdfDoc.CurrentY    = 0;
                pdfDoc.RenderDPI   = 72;
                using (var oPictureBox = new PictureBox())
                {
                    pdfDoc.RenderPage(oPictureBox.Handle);

                    pageBuffer          = new Bitmap(pdfDoc.PageWidth, pdfDoc.PageHeight);
                    pdfDoc.ClientBounds = new System.Drawing.Rectangle(0, 0, pdfDoc.PageWidth, pdfDoc.PageHeight);
                    using (var g = Graphics.FromImage(pageBuffer))
                    {
                        var hdc = g.GetHdc();
                        pdfDoc.DrawPageHDC(hdc);
                        g.ReleaseHdc();
                    }
                }
                break;

            default:
                pageBuffer = System.Drawing.Image.FromStream(ms);
                break;
            }
            LoadImage(pageBuffer, url);
            GC.Collect();
        }