web打印:预览时正常,打印时只显示部分内容

webmaxer 2004-08-20 03:08:57
我开发了一个web打印系统,页面大小为自定义238*338mm,转换为象素为937*1330,预览时全部正常显示,但实际打印时只显示750*1100的范围,有高手能解决此问题吗?
...全文
712 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
biliboy 2004-10-13
  • 打赏
  • 举报
回复
up
biliboy 2004-10-13
  • 打赏
  • 举报
回复
up
biliboy 2004-10-13
  • 打赏
  • 举报
回复
up
lazybone 2004-09-14
  • 打赏
  • 举报
回复
肯定是crystal report 里面把纸张的大小设错了
zairwolfc 2004-08-23
  • 打赏
  • 举报
回复
唉。





_____________________________________________________________________________

该问题已经结贴 ,得分记录: zairwolfc (300)
长江支流 2004-08-23
  • 打赏
  • 举报
回复
源程序发出来看看flygoldfish@sina.com
webmaxer 2004-08-23
  • 打赏
  • 举报
回复
我想问题应该不在分辩率上。

我在打印时再用MessageBox.Show(g.VisibleClipBounds.ToString());测试了一下,用我自定义的纸张和A4纸张均显示为{x=0,y=0,width=1072.778,height=801.6666}
长江支流 2004-08-23
  • 打赏
  • 举报
回复
在预览时能显示是因为输出设备即显示器为同一设备,而打印时输出设备是打印机,由于打印机与显示器的分辩率不同,因此会出现你说的结果.

这里有打印源码下载,大家可以看看
.^^.开源:C#.NET开发的MIS打印程序,各种网格如DataGrid打印、DataTable、HtmlTable等二维形式全搞定,源码免费下载
http://community.csdn.net/Expert/topic/3278/3278050.xml?temp=.5012934

开源:.NET环境下有关打印页面设置、打印机设置、打印预览对话框的实现
http://blog.csdn.net/flygoldfish/archive/2004/08/17/77208.aspx
webmaxer 2004-08-23
  • 打赏
  • 举报
回复
我在打印预览时调用下面语句:
g.DrawString(e.PageSettings.PaperSize.ToString()+ " " +e.PageBounds.ToString(),font,brush,e.MarginBounds.Left,e.MarginBounds.Height-20);

显示结果为:
[PaperSize Custom Kind=Custom Height=1330 width=937] {X=0,Y=0,width=1330,height=937}

显示结果应该证明代码没有问题,但是打印时为什么只能打印部分(横打750*1100)呢?
webmaxer 2004-08-23
  • 打赏
  • 举报
回复
是的。打印设置方法哪儿有问题呢?
SuperGam 2004-08-23
  • 打赏
  • 举报
回复
问题好象出在打印机设置上,用下面的打印设置按钮更改还是有效,打印时能看到效果。
webmaxer 2004-08-23
  • 打赏
  • 举报
回复
问题好象出在打印机设置上,用下面的打印设置按钮更改还是有效,打印时能看到效果。
private void Set_Click(object sender, System.EventArgs e)
{
this.pageSetupDialog1.ShowDialog();
this.printDocument1.DefaultPageSettings = this.pageSetupDialog1.PageSettings;

}

