PrintDocument在有段落、图片、绘制表格的情况下,如何分页?

calen 2009-06-16 08:09:09
PrintDocument在有段落、图片、绘制表格的情况下,如何分页?

针对段落的情况下,一个段落有可能处在当前页尾和下一页的页头,当然也可能会有好几页,要对其按文字行来分页,打满一页中的空白页,然后剩余部分打印到下一页。

针对图片的情况下,要求处在同一页,如果一页放不下,则新建一页打印。

针对表格的情况下,要求表格同一行处在同一页中。

之前想用RDLC实现,但是RDLC无法动态加入若干(未知数量)的图片,因此没有办法使用。

报表中有页眉和页脚
...全文
222 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
calen 2009-06-28
  • 打赏
  • 举报
回复
up
calen 2009-06-19
  • 打赏
  • 举报
回复
up
calen 2009-06-18
  • 打赏
  • 举报
回复
并非1个图片一页,真对图片小的,还是一页要有多张的
calen 2009-06-17
  • 打赏
  • 举报
回复
建立BitMap好像很难哦
龙宜坡 2009-06-17
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 calen 的回复:]
建立BitMap好像很难哦

[/Quote]


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);
}

龙宜坡 2009-06-16
  • 打赏
  • 举报
回复
主要看如何绘图及打印那块!

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();
}
}
}
龙宜坡 2009-06-16
  • 打赏
  • 举报
回复
PrintDocument在有段落、图片、绘制表格的情况下,如何分页?

针对段落的情况下,一个段落有可能处在当前页尾和下一页的页头,当然也可能会有好几页,要对其按文字行来分页,打满一页中的空白页,然后剩余部分打印到下一页。

针对图片的情况下,要求处在同一页,如果一页放不下,则新建一页打印。

针对表格的情况下,要求表格同一行处在同一页中。

之前想用RDLC实现,但是RDLC无法动态加入若干(未知数量)的图片,因此没有办法使用。 //可以的

报表中有页眉和页脚


其实可以看看这个文章

演练:在不预览的情况下打印本地报表
ximi82878 2009-06-16
  • 打赏
  • 举报
回复
换个别的方法实现吧,这个很不好用,很不灵活
kangbo818 2009-06-16
  • 打赏
  • 举报
回复
帮顶
cnwolfs 2009-06-16
  • 打赏
  • 举报
回复
mark
sxmonsy 2009-06-16
  • 打赏
  • 举报
回复
占位等高人
gaoshanlaike 2009-06-16
  • 打赏
  • 举报
回复
用水晶报表。这个问题肯定能解决。五年前做过了。
cnming 2009-06-16
  • 打赏
  • 举报
回复
或者自己编写报表组件
从网络上找个开源报表组件,然后调整调整
cnming 2009-06-16
  • 打赏
  • 举报
回复
建立一个很长很长的Bitmap,宽度设置为纸张的宽度。

然后往这个Bitmap中绘制报表,绘制的部分是除了页眉和页脚的主体部分。
同时记录下段落、图片、表格等Y轴坐标信息,以及行高、段落高度等。

最后剪裁绘制到PrintDocument中,同时绘制上页眉页脚
剪裁时,如果在段落区域,则考虑是否把行给切了,如果是在图片区域,考虑是否够高,是否需要领起一行,如果是在表格区域,则根据表格线来剪裁。

calen 2009-06-16
  • 打赏
  • 举报
回复
goga21cn 提供的例子只是告诉你如何不预览的情况下打印RDLC,并没有解决我的分页问题,或者RDLC的动态添加图片的问题。
calen 2009-06-16
  • 打赏
  • 举报
回复
up

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