【问题标题】:Printer Printing Cutting Off Strings打印机打印切断字符串
【发布时间】:2018-11-20 05:10:18
【问题描述】:

我正在使用此代码从富文本框中打印字符串集合:

private void printBtn_Click(object sender, EventArgs e)
    {
        PrintDocument p = new PrintDocument();
        p.OriginAtMargins = true;
        Margins pMargins = new Margins(100, 100, 100, 100);
        p.DefaultPageSettings.Margins = pMargins;

        p.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
        {
            e1.Graphics.DrawString(summaryBox.Text, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));
        };
        try
        {
            p.Print();
            this.Close();
        }
        catch (Exception ex)
        {
            throw new Exception("Error During Printing", ex);
        }
    }

但是,长字符串会被切断并且不会自动切断。字符串向右截断,第一页填满后,第二页不打印,剩余内容被忽略。是打印机设置有问题还是富文本框有问题,或者两者都有?如果内容太长,如何确保长字符串换行并打印第二页或第三页?

【问题讨论】:

  • 我在下面添加了我的答案。如果有帮助,请在答案左侧标记勾选以使其变为绿色并通过单击向上箭头进行投票来回答:)

标签: c# printing


【解决方案1】:

您需要使用PrintPageEventArgs.MarginBounds 来获取打印页面中的可打印区域。

现在你的e1PrintPageEventArgs 类型的参数。你会得到MarginBounds,就像e1.MarginBounds一样。

所以你的代码将是。

p.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
{
    e1.Graphics.DrawString(summaryBox.Text, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, e1.MarginBounds.Width, e1.MarginBounds.Height));
};

【讨论】:

  • 使用此代码,一旦打印了第一页,字符串就会被切断,第二页不会打印。我如何解释可能需要打印两页的文本框?此外,页面上的最后一个单词甚至被打印了一半,水平切割。
  • 能否请您将richtectbox 中的示例数据添加到您的帖子中,以便我进行测试
  • @Arcaster,请将richtextbox 中的示例数据添加到您的帖子中,以便我解决您的所有问题
猜你喜欢
  • 1970-01-01
  • 2012-09-04
  • 1970-01-01
  • 2014-10-11
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 2015-05-30
  • 1970-01-01
相关资源
最近更新 更多