如何用C#打印存储在本地硬盘上的文件?

发布于 2024-10-08 15:32:14 字数 330 浏览 0 评论 0原文

我在 C# (WinForms) 中创建了一个函数,它将文件作为 gif 图像保存在本地目录中。如何访问它并将其发送到我的一台网络打印机进行打印?

我现在这里有这段代码:

internal void PrintLabels(string printerInfo, List<string> shippingLabels)
{
    //this is where I print to printer...
    foreach (string labelPath in shippingLabels)
    {

    }

}

有帮助吗?

I have a function created in C# (WinForms) which saves the file as gif image in local directory. How can I access it and send it to printing to one of my network printers?

I have this code here right now:

internal void PrintLabels(string printerInfo, List<string> shippingLabels)
{
    //this is where I print to printer...
    foreach (string labelPath in shippingLabels)
    {

    }

}

Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玩世 2024-10-15 15:32:14

另一种方法是以编程方式创建 pdf 文档,然后通过命令行批量打印

查看 iText 库。

创建文件后,您可以通过命令行打印它们(您可以使用 System.Diagnostics 命名空间中的 Command 类)

如果您要批量执行所有这些操作,那么您还需要如果您要打印到的打印队列出现问题,则会收到通知(可能以编程方式)。我相信有一门课可以做到这一点。

有关该主题的详细信息,请尝试此处

An alternative method would be to programmatically create pdf document/s that you then batch print via CommandLine

Take a look at the iText library.

Once you have created your files, you can print them via a command line (you can using the Command class found in the System.Diagnostics namespace for that)

If you're doing all this from a batch, then you'll want to also be notified (perhaps programmatically) if something goes wrong with the print queue that you are printing to. I believe there is a class for that.

For more information on the subject, try here.

绻影浮沉 2024-10-15 15:32:14

我有一堆“gif”图像。您的所有链接都是针对 .txt 文件的。这是我的代码:

 public void PrintShippingLabels()
    {

        //mock of what the reset of the program will produce up to this step
        List<string> shippingLabels = new List<string>();
        for (var i = 0; i < 10; i++)
        {
            var trackingNumber = "1ZR02XXXXXXXXXXXXX" + i + ".gif";
            shippingLabels.Add(trackingNumber);
            CreateSampleShippingLabel(trackingNumber);
        }

        Assert.AreEqual(10, shippingLabels.Count);

        IceTechUPSClient.Instance.PrintLabels("", shippingLabels);

    }

  public void PrintLabels(List<string> shippingLabels)
    {
        //this is where I print to printer...
        PrintDocument pd = new PrintDocument();
        foreach (string labelPath in shippingLabels)
        {  
            pd.Print();

        }

    }

I have bunch of 'gif' images. Your all links are for the .txt files. This is my code:

 public void PrintShippingLabels()
    {

        //mock of what the reset of the program will produce up to this step
        List<string> shippingLabels = new List<string>();
        for (var i = 0; i < 10; i++)
        {
            var trackingNumber = "1ZR02XXXXXXXXXXXXX" + i + ".gif";
            shippingLabels.Add(trackingNumber);
            CreateSampleShippingLabel(trackingNumber);
        }

        Assert.AreEqual(10, shippingLabels.Count);

        IceTechUPSClient.Instance.PrintLabels("", shippingLabels);

    }

  public void PrintLabels(List<string> shippingLabels)
    {
        //this is where I print to printer...
        PrintDocument pd = new PrintDocument();
        foreach (string labelPath in shippingLabels)
        {  
            pd.Print();

        }

    }
蓝眼睛不忧郁 2024-10-15 15:32:14

检查您拥有的打印机。一些设备现在能够本地打印 gif、jpeg、tiff 等,无需转换为 PCL 或 PostScript 数据流(或其他打印语言)。如果是这种情况,您可以通过 LPR 协议直接通过端口 9100 或直接通过 Windows 打印队列发送文件 (http://support.microsoft.com/kb/322091)

Check the printer you have. Some devices now have the ability to print gif, jpeg, tiff, etc. natively without requiring the conversion into PCL or PostScript datastreams (or other print language). If this is the case you could just send the file via the LPR protocol, directly over port 9100 or directly through a Windows print queue (http://support.microsoft.com/kb/322091)

烟织青萝梦 2024-10-15 15:32:14

自从“很久以前”默认的 GIF(和其他图像)“文件处理程序”以来,因此非常不受控制的方法一直是 MS Paint。不是最好的答案,但在此处添加为最基本的 Windows 操作系统方法。

简而言之,您可以 mspaint /p a.gif 到默认打印机(在 Windows 10 中仍然有效)。如果您的默认打印机是 PortPrompt,则显然会出现问题(通过在调用打印之前将默认值设置为其他打印​​机可​​以轻松修复) )

,如果您有 MS Print to PDF 的“Prompt less Port”副本(这里是 A4 横向),那么您可以使用备用“静默”(除了进度消息框)。 PrintTo shell 命令。
mspaint /pt a.gif Printername

主要缺点是,您需要修改注册表设置来设置边距和页面打印居中等。因此任何其他应用程序都应该提供更好的打印机控制设置。

输入图片此处描述

Since "way back when" the default GIF (and other images) "File Handler", thus very uncontrolled method, has always been MS Paint. Not the best answer, but added here as the most basic Windows OS method.

Simply put you can mspaint /p a.gif to the default printer (still works in Windows 10. Clearly a problem if your default is a PortPrompt (Easily fixed by set default to a different printer before invoking print)
enter image description here

So if you have a "Prompt less Port" copy of MS Print to PDF (Here it is A4 Landscape) then you can use the alternate "silent" (apart from a progress message box). PrintTo shell command.
mspaint /pt a.gif printername

Major disadvantage is, you need to fiddle registry settings to set margins and page print centering etc. So any other app should offer better printer control settings.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文