如何在 C# 中覆盖自定义 Papersize [英] How to override Custom Papersize in C#

查看:53
本文介绍了如何在 C# 中覆盖自定义 Papersize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 开发一个项目.我有一台标签打印机,需要打印我发送的文档.但是,打印机打印时,我无法覆盖此处看到的 Custom 纸张格式(荷兰语为 Papierformaat)的以下值:https://gyazo.com/e350ed1e355b45b8cae24196d2b5869b.如果我将 new PaperSize(); 的高度设置为小于或等于 300,它可以工作,但是如果我尝试将它设置得更大,比如 500,它会减少到 300.为什么会发生这种情况?似乎我无法覆盖链接图片中的值(即 300).

I'm working on a project in C#. I have a labelprinter which needs to print a document that I send. The printer prints, however, I'm not able to override the following values of the Custom Paper format (Papierformaat in Dutch) seen here: https://gyazo.com/e350ed1e355b45b8cae24196d2b5869b. If I make the new PaperSize(); its height smaller or equal to 300 it works, but if I try to make it bigger, say 500, it cuts it down at 300. Why does this happpen? It seems like I can't override the values from the link's picture (which is 300).

public void Printing()
{
    try
    {
        streamToPrint = new StreamReader(filePath);
        try
        {
            PrinterSettings settings = new PrinterSettings();

            printFont = new Font("Arial", 10);
            PrintDocument pd = new PrintDocument();

            PaperSize paperSize = new PaperSize("Test", 315, 300);
            paperSize.RawKind = (int)PaperKind.Custom;


            pd.DefaultPageSettings.PaperSize = paperSize;
            pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);

            pd.PrinterSettings.DefaultPageSettings.PaperSize = paperSize;
            pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);

            pd.PrintPage += (sender, args) => Console.Out.WriteLine("Printable Area for printer {0} = {1}", args.PageSettings.PrinterSettings.PrinterName, args.PageSettings.PrintableArea);
            Console.Out.WriteLine("My paper size: " + pd.DefaultPageSettings.PaperSize); 


            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
            // Print the document.
            pd.Print();
        }
        finally
        {
            streamToPrint.Close();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

控制台输出如下:

My paper size: [PaperSize Test Kind=Custom Height=500 Width=315]
Printable Area for printer xxx = {X=0,Y=0,Width=400,Height=300}

编辑

对于那些想知道的人,我正在处理带有连续卷筒的标签打印机.所以从技术上讲,我可以打印具有无限高度和 80 毫米宽度的文档.但我似乎无法从对话框设置中覆盖 Custom 300 值.

For the people wondering, I'm dealing with a Label Printer with a continuous roll. So technically I could print a document with infinite height and a set width of 80mm. But I can't seem to override the Custom 300 value from the dialog settings.

我还想指出,还有 2 个其他程序实际上能够超过 300 值并扩展 PrintableArea.谁能帮忙?

I also want to point out that there are 2 other programs which are actually able to go over the 300 value and extend the PrintableArea. Who can help?

编辑 2

在 Shakir Ahamed 发表评论后,我进一步了解:

After Shakir Ahamed's comment I got a little further:

gyazo.com/3298e480b77c5ba837b071b2ec4f7b8d 我明白了,这比我过去使用您的上一个解决方案时得到的要多得多.但是当我打印它时,页面再次像以前一样在 300 处切断,它总是在对话框中给出的值处切断(具有 300 和 400 值的框)

gyazo.com/3298e480b77c5ba837b071b2ec4f7b8d I get this, which is a lot more than I used to get with using your last solution. But when I print it, the page cuts off at 300 again like it used to, it always cuts off at the value given in the dialog box (the box with the 300 and 400 value)

我觉得它不适用于基本的打印选项,因为我认为驱动程序会覆盖页面值并且只是将它们切断而不关心 PaperSizes.我读了一些关于 DEVMODE 结构的内容,这有什么可能?我可以覆盖此处的打印机驱动程序设置并使用连续卷打印无限长的打印件吗?

I feel like it won't work with the basic printing options, because I think that the driver overrides the page values and just cuts them off without caring about the PaperSizes. I read something about a DEVMODE structure, what's all possible with that? Can I override printer driver settings in here and print infinitely long prints with the continuous roll?

EDIT 3(已解决,2016 年 10 月 20 日)

EDIT 3 (Solved, 20 oct. 2016)

有兴趣的朋友,我的打印机出现了其他一些问题,它开始表现得很奇怪(比如不打印打印作业).毕竟我猜安装驱动程序出了问题.我删除了驱动程序并根据驱动程序 CD 重新安装了所有内容,现在我最初发布的代码似乎一开始就可以正常工作.有点可惜,因为我浪费了太多时间编码,只是安装了一个糟糕的驱动程序.现在我可以打印超过 300 个单位,如果我愿意,我可以用连续卷打印超过 25 厘米.感谢所有和我一起思考解决这个问题的人!

For anyone interested, Some other problems occured with my printer and it started to act weird (like not printing print jobs). After all I guess something went wrong with installing the drivers. I deleted the drivers and reïnstalled everything according to the driver CD and now my initially posted code just seems to work fine in the first place. Kind of a bummer since I wasted so much time coding with just a bad driver installation. Now I'm able to print over the 300 units and I'm able to print with a continuous roll for more than 25cm if I want to. Thanks to everyone who was thinking with me to solve this problem!

推荐答案

尝试这样代替你的设置,在设置自定义设置之前将 PrinterSettings 的实例分配给 PrintDocument 的实例

Try like this instead of your settings,before set the custom setting assign the instance of PrinterSettings to instance of PrintDocument

PrinterSettings ps = new PrinterSettings();
PrintDocument printDoc = new PrintDocument();
printDoc.PrinterSettings = ps; 

printDoc.DefaultPageSettings.PaperSize = new PaperSize("Custom", 315, 300);

或尝试这种方式我希望这会奏效

or try this way I hope this will work

PrintDocument pd = new PrintDocument();
pd.DefaultPageSettings.PaperSize = new PaperSize("Custom", 315, 300);
pd.DefaultPageSettings.PaperSize.RawKind = 119;
pd.PrinterSettings.DefaultPageSettings.PaperSize.RawKind = 119;
pd.DefaultPageSettings.Landscape = false;

这篇关于如何在 C# 中覆盖自定义 Papersize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