简体   繁体   English

PDF 使用 System.Drawing.Printing 在 .NET 内核中打印

[英]PDF printing in .NET Core using System.Drawing.Printing

I'm printing PDF files by converting every page to PNG image with PDFiumSharp.我通过使用 PDFiumSharp 将每一页转换为 PNG 图像来打印 PDF 文件。 Next, I draw this image to Graphics.接下来,我将此图像绘制到 Graphics。

private void PrintPage(object sender, PrintPageEventArgs ev)
{
    ev.Graphics.DrawImage(images[pageNum], ev.Graphics.VisibleClipBounds);
    pageNum++;
    if (pageNum == images.Count)
    {
        ev.HasMorePages = false;
    }
    else
    {
        ev.HasMorePages = true;
    }
}

public void Print()
{
    printDocument.PrintPage += new PrintPageEventHandler(PrintPage);
    pageNum = 0;

    if (printDocument.PrinterSettings.IsValid)
    {
        printDocument.Print();
    }
    else
    {
        throw new Exception("Printer is invalid.");
    }
}

The problem is that printer receives very large data and the whole process works slowly.问题是打印机接收到的数据非常大,整个过程运行缓慢。 I tried to use lpr command on Windows.我尝试在 Windows 上使用lpr命令。 It works directly with PDF files, but my application is required to support duplex, different paper source, etc. which is unavailable in lpr .它直接与 PDF 文件一起使用,但我的应用程序需要支持双工、不同纸张来源等,这在lpr中不可用。

How can I print PDF using System.Drawing.Printig (or something other what offers similar functionalities) without conversion to image?如何在不转换为图像的情况下使用 System.Drawing.Printig (或其他提供类似功能的东西)打印 PDF ? I use .NET Core 3.1, my application should be cross-platform.我使用 .NET Core 3.1,我的应用程序应该是跨平台的。

Actually, you need to choose any 2 of 3:实际上,您需要选择 3 个中的任何 2 个:

  1. Send data to printer directly without raster images无需光栅图像直接将数据发送到打印机
  2. Use System.Drawing.Printing使用 System.Drawing.Printing
  3. Cross-platform跨平台

For example, 1 and 2 work fine on Windows.例如,1 和 2 在 Windows 上工作正常。 There are cross-platform PDF libraries that can parse and print PDF directly to System.Drawing.Graphics.有跨平台的 PDF 库可以直接解析和打印 PDF 到 System.Drawing.Graphics。 Look at this sample .看看这个样本

The problem is that System.Drawing does not work well on Linux and macOS.问题是 System.Drawing 在 Linux 和 macOS 上无法正常工作。 It uses libgdiplus and this implementation has many practical issues.它使用libgdiplus ,这个实现有很多实际问题。
For example, libgdiplus does not handle clipping paths well enough.例如,libgdiplus 不能很好地处理剪切路径。 I fixed few related issues there ( #545 , #547 , #552 ) and found serious blockers for further improvements.我在那里修复了一些相关问题( #545#547#552 ),并发现了严重的障碍,可以进一步改进。

The first one is maintainers.第一个是维护者。 They did not respond to questions and reviewed simple pull requests by months.他们没有回答问题,而是按月审查了简单的拉取请求。 It looks that Microsoft is not very interested in this project.看来微软对这个项目不是很感兴趣。

The second blocker is internal architecture.第二个障碍是内部架构。 There are 2 old implementations of regions (based on bitmaps and rectangles) that should be reconsidered.应该重新考虑 2 个旧的区域实现(基于位图和矩形)。 However, without communication with maintainers it is also infeasible to make architectural changes.但是,如果不与维护人员沟通,进行架构更改也是不可行的。

I think you have 2 options:我认为你有两个选择:

  1. Use PDF library that can print to System.Drawing.Graphics without intermediate images (like the sample above).使用 PDF 库可以打印到 System.Drawing.Graphics 没有中间图像(如上面的示例)。 It will work fine on Windows.它可以在 Windows 上正常工作。 On Linux, you will need either:在 Linux 上,您将需要:

    • fix found issues in libgdiplus yourself, use customized version in your production.自己修复 libgdiplus 中发现的问题,在您的生产中使用自定义版本。
    • render PDF to images and then print raster images.将 PDF 渲染为图像,然后打印光栅图像。
  2. Do not use System.Drawing.Printing不要使用 System.Drawing.Printing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM