c#打印预览和实际尺寸不匹配?

新手上路,请多包涵

环境:

Win10 x64, .NET6.0 C# WinForm, VS Professional 2022 ,WPS PDF 虚拟打印机。

问题:

C# WinForm 【打印预览】时尺寸正确, 但打印到 【WPS PDF 虚拟打印机】或【真打印机】时尺寸就不正确了。请问如何解决? 不要用第三方插件。


using System.Diagnostics;
using System.Drawing.Printing;

namespace testPrint2
{
    public partial class Form1 : Form
    {
        //实例化
        PrintDocument printDocument = new PrintDocument();
        public PaperSize size = new PaperSize()
        {
            PaperName = "Custom 200x200",
            RawKind = 0,
            Height = 200,
            Width = 200
        };

        public Form1()
        {
            InitializeComponent();
        }

        //初始化
        private void Form1_Load(object sender, EventArgs e)
        {
            printDocument.DefaultPageSettings.PaperSize = size;
            printDocument.PrintPage += PrintDocument_PrintPage;
        }

        //绘制打印内容
        private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            if (e.Graphics != null)
            {
                e.Graphics.PageUnit = GraphicsUnit.Millimeter;
                Font font = new Font("Arial", 12);
                SolidBrush brush = new SolidBrush(Color.Black);
                PointF point = new PointF(10, 10);
                e.Graphics.DrawString("测试 200x200", font, brush, point);
            }
        }

        //打印
        private void button_Print_Click(object sender, EventArgs e)
        {
            printDocument.Print();
            Debug.WriteLine("*********************");
            Debug.WriteLine(printDocument.DefaultPageSettings.PaperSize);
            Debug.WriteLine("*********************");
        }

        //打印预览
        private void button_Preview_Click(object sender, EventArgs e)
        {
            PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
            printPreviewDialog.Document = printDocument;
            printPreviewDialog.ShowDialog();
        }
    }
}
阅读 2.7k
1 个回答

打印之前设置默认默认纸张大小:

PrinterSettings settings = new PrinterSettings();
settings.DefaultPageSettings.PaperSize = size;
printDocument.PrinterSettings = settings;
logo
Microsoft
子站问答
访问
宣传栏