但是在程序中调用打印机设置方法似乎没有起作用,老是用A4的纸打的。
private void SettingPrinter(XmlNode ps)
{
MessageBox.Show("SettingPrinter");
//打印方向
this.printDocument1.DefaultPageSettings.Landscape = bool.Parse(ps["landscape"].InnerText);
//页边距
this.printDocument1.DefaultPageSettings.Margins.Left = int.Parse(ps["pageleft"].InnerText);
this.printDocument1.DefaultPageSettings.Margins.Right = int.Parse(ps["pageright"].InnerText);
this.printDocument1.DefaultPageSettings.Margins.Top = int.Parse(ps["pagetop"].InnerText);
this.printDocument1.DefaultPageSettings.Margins.Bottom = int.Parse(ps["pagebottom"].InnerText);
//MessageBox.Show(this.printDocument1.DefaultPageSettings.PaperSize.ToString() + " " + this.printDocument1.PrinterSettings.DefaultPageSettings.PaperSize.ToString() + " " + this.printDocument1.PrinterSettings.DefaultPageSettings.Margins.ToString());
//纸张类型
String paperName = ps["paperkind"].InnerText;
bool fitPaper = false;
//获取打印机支持的所有纸张类型
foreach(PaperSize size in this.printDocument1.PrinterSettings.PaperSizes)
{
//看该打印机是否有我们需要的纸张类型
if(paperName == size.PaperName)
{
this.printDocument1.DefaultPageSettings.PaperSize = size;
fitPaper = true;
}
}
//如果没有我们需要的标准类型,则使用自定义的尺寸
if(!fitPaper)
{
this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custom",int.Parse(ps["paperwidth"].InnerText),int.Parse(ps["paperheight"].InnerText));
//this.printDocument1.PrinterSettings.DefaultPageSettings.PaperSize = this.printDocument1.DefaultPageSettings.PaperSize;
//this.printDocument1.PrinterSettings.DefaultPageSettings.Margins = this.printDocument1.DefaultPageSettings.Margins;
}
//MessageBox.Show(this.printDocument1.DefaultPageSettings.PaperSize.ToString() + " " + this.printDocument1.DefaultPageSettings.Margins.ToString() + " " + this.printDocument1.PrinterSettings.DefaultPageSettings.PaperSize.ToString() + " " + this.printDocument1.PrinterSettings.DefaultPageSettings.Margins.ToString());
}

private void PrintControl_Load(object sender, System.EventArgs e)
{
try
{
//装载报表XML数据
this.label1.Text = "正在加载报表数据,请稍候…";
doc.Load("http://localhost/lqz.xml");
this.SettingPrinter(doc["root"].FirstChild);
this.label1.Text = "数据加载完毕!";
this.Set.Enabled = this.PrintPrivew.Enabled = this.Print.Enabled = true;
}
catch(Exception ex)
{
this.label1.Text = "出现错误:" + ex.Message;
}
}
yellowhwb 2004-08-23
  • 打赏
  • 举报
回复
严重导分
webmaxer 2004-08-23
  • 打赏
  • 举报
回复


private void SettingPrinter(XmlNode ps)
{
MessageBox.Show("SettingPrinter");
//打印方向
this.printDocument1.DefaultPageSettings.Landscape = bool.Parse(ps["landscape"].InnerText);
//页边距
this.printDocument1.DefaultPageSettings.Margins.Left = int.Parse(ps["pageleft"].InnerText);
this.printDocument1.DefaultPageSettings.Margins.Right = int.Parse(ps["pageright"].InnerText);
this.printDocument1.DefaultPageSettings.Margins.Top = int.Parse(ps["pagetop"].InnerText);
this.printDocument1.DefaultPageSettings.Margins.Bottom = int.Parse(ps["pagebottom"].InnerText);
//MessageBox.Show(this.printDocument1.DefaultPageSettings.PaperSize.ToString() + " " + this.printDocument1.PrinterSettings.DefaultPageSettings.PaperSize.ToString() + " " + this.printDocument1.PrinterSettings.DefaultPageSettings.Margins.ToString());
//纸张类型
String paperName = ps["paperkind"].InnerText;
bool fitPaper = false;
//获取打印机支持的所有纸张类型
foreach(PaperSize size in this.printDocument1.PrinterSettings.PaperSizes)
{
//看该打印机是否有我们需要的纸张类型
if(paperName == size.PaperName)
{
this.printDocument1.DefaultPageSettings.PaperSize = size;
fitPaper = true;
}
}
//如果没有我们需要的标准类型,则使用自定义的尺寸
if(!fitPaper)
{
this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custom",int.Parse(ps["paperwidth"].InnerText),int.Parse(ps["paperheight"].InnerText));
//this.printDocument1.PrinterSettings.DefaultPageSettings.PaperSize = this.printDocument1.DefaultPageSettings.PaperSize;
//this.printDocument1.PrinterSettings.DefaultPageSettings.Margins = this.printDocument1.DefaultPageSettings.Margins;
}
//MessageBox.Show(this.printDocument1.DefaultPageSettings.PaperSize.ToString() + " " + this.printDocument1.DefaultPageSettings.Margins.ToString() + " " + this.printDocument1.PrinterSettings.DefaultPageSettings.PaperSize.ToString() + " " + this.printDocument1.PrinterSettings.DefaultPageSettings.Margins.ToString());
}

