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

.NET模拟热敏打印机票据PDF打印,无法设置正确纸张尺寸求助

解决Star TSP650热敏收据PDF模拟的纸张尺寸问题

Star TSP650热敏打印机的标准收据纸张宽度为80mm(约3.15英寸),换算为PDF的点单位(1英寸=72点)是227点,高度可根据收据内容动态调整或设置固定值。以下是使用PdfSharp库生成指定尺寸PDF的C#示例:

步骤1:安装依赖

通过NuGet安装PdfSharp包:

Install-Package PdfSharp

步骤2:C#代码实现

using PdfSharp.Drawing;
using PdfSharp.Pdf;

class ReceiptGenerator
{
    static void Main(string[] args)
    {
        // 创建PDF文档
        PdfDocument document = new PdfDocument();
        document.Info.Title = "Star TSP650模拟收据";

        // 定义TSP650的纸张尺寸:宽80mm≈227点,高设为500点(可根据内容调整)
        XSize paperSize = new XSize(227, 500);
        PdfPage page = document.AddPage();
        page.Width = paperSize.Width;
        page.Height = paperSize.Height;

        // 获取绘图对象
        XGraphics gfx = XGraphics.FromPdfPage(page);
        XFont font = new XFont("Arial", 10, XFontStyle.Regular);

        // 绘制收据内容(示例)
        gfx.DrawString("===== 订单收据 =====", new XFont("Arial", 12, XFontStyle.Bold), XBrushes.Black,
            new XPoint(10, 10));
        gfx.DrawString("订单号: 20240520001", font, XBrushes.Black, new XPoint(10, 40));
        gfx.DrawString("商品: 热敏打印测试纸", font, XBrushes.Black, new XPoint(10, 60));
        gfx.DrawString("金额: 19.99元", font, XBrushes.Black, new XPoint(10, 80));
        gfx.DrawString("===================", new XFont("Arial", 12, XFontStyle.Bold), XBrushes.Black,
            new XPoint(10, 110));

        // 保存PDF
        string outputPath = @"C:\Temp\StarReceipt.pdf";
        document.Save(outputPath);
    }
}

关键说明

  • 纸张尺寸的单位是点(Point),80mm宽度对应的点值通过80 / 25.4 * 72计算得出(25.4是毫米转英寸的系数)。
  • 若需要动态调整纸张高度,可以先绘制所有内容,计算总高度后再设置页面Height属性,避免内容被截断。
  • 如果需要更精准的排版,可以使用XTextFormatter来处理换行和对齐。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 23:25:59