private void document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { try { //当 PrintPreviewDialog的Show方法触发时,引用这段代码 PrintPagelayoutControl.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingTile; //获取打印分辨率 short dpi = (short)e.Graphics.DpiX; IEnvelope devBounds = new EnvelopeClass(); //获取打印页面 IPage page = PrintPagelayoutControl.Page; //获取打印的页数 short printPageCount; printPageCount = PrintPagelayoutControl.get_PrinterPageCount(0); m_CurrentPrintPage++; //选择打印机 IPrinter printer = PrintPagelayoutControl.Printer; //获得打印机页面大小 page.GetDeviceBounds(printer, m_CurrentPrintPage, 0, dpi, devBounds); //获得页面大小的坐标范围,即四个角的坐标 tagRECT deviceRect; double xmin, ymin, xmax, ymax; devBounds.QueryCoords(out xmin, out ymin, out xmax, out ymax); deviceRect.bottom = (int)ymax; deviceRect.left = (int)xmin; deviceRect.top = (int)ymin; deviceRect.right = (int)xmax; //确定当前打印页面的大小 IEnvelope visBounds = new EnvelopeClass(); page.GetPageBounds(printer, m_CurrentPrintPage, 0, visBounds); IntPtr hdc = e.Graphics.GetHdc(); PrintPagelayoutControl.ActiveView.Output(hdc.ToInt32(), dpi, ref deviceRect, visBounds, m_TrackCancel); e.Graphics.ReleaseHdc(hdc); if (m_CurrentPrintPage < printPageCount) { e.HasMorePages = true; } else { e.HasMorePages = false; } } catch { } }
private void UpdatePrintPageDisplay() { //Determine the number of pages short iPageCount = axPageLayoutControl1.get_PrinterPageCount(Convert.ToDouble(txbOverlap.Text)); lblPageCount.Text = iPageCount.ToString(); //Validate start and end pages int iPageStart = Convert.ToInt32(txbStartPage.Text); int iPageEnd = Convert.ToInt32(txbEndPage.Text); if ((iPageStart < 1) | (iPageStart > iPageCount)) { txbStartPage.Text = "1"; } if ((iPageEnd < 1) | (iPageEnd > iPageCount)) { txbEndPage.Text = iPageCount.ToString(); } }
private void document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { //this code will be called when the PrintPreviewDialog.Show method is called //set the PageToPrinterMapping property of the Page. This specifies how the page //is mapped onto the printer page. By default the page will be tiled //get the selected mapping option string sPageToPrinterMapping = (string)this.comboBox1.SelectedItem; if (sPageToPrinterMapping == null) { //if no selection has been made the default is tiling axPageLayoutControl1.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingTile; } else if (sPageToPrinterMapping.Equals("esriPageMappingTile")) { axPageLayoutControl1.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingTile; } else if (sPageToPrinterMapping.Equals("esriPageMappingCrop")) { axPageLayoutControl1.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingCrop; } else if (sPageToPrinterMapping.Equals("esriPageMappingScale")) { axPageLayoutControl1.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingScale; } else { axPageLayoutControl1.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingTile; } //get the resolution of the graphics device used by the print preview (including the graphics device) short dpi = (short)e.Graphics.DpiX; //envelope for the device boundaries IEnvelope devBounds = new EnvelopeClass(); //get page IPage page = axPageLayoutControl1.Page; //the number of printer pages the page will be printed on short printPageCount; printPageCount = axPageLayoutControl1.get_PrinterPageCount(0); m_CurrentPrintPage++; //the currently selected printer IPrinter printer = axPageLayoutControl1.Printer; //get the device bounds of the currently selected printer page.GetDeviceBounds(printer, m_CurrentPrintPage, 0, dpi, devBounds); //structure for the device boundaries tagRECT deviceRect; //Returns the coordinates of lower, left and upper, right corners double xmin, ymin, xmax, ymax; devBounds.QueryCoords(out xmin, out ymin, out xmax, out ymax); //initialize the structure for the device boundaries deviceRect.bottom = (int)ymax; deviceRect.left = (int)xmin; deviceRect.top = (int)ymin; deviceRect.right = (int)xmax; //determine the visible bounds of the currently printed page IEnvelope visBounds = new EnvelopeClass(); page.GetPageBounds(printer, m_CurrentPrintPage, 0, visBounds); //get a handle to the graphics device that the print preview will be drawn to IntPtr hdc = e.Graphics.GetHdc(); //print the page to the graphics device using the specified boundaries axPageLayoutControl1.ActiveView.Output(hdc.ToInt32(), dpi, ref deviceRect, visBounds, m_TrackCancel); //release the graphics device handle e.Graphics.ReleaseHdc(hdc); //check if further pages have to be printed if (m_CurrentPrintPage < printPageCount) { e.HasMorePages = true; //document_PrintPage event will be called again } else { e.HasMorePages = false; } }