PrintDocument在有段落、图片、绘制表格的情况下,如何分页?针对段落的情况下,一个段落有可能处在当前页尾和下一页的页头,当然也可能会有好几页,要对其按文字行来分页,打满一页中的空白页,然后剩余部分打印到下一页。针对图片的情况下,要求处在同一页,如果一页放不下,则新建一页打印。针对表格的情况下,要求表格同一行处在同一页中。之前想用RDLC实现,但是RDLC无法动态加入若干(未知数量)的图片,因此没有办法使用。报表中有页眉和页脚

解决方案 »

  1.   

    PrintDocument在有段落、图片、绘制表格的情况下,如何分页?针对段落的情况下,一个段落有可能处在当前页尾和下一页的页头,当然也可能会有好几页,要对其按文字行来分页,打满一页中的空白页,然后剩余部分打印到下一页。针对图片的情况下,要求处在同一页,如果一页放不下,则新建一页打印。针对表格的情况下,要求表格同一行处在同一页中。之前想用RDLC实现,但是RDLC无法动态加入若干(未知数量)的图片,因此没有办法使用。  //可以的报表中有页眉和页脚
    其实可以看看这个文章演练:在不预览的情况下打印本地报表
      

  2.   

    主要看如何绘图及打印那块!using System;
    using System.IO;
    using System.Data;
    using System.Text;
    using System.Drawing.Imaging;
    using System.Drawing.Printing;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using Microsoft.Reporting.WinForms;public class Demo : IDisposable
    {
        private int m_currentPageIndex;
        private IList<Stream> m_streams;    private DataTable LoadSalesData()
        {
            // Create a new DataSet and read sales data file 
            //    data.xml into the first DataTable.
            DataSet dataSet = new DataSet();
            dataSet.ReadXml(@"..\..\data.xml");
            return dataSet.Tables[0];
        }
        // Routine to provide to the report renderer, in order to
        //    save an image for each page of the report.
        private Stream CreateStream(string name,
          string fileNameExtension, Encoding encoding,
          string mimeType, bool willSeek)
        {
            Stream stream = new FileStream(@"..\..\" + name +
               "." + fileNameExtension, FileMode.Create);
            m_streams.Add(stream);
            return stream;
        }
        // Export the given report as an EMF (Enhanced Metafile) file.
        private void Export(LocalReport report)
        {
            string deviceInfo =
              "<DeviceInfo>" +
              "  <OutputFormat>EMF</OutputFormat>" +
              "  <PageWidth>8.5in</PageWidth>" +
              "  <PageHeight>11in</PageHeight>" +
              "  <MarginTop>0.25in</MarginTop>" +
              "  <MarginLeft>0.25in</MarginLeft>" +
              "  <MarginRight>0.25in</MarginRight>" +
              "  <MarginBottom>0.25in</MarginBottom>" +
              "</DeviceInfo>";
            Warning[] warnings;
            m_streams = new List<Stream>();
            report.Render("Image", deviceInfo, CreateStream,
               out warnings);
            foreach (Stream stream in m_streams)
                stream.Position = 0;
        }
        // Handler for PrintPageEvents
        private void PrintPage(object sender, PrintPageEventArgs ev)
        {
            Metafile pageImage = new
               Metafile(m_streams[m_currentPageIndex]);
            ev.Graphics.DrawImage(pageImage, ev.PageBounds);
            m_currentPageIndex++;
            ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
        }    private void Print()
        {
            const string printerName =
               "Microsoft Office Document Image Writer";
            if (m_streams == null || m_streams.Count == 0)
                return;
            PrintDocument printDoc = new PrintDocument();
            printDoc.PrinterSettings.PrinterName = printerName;
            if (!printDoc.PrinterSettings.IsValid)
            {
                string msg = String.Format(
                   "Can't find printer \"{0}\".", printerName);
                MessageBox.Show(msg, "Print Error");
                return;
            }
            printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
            printDoc.Print();
        }
        // Create a local report for Report.rdlc, load the data,
        //    export the report to an .emf file, and print it.
        private void Run()
        {
            LocalReport report = new LocalReport();
            report.ReportPath = @"..\..\Report.rdlc";
            report.DataSources.Add(
               new ReportDataSource("Sales", LoadSalesData()));
            Export(report);
            m_currentPageIndex = 0;
            Print();
        }    public void Dispose()
        {
            if (m_streams != null)
            {
                foreach (Stream stream in m_streams)
                    stream.Close();
                m_streams = null;
            }
        }    public static void Main(string[] args)
        {
            using (Demo demo = new Demo())
            {
                demo.Run();
            }
        }
    }
      

  3.   

    goga21cn 提供的例子只是告诉你如何不预览的情况下打印RDLC,并没有解决我的分页问题,或者RDLC的动态添加图片的问题。
      

  4.   

    建立一个很长很长的Bitmap,宽度设置为纸张的宽度。然后往这个Bitmap中绘制报表,绘制的部分是除了页眉和页脚的主体部分。
    同时记录下段落、图片、表格等Y轴坐标信息,以及行高、段落高度等。最后剪裁绘制到PrintDocument中,同时绘制上页眉页脚
    剪裁时,如果在段落区域,则考虑是否把行给切了,如果是在图片区域,考虑是否够高,是否需要领起一行,如果是在表格区域,则根据表格线来剪裁。
      

  5.   

    private void PrintPage(object sender, PrintPageEventArgs ev)
        {
            Metafile pageImage = new
               Metafile(m_streams[m_currentPageIndex]);//这个不就是用图片方式打印么
            ev.Graphics.DrawImage(pageImage, ev.PageBounds);
            m_currentPageIndex++;
            ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
        }