public int Open(string strInputFileName, out string strError) { strError = ""; try { m_file = File.Open(strInputFileName, FileMode.Open, FileAccess.Read); } catch (Exception ex) { strError = "打开文件 " + strInputFileName + " 失败: " + ex.Message; return -1; } if (this.SetProgress != null) { SetProgressEventArgs e = new SetProgressEventArgs(); e.Start = 0; e.End = m_file.Length; e.Value = -1; this.SetProgress(this, e); } this.XmlFilename = strInputFileName; this.m_reader = new XmlTextReader(m_file); while (true) { bool bRet = m_reader.Read(); if (bRet == false) { strError = "没有根元素"; return -1; } if (m_reader.NodeType == XmlNodeType.Element) break; } string strPageSettingXml = ""; // return: // -1 error // 0 normal // 1 reach file end。strXml中无内容 int nRet = GetPageSetting( out strPageSettingXml, out strError); if (nRet == -1) return -1; if (nRet == 0) { this.m_pagesetting = new PageSetting(); nRet = this.m_pagesetting.Build(strPageSettingXml, out strError); if (nRet == -1) return -1; } else { this.m_pagesetting = null; // 表示XML文件中没有定义 } /* m_reader.Close(); m_file = File.Open(strInputFileName, FileMode.Open, FileAccess.Read); * */ m_file.Position = 0; this.m_reader = new XmlTextReader(m_file); this.m_nPageNo = 0; this.m_pages.Clear(); this.EOF = false; return 0; }
internal void DoPrintPage( IWin32Window owner, string strStyle, PrintPageEventArgs e) { string strError = ""; int nRet = 0; if (e.Cancel == true) return; // 如果XML文件中没有页面参数,则采用当前实际的 if (this.m_pagesetting == null) { this.m_pagesetting = new PageSetting(); this.m_pagesetting.Width = e.PageBounds.Width; this.m_pagesetting.Height = e.PageBounds.Height; } else { if (this.m_pagesetting.Width == 0) this.m_pagesetting.Width = e.PageBounds.Width; if (this.m_pagesetting.Height == 0) this.m_pagesetting.Height = e.PageBounds.Height; } bool bTestingGrid = false; if (StringUtil.IsInList("TestingGrid", strStyle) == true) bTestingGrid = true; int nYCount = 0; int nXCount = 0; // 垂直方向的个数 nYCount = (int)Math.Floor((float)e.PageBounds.Height / this.m_pagesetting.Height); // 2012/4/24 if (nYCount == 0) nYCount = 1; // 水平方向的个数 nXCount = (int)Math.Floor((float)e.PageBounds.Width / this.m_pagesetting.Width); // 2012/4/24 if (nXCount == 0) nXCount = 1; int from = 0; int to = 0; bool bOutput = true; if (e.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages) { from = e.PageSettings.PrinterSettings.FromPage; to = e.PageSettings.PrinterSettings.ToPage; // 交换,保证from为小 if (from > to) { int temp = to; to = from; from = temp; } if (this.m_nPageNo == 0) { this.m_nPageNo = from; Debug.Assert(this.m_nPageNo >= 1, "this.m_nPageNo >= 1 不满足"); long nLabelCount = (nXCount * nYCount) * (this.m_nPageNo - 1); // if (this.EOF == false) { // parameters: // nCount 希望最少获得的page对象数 // return: // 0 普通 // 1 文件已经到达末尾,后面再也没有任何<document>了 nRet = GetPages( e.Graphics, (int)nLabelCount, true, out strError); if (nRet == -1) goto ERROR1; if (nRet == 1) { this.EOF = true; if (this.m_pages.Count <= nLabelCount) { e.Cancel = true; return; } } for (int i = 0; i < nLabelCount; i++) { this.m_pages.RemoveAt(0); } } } } else { if (this.m_nPageNo == 0) this.m_nPageNo = 1; // 一般性的初始化 } // 加快运行速度 float nXDelta = e.PageSettings.PrintableArea.Left; float nYDelta = e.PageSettings.PrintableArea.Top; if (this.OriginAtMargins == false) { nXDelta = 0; nYDelta = 0; } float nPrintableWidth = e.PageSettings.PrintableArea.Width; float nPrintableHeight = e.PageSettings.PrintableArea.Height; // 绘制可打印区域 // 黄色 if (bTestingGrid == true && bOutput == true) { float nXOffs = 0; float nYOffs = 0; // 如果为正式打印,左上角(0,0)已经就是可以打印区域的左上角 // 如果为preview模式,则左上角要向右向下移动,才能模拟出显示效果 if (this.OriginAtMargins == false) { nXOffs = e.PageSettings.PrintableArea.Left; nYOffs = e.PageSettings.PrintableArea.Top; } using (Pen pen = new Pen(Color.Green, (float)1)) { DrawFourAngel( e.Graphics, pen, nXOffs, nYOffs, nPrintableWidth, nPrintableHeight, 50); // 半英寸 } } // 绘制内容区域边界(也就是排除了页面边空的中间部分) // 绿色 if (bTestingGrid == true && bOutput == true) { using (Pen pen = new Pen(Color.Green, (float)3)) { /* e.Graphics.DrawRectangle(pen, label_param.PageMargins.Left - nXDelta, label_param.PageMargins.Top - nYDelta, e.PageBounds.Width - label_param.PageMargins.Left - label_param.PageMargins.Right, e.PageBounds.Height - label_param.PageMargins.Top - label_param.PageMargins.Bottom); */ } } // bool bEOF = false; #if NO if (this.m_pages.Count == 0) { string strXml = ""; // TODO: 如何提前判断这是最后一个document // return: // -1 error // 0 normal // 1 reach file end nRet = GetOneDocument( out strXml, out strError); if (nRet == -1) goto ERROR1; if (nRet == 1) { bEOF = true; goto END1; } else { List<Page> temp_pages = null; nRet = BuildPages( e.Graphics, strXml, this.m_pagesetting.Width, this.m_pagesetting.Height, out temp_pages, out strError); if (nRet == -1) goto ERROR1; this.m_pages.AddRange(temp_pages); } } #endif if (this.EOF == false) { // parameters: // nCount 希望最少获得的page对象数 // return: // 0 普通 // 1 文件已经到达末尾,后面再也没有任何<document>了 nRet = GetPages( e.Graphics, (nXCount * nYCount) + 1, false, out strError); if (nRet == -1) goto ERROR1; if (nRet == 1) this.EOF = true; } Debug.Assert(this.m_pages.Count > 0, "this.m_pages.Count > 0 不满足"); float y = 0; for (int i = 0; i < nYCount; i++) { float x = 0; for (int j = 0; j < nXCount; j++) { if (this.m_pages.Count == 0) goto END1; Page current_page = this.m_pages[0]; this.m_pages.RemoveAt(0); if (bOutput == true) { nRet = DoPrintPage(e.Graphics, x, y, current_page, out strError); if (nRet == -1) goto ERROR1; // 绘制标签边界 // 黑色 if (bTestingGrid == true) { using (Pen pen = new Pen(Color.Black, (float)1)) { e.Graphics.DrawRectangle(pen, x - nXDelta, y - nYDelta, this.m_pagesetting.Width, this.m_pagesetting.Height); } } } // end if bOutput == true x += this.m_pagesetting.Width; } y += this.m_pagesetting.Height; } #if NO Page current_page = this.m_pages[0]; this.m_pages.RemoveAt(0); nRet = PrintPage(e.Graphics, current_page, out strError); if (nRet == -1) goto ERROR1; // float y = label_param.PageMargins.Top; #endif END1: // If more lines exist, print another page. if (this.EOF == true && this.m_pages.Count == 0) { e.HasMorePages = false; return; } else { if (e.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages) { if (this.m_nPageNo >= to) { e.HasMorePages = false; return; } } } this.m_nPageNo++; e.HasMorePages = true; return; ERROR1: MessageBox.Show(owner, strError); }