private void PrintControl_Load(object sender, System.EventArgs e)
{
try
{
//装载报表XML数据
this.label1.Text = "正在加载报表数据,请稍候…";
doc.Load("http://localhost/lqz.xml");
this.SettingPrinter(doc["root"].FirstChild);
this.label1.Text = "数据加载完毕!";
this.Set.Enabled = this.PrintPrivew.Enabled = this.Print.Enabled = true;
}
catch(Exception ex)
{
this.label1.Text = "出现错误:" + ex.Message;
}
}

private void PrintPage(object sender,PrintPageEventArgs e)
{
this.WriteMetricsToConsole(e);
Graphics g = e.Graphics;
//g.PageUnit = GraphicsUnit.Millimeter;
MessageBox.Show("width:" + g.VisibleClipBounds.Width + " height:" + g.VisibleClipBounds.Height +" x:" + g.DpiX.ToString() + " y:" + g.DpiY.ToString());
//g.SetClip(new RectangleF(0,0,g.VisibleClipBounds.Width * g.DpiX/100,g.VisibleClipBounds.Height * g.DpiY/100));
//MessageBox.Show(g.VisibleClipBounds.ToString());
bool hasMorePages = false;
PrintElement printElement = null;

foreach(XmlNode node in doc["root"]["reporttable"].ChildNodes)
{
//调用解析器生成相应的对象
printElement = Parser.CreateElement(node);
try
{
hasMorePages = printElement.Draw(g);
}
catch(Exception ex)
{
this.label1.Text = ex.Message;
}
}

//在页底中间输出页码
Font font = new Font ("宋体",9.0f);
Font font2 = new Font("黑体",9.0f,FontStyle.Bold);
Brush brush = new SolidBrush(Color.Black);
//打印刻度
//Y
for(int i=0;i<e.PageBounds.Height;i=i+10)
{
g.DrawString("-",font,brush,e.PageBounds.Left,e.MarginBounds.Top+i);
//MessageBox.Show(Math.IEEERemainder(i,50.0).ToString());
if(Math.IEEERemainder(i,50.0)==0)
{
g.DrawString("—",font2,brush,e.PageBounds.Left,e.MarginBounds.Top+i);
g.DrawString(i.ToString(),font,brush,e.PageBounds.Left+20,e.MarginBounds.Top+i);
}
}
//X
for(int i=0;i<e.PageBounds.Width;i=i+10)
{
g.DrawString("|",font,brush,e.PageBounds.Left+i,e.MarginBounds.Top);
if(Math.IEEERemainder(i,50.0)==0)
{
g.DrawString("|",font2,brush,e.PageBounds.Left+i,e.MarginBounds.Top);
g.DrawString(i.ToString(),font,brush,e.PageBounds.Left+i,e.MarginBounds.Top+20);
}
}
g.DrawString(e.PageSettings.PaperSize.ToString()+ " " +e.PageBounds.ToString(),font,brush,e.MarginBounds.Left,e.MarginBounds.Height-20);
//g.DrawString("第" + Pages.ToString() + "页",font,brush,e.MarginBounds.Width/2 + e.MarginBounds.Left -30,e.PageBounds.Height-100);
if(hasMorePages)
Pages ++;
e.HasMorePages = hasMorePages;
}

private void Set_Click(object sender, System.EventArgs e)
{
this.pageSetupDialog1.ShowDialog();
this.printDocument1.DefaultPageSettings = this.pageSetupDialog1.PageSettings;
}

private void PrintPrivew_Click(object sender, System.EventArgs e)
{
try
{
this.printPreviewDialog1.ShowDialog();
}
catch(Exception ex)
{
this.label1.Text = ex.Message;
}
}

private void Print_Click(object sender, System.EventArgs e)
{
try
{
this.printDocument1.Print();
}
catch(Exception ex)
{
this.label1.Text = ex.Message;
}
}
}
}
webmaxer 2004-08-23
  • 打赏
  • 举报
回复
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Xml;
using System.Drawing.Printing;

