设置纸张大小到C#打印

huangapple go评论66阅读模式
英文:

Setting a Paper Size into C# print

问题

我有一个项目,需要我将大数据打印到纸上,纸的宽度为3.3英寸,但高度可以根据数据的大小从0.8英寸到100英寸不等(标签打印机)。

我被告知打印对话框不应该对用户可见,所以我决定通过硬编码设置打印选项,并通过应用程序计算纸张的高度。

在将PrintDocument放入观察中,我发现页面大小可以通过以下选项来设置(如果我没有理解错的话):

doc.DefaultPageSettings.PaperSize.Kind = System.Drawing.Printing.PaperKind.Custom;
doc.DefaultPageSettings.PaperSize.Height = 500;

现在的问题是,如果纸张类型不是自定义,我将会收到一个异常,我需要将doc.DefaultPageSettings.PaperSize.Kind更改为自定义,然而,我发现纸张类型是只读的。

我如何在PrintDoc中更改纸张类型为自定义?

英文:

I have a project which requires me to print big data on paper , width f the paper in 3.3inch but the height can be as small aS 0.8 inch up to 100 inches base on the data(label printer)

I have been told that printDialog must NOT be accessible to the user so I decided to set the setting by hardcoding it and calculating the height of the paper by the app .

after putting PrintDocument into watch I have found that page size can be set by these options(probably if I am not wrong)

        doc.DefaultPageSettings.PaperSize.kind = System.Drawing.Printing.PaperKind.Custom;
        doc.DefaultPageSettings.PaperSize.Height = 500;

now the problem is if the paper kind is not custom I will get an exception which I need to change doc.DefaultPageSettings.PaperSize.kind to custom , however, I get that paper kind is read-only

how can I change the paper type to custom in PrintDoc?

答案1

得分: 0

以下是翻译好的部分:

所以我遇到了一篇帖子,经过一些修改,我得到了下面的代码,它完全符合我的需求。

PrintDocument doc = new PrintDocument();
PrintDialog print = new PrintDialog();
if (print.ShowDialog(this) == DialogResult.OK)
{
   print.Document = doc;
}
float height = (((float)0.763 * printNumbers) + ((float)0.098 * (printNumbers - (float)1))) * 100;
PaperSize paperSize = new PaperSize("MY_PAGE_SIZE_NME", 333, (int)height);
paperSize.RawKind = 256;
doc.DefaultPageSettings.PaperSize = paperSize;
doc.Print();

Height中的数字是根据我的需要自定义的,如果需要的话,您可能需要更改并重新计算。

英文:

So I have come across a post and with some modification, I got the below code which works perfectly for my purpose

PrintDocument doc = new PrintDocument();
PrintDialog print = new PrintDialog();
if (print.ShowDialog(this) == DialogResult.OK)
{
   print.Document = doc;
}
float height = (((float)0.763 * printNumbers) + ((float)0.098 * (printNumbers - (float)1))) * 100;
PaperSize paperSize = new PaperSize("MY_PAGE_SIZE_NME", 333, (int)height);
paperSize.RawKind = 256;
doc.DefaultPageSettings.PaperSize = paperSize;
doc.Print();

the numbers in Height are customized to my own need, you may wanna change and calculate your if needed

huangapple
  • 本文由 发表于 2023年7月23日 21:56:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定