英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论