namespace RemotePrint
{
/// <summary>
/// PrintControl 的摘要说明。
/// </summary>
public class PrintControl : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Label label1;
private System.Drawing.Printing.PrintDocument printDocument1;
private System.Windows.Forms.PageSetupDialog pageSetupDialog1;
private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
private XmlDocument doc = new XmlDocument ();
public static int Pages = 1;
private System.Windows.Forms.Button Set;
private System.Windows.Forms.Button PrintPrivew;
private System.Windows.Forms.Button Print;//计算页码
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public PrintControl()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
this.printDocument1.PrintPage += new PrintPageEventHandler(this.PrintPage);
// TODO: 在 InitializeComponent 调用后添加任何初始化

}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(PrintControl));
this.label1 = new System.Windows.Forms.Label();
this.Set = new System.Windows.Forms.Button();
this.PrintPrivew = new System.Windows.Forms.Button();
this.Print = new System.Windows.Forms.Button();
this.printDocument1 = new System.Drawing.Printing.PrintDocument();
this.pageSetupDialog1 = new System.Windows.Forms.PageSetupDialog();
this.printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog();
this.SuspendLayout();
//
// label1
//
this.label1.Anchor = System.Windows.Forms.AnchorStyles.Left;
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(64, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(42, 17);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// Set
//
this.Set.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.Set.Enabled = false;
this.Set.Location = new System.Drawing.Point(168, 14);
this.Set.Name = "Set";
this.Set.TabIndex = 1;
this.Set.Text = "Set";
this.Set.Click += new System.EventHandler(this.Set_Click);
//
// PrintPrivew
//
this.PrintPrivew.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.PrintPrivew.Enabled = false;
this.PrintPrivew.Location = new System.Drawing.Point(288, 14);
this.PrintPrivew.Name = "PrintPrivew";
this.PrintPrivew.Size = new System.Drawing.Size(104, 23);
this.PrintPrivew.TabIndex = 2;
this.PrintPrivew.Text = "PrintPrivew";
this.PrintPrivew.Click += new System.EventHandler(this.PrintPrivew_Click);
//
// Print
//
this.Print.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.Print.Enabled = false;
this.Print.Location = new System.Drawing.Point(424, 14);
this.Print.Name = "Print";
this.Print.TabIndex = 3;
this.Print.Text = "Print";
this.Print.Click += new System.EventHandler(this.Print_Click);
//
// pageSetupDialog1
//
this.pageSetupDialog1.Document = this.printDocument1;
//
// printPreviewDialog1
//
this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.ClientSize = new System.Drawing.Size(400, 300);
this.printPreviewDialog1.Document = this.printDocument1;
this.printPreviewDialog1.Enabled = true;
this.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
this.printPreviewDialog1.Location = new System.Drawing.Point(326, 20);
this.printPreviewDialog1.MinimumSize = new System.Drawing.Size(375, 250);
this.printPreviewDialog1.Name = "printPreviewDialog1";
this.printPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty;
this.printPreviewDialog1.Visible = false;
//
// PrintControl
//
this.Controls.Add(this.Print);
this.Controls.Add(this.PrintPrivew);
this.Controls.Add(this.Set);
this.Controls.Add(this.label1);
this.Name = "PrintControl";
this.Size = new System.Drawing.Size(600, 48);
this.Load += new System.EventHandler(this.PrintControl_Load);
this.ResumeLayout(false);

}
#endregion
xiaomaoy 2004-08-22
  • 打赏
  • 举报
回复
源码贴出来看看.
看是否是代码的问题,还是打印机的问题
webmaxer 2004-08-22
  • 打赏
  • 举报
回复
源码请参见:卢彦先生的文章(续:利用XML实现通用WEB报表打印(实现篇))

我只是改了XML文件及在打印之前调用了打印设置方法:SettingPrinter()

http://www.microsoft.com/china/community/Column/61.mspx
charliecy 2004-08-22
  • 打赏
  • 举报
回复
把打印机页面设置调大,或者编写针对指定页面大小的打印。比如说程序中是针对A3的打印,但打印机的页面设置是A4的,结果当然只能打印一部分了。
robin0925 2004-08-22
  • 打赏
  • 举报
回复
那就是传给打印机的时候有问题!先看看打印设置!
hivak47 2004-08-21
  • 打赏
  • 举报
回复
源码贴出来看看.
加载更多回复(3)

110,535

社区成员

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

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

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