You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# System.Drawing打印PDF至全页:解决模糊不清问题

问题描述

开发了一个Windows后台服务,监听Web应用的打印指令。Web应用发送PDF文件、打印机纸张宽高参数及打印份数,通过以下逻辑实现打印:

  1. PDF转图像(使用Spire.PDF)
  2. 处理PrintPage事件绘制图像
  3. 配置PrintDocument并执行打印

当前纸张尺寸正确(标签打印机+自定义尺寸标签纸),但打印内容模糊、难以辨认,部分区域杂乱,需要优化实现清晰高质量的全页打印。


优化方案

1. 提升PDF转图像的分辨率

默认转图分辨率过低是模糊的核心原因,需指定与打印机匹配的高DPI参数:

public Image ConverPdfToImage(IFormFile pdfFile)
{
    if (pdfFile == null || pdfFile.Length == 0)
    {
        return null;
    }
    var document = new Spire.Pdf.PdfDocument();
    document.LoadFromStream(pdfFile.OpenReadStream());
    
    // 匹配打印机DPI(标签打印机通常为203/300DPI,按需调整)
    int targetDpi = 300;
    Image image = document.SaveAsImage(0, targetDpi, targetDpi, PdfImageType.Bitmap);
    
    return image;
}

2. 修正PrintPage事件的图像绘制逻辑

避免拉伸失真,改用等比缩放绘制,同时优化绘图参数:

private void PrintPage(object sender, PrintPageEventArgs ev, Image img)
{
    // 获取打印机实际可打印区域(排除硬边距)
    Rectangle printArea = ev.PageSettings.PrintableArea;
    
    // 计算等比缩放比例,避免图像拉伸
    float scaleX = (float)printArea.Width / img.Width;
    float scaleY = (float)printArea.Height / img.Height;
    float scale = Math.Min(scaleX, scaleY);
    
    // 计算绘制位置(这里按左上角对齐,需居中可调整为(printArea.Width - drawWidth)/2)
    int drawWidth = (int)(img.Width * scale);
    int drawHeight = (int)(img.Height * scale);
    int x = printArea.Left;
    int y = printArea.Top;
    
    // 配置高质量绘图参数,关闭不必要的插值
    ev.Graphics.CompositingQuality = CompositingQuality.HighQuality;
    ev.Graphics.SmoothingMode = SmoothingMode.HighQuality;
    ev.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
    ev.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor; // 避免模糊插值
    
    // 绘制图像
    ev.Graphics.DrawImage(img, x, y, drawWidth, drawHeight);
}

3. 同步PrintDocument的打印分辨率

确保打印配置与转图DPI一致,强化输出质量:

PrintDocument printDocument = new PrintDocument();
printDocument.PrinterSettings.PrinterName = form.Printer;
printDocument.DefaultPageSettings.Landscape = form.Landscape;
printDocument.DefaultPageSettings.PrinterSettings.Copies = form.Count;

printDocument.DefaultPageSettings.PaperSize = GetPaperSize(form.Width, form.Height);
printDocument.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);

// 设置打印分辨率,与转图DPI保持一致
printDocument.DefaultPageSettings.PrintResolution = new PrintResolution(PrintResolutionKind.Custom, 300, 300);

Image img = ConverPdfToImage(form.PrintFile);
printDocument.PrintPage += (sender, args) => this.PrintPage(sender, args, img);
printDocument.Print();

4. 终极优化:直接打印PDF(跳过转图步骤)

转图会损失精度,推荐使用Spire.PDF原生打印功能,彻底解决模糊问题:

public void PrintPdfDirectly(IFormFile pdfFile, string printerName, int copies, bool landscape, PaperSize paperSize)
{
    if (pdfFile == null || pdfFile.Length == 0) return;
    
    var document = new Spire.Pdf.PdfDocument();
    document.LoadFromStream(pdfFile.OpenReadStream());
    
    document.PrintSettings.PrinterName = printerName;
    document.PrintSettings.Copies = (short)copies;
    document.PrintSettings.Landscape = landscape;
    document.PrintSettings.PaperSize = paperSize;
    document.PrintSettings.Margins = new PdfMargins(0); // 无边距打印
    
    document.Print();
}

内容的提问来源于stack exchange,提问作者afshar003

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 10:50:15