using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Windows.Forms; using System.IO; using System.Drawing.Printing; using System; using Spire.Pdf; using Spire.Pdf.Annotations; using Spire.Pdf.Widget; namespace PDFViewer { class PDFUtil { MemoryStream PDFStream; public int CurrentPage; private string FileName; PDFLibNet.PDFWrapper doc; PictureBox __pic; public int PagesCount; public Size PDFSize; public PDFUtil(byte[] PDFByte, PictureBox pic, string fileName) { this.FileName = fileName; __pic = pic; PDFStream = new MemoryStream(PDFByte); doc = new PDFLibNet.PDFWrapper(); doc.LoadPDF(PDFStream); doc.CurrentPage = 1; PagesCount = doc.PageCount; CurrentPage = doc.CurrentPage; doc.Zoom = 100; pic.Image = GetImageFromPDF(doc, __pic, 1); PDFSize = new Size(doc.PageWidth, doc.PageHeight); } public void GetPage(int numberPage) { doc.CurrentPage = numberPage; if (numberPage > doc.PageCount) doc.CurrentPage = doc.PageCount; else if (numberPage <= 0) doc.CurrentPage = 1; doc.Zoom = 100; CurrentPage = doc.CurrentPage; __pic.Image = GetImageFromPDF(doc, __pic, doc.CurrentPage); } public void NextPage() { CurrentPage++; GetPage(CurrentPage); } public void PrecPage() { CurrentPage--; GetPage(CurrentPage); } public System.Drawing.Bitmap Render(PDFLibNet.PDFWrapper Doc) { if (Doc != null) { try { System.Drawing.Bitmap Im = new Bitmap(Doc.PageWidth, Doc.PageHeight); Doc.ClientBounds = new Rectangle(0, 0, Doc.PageWidth, Doc.PageHeight); Graphics g = Graphics.FromImage(Im); using (g) { Doc.DrawPageHDC(g.GetHdc()); g.ReleaseHdc(); } g.Dispose(); return Im; } catch (Exception) { return null; } } return null; } System.Drawing.Image GetImageFromPDF(PDFLibNet.PDFWrapper pdfDoc, PictureBox pic, int PageNumber) { if (pdfDoc != null) { pdfDoc.CurrentPage = PageNumber; pdfDoc.CurrentX = 0; pdfDoc.CurrentY = 0; //if (DPI < 1 ) DPI = 200; pdfDoc.RenderPage(pic.Handle); pdfDoc.RenderDPI = 100;// GetDPI(pdfDoc, pic); return Render(pdfDoc); } return null; } System.Drawing.Image GetImageFromPDF(PDFLibNet.PDFWrapper pdfDoc, int PageNumber) { if (pdfDoc != null) { pdfDoc.CurrentPage = PageNumber; pdfDoc.CurrentX = 0; pdfDoc.CurrentY = 0; PictureBox _pic = new PictureBox(); pdfDoc.RenderPage(_pic.Handle); pdfDoc.RenderDPI = 200; return Render(pdfDoc); } return null; } double GetDPI(PDFLibNet.PDFWrapper pdfDoc, PictureBox pic) { if (pdfDoc != null) { if (pdfDoc.PageWidth > 0 && pdfDoc.PageHeight > 0) { double DPIScalePercent = 72 / pdfDoc.RenderDPI; int picHeight = pic.Height; int picWidth = pic.Width; int docHeight = pdfDoc.PageHeight; int docWidth = pdfDoc.PageWidth; PictureBox dummyPicBox = new PictureBox(); dummyPicBox.Size = pic.Size; if ((picWidth > picHeight && docWidth < docHeight) || (picWidth < picHeight && docWidth > docHeight)) { dummyPicBox.Width = picHeight; dummyPicBox.Height = picWidth; } double HScale = dummyPicBox.Width / (pdfDoc.PageWidth * DPIScalePercent); double VScale = dummyPicBox.Height / (pdfDoc.PageHeight * DPIScalePercent); dummyPicBox.Dispose(); if (VScale > HScale) return Math.Floor(72 * HScale); else return Math.Floor(72 * VScale); } } return 200; } public Image GetImageToPrint() { PDFLibNet.PDFWrapper doc = new PDFLibNet.PDFWrapper(); doc.LoadPDF(PDFStream); doc.CurrentPage = CurrentPage; doc.Zoom = 150; return GetImageFromPDF(doc, 1); } public void PrintPDF() { PrintDocument PrintDoc = new PrintDocument(); PdfDocument doc = new PdfDocument(); PrintDoc.PrintPage += new PrintPageEventHandler(PrintDoc_PrintPage); PrintDialog DPrint = new PrintDialog(); DPrint.Document = PrintDoc; doc.LoadFromFile(this.FileName); DPrint.AllowPrintToFile = true; DPrint.AllowSomePages = true; DPrint.PrinterSettings.MinimumPage = 1; DPrint.PrinterSettings.MaximumPage = doc.Pages.Count; DPrint.PrinterSettings.FromPage = 1; DPrint.PrinterSettings.ToPage = doc.Pages.Count; //DialogResult r = DPrint.ShowDialog(); if (DPrint.ShowDialog() == DialogResult.OK) { //Set the pagenumber which you choose as the start page to print doc.PrintFromPage = DPrint.PrinterSettings.FromPage; //Set the pagenumber which you choose as the final page to print doc.PrintToPage = DPrint.PrinterSettings.ToPage; //Set the name of the printer which is to print the PDF doc.PrinterName = DPrint.PrinterSettings.PrinterName; PrintDocument printDoc = doc.PrintDocument; DPrint.Document = printDoc; printDoc.Print(); } } void PrintDoc_PrintPage(object sender, PrintPageEventArgs e) { int RenderDPI = 200; System.Drawing.Image image = GetImageToPrint(); if ((image.Height > image.Width && e.Graphics.VisibleClipBounds.Width > e.Graphics.VisibleClipBounds.Height) || (image.Width > image.Height && e.Graphics.VisibleClipBounds.Height > e.Graphics.VisibleClipBounds.Width)) { image.RotateFlip(RotateFlipType.Rotate270FlipNone); } double ScalePercentage; double XMaxPixels = (e.Graphics.VisibleClipBounds.Width / 100) * image.HorizontalResolution; double YMaxPixels = (e.Graphics.VisibleClipBounds.Height / 100) * image.VerticalResolution; double XFactor = XMaxPixels / image.Width; double YFactor = YMaxPixels / image.Height; double OptimalDPI; if (YFactor > XFactor) { ScalePercentage = XFactor; OptimalDPI = RenderDPI * XFactor; } else { ScalePercentage = YFactor; OptimalDPI = RenderDPI * YFactor; } if (ScalePercentage < 0.75F) { if ((image.Height > image.Width && e.Graphics.VisibleClipBounds.Width > e.Graphics.VisibleClipBounds.Height) || (image.Width > image.Height && e.Graphics.VisibleClipBounds.Height > e.Graphics.VisibleClipBounds.Width)) image.RotateFlip(RotateFlipType.Rotate270FlipNone); } e.Graphics.ScaleTransform((float)ScalePercentage, (float)ScalePercentage); e.Graphics.DrawImage(image, 0, 0); image.Dispose(); } } }