【问题标题】:Format Print Document in C#在 C# 中格式化打印文档
【发布时间】:2014-02-17 09:01:44
【问题描述】:

我是 C# 的新手,需要一些帮助来格式化文档以进行打印。

我已经设法通过此代码与打印机对话:

private void Print(string printer)
    {
        PrintDocument PrintDoc = new PrintDocument();

        PrintDoc.PrinterSettings.PrinterName = printer;

        PrintDoc.PrintPage += new PrintPageEventHandler(PrintPage);
        PrintDoc.Print();
    }

    void PrintPage(object sender, PrintPageEventArgs e)
    {
        e.Graphics.DrawLine(new Pen(Color.Black), new Point(0, 0), new Point(100, 100));
        e.Graphics.DrawString("Hello World", new Font("Times New Roman", 12), new SolidBrush(Color.Black), new Point(45, 45));

    }

这会打印出我的“hello world”字符串。显然 PrintPage 方法是我在网上找到的代码。 可惜没找到办法

a) 设置打印纸张尺寸的格式(138mm x 99mm 横向格式)

b) 告诉打印机在哪里打印我的文本。

论文是预印的表格,我必须在特定字段中写下我的文字。

所以我正在寻找一种方法来为我的打印机提供一个格式化的文档,例如:

<field1>
    <x> 2cm </x>
    <y> 1cm </y>
    <text> textfield1 </text>
</field1>
<field2>
     ....

我找不到有关如何执行此操作的信息。所以如果有人能告诉我如何做到这一点或有一个好的教程的链接,我会非常感激

【问题讨论】:

    标签: c# printing


    【解决方案1】:

    设置纸张尺寸

    printDocument.DefaultPageSettings.PaperSize = new PaperSize("Custom Name", width, height);
    printDocument.DefaultPageSettings.Landscape = true;
    

    宽度和高度以百分之一英寸为单位

    请参阅this SO question 中的教程,了解如何在预印纸上打印文本。

    为了避免在实验过程中浪费纸张,请将预印纸扫描为图像,并在预览期间将其设置为PrintPageHanlder 中的背景。

    void printDocument_PrintPage(object sender, PrintPageEventArgs e)
    {
        if (printDocument.PrintController.IsPreview)
        {
            Image image = Image.FromFile("ScannedImagePath");
            e.Graphics.DrawImage(image,0,0)
        }
        // print other text here after drawing the background
    }           
    

    【讨论】:

    • 谢谢,这正是我想要的。
    • 我很想为您的帖子点赞,遗憾的是我刚刚注册,所以没有足够的声誉来点赞您的评论。我似乎错过了 4 分^^;
    • 我终于有足够的点数来投票了。所以我猜迟到总比没有好:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多