自动换行文本到打印页面?

发布于 2024-10-17 14:04:53 字数 528 浏览 1 评论 0原文

我有一些打印字符串的代码,但如果字符串是:“Blah blah blah”...并且没有换行符,则文本占据一行。我希望能够塑造字符串的形状,使其自动换行到纸张的尺寸。

private void PrintIt(){
    PrintDocument document = new PrintDocument();
    document.PrintPage += (sender, e) => Document_PrintText(e, inputString);
    document.Print();
}

static private void Document_PrintText(PrintPageEventArgs e, string inputString) {
    e.Graphics.DrawString(inputString, new Font("Courier New", 12), Brushes.Black, 0, 0);
}

我想我可以计算出字符的长度,并手动换行文本,但如果有内置的方法可以做到这一点,我宁愿这样做。谢谢!

I have some code that prints a string, but if the string is say: "Blah blah blah"... and there are no line breaks, the text occupies a single line. I would like to be able to shape the string so it word wraps to the dimensions of the paper.

private void PrintIt(){
    PrintDocument document = new PrintDocument();
    document.PrintPage += (sender, e) => Document_PrintText(e, inputString);
    document.Print();
}

static private void Document_PrintText(PrintPageEventArgs e, string inputString) {
    e.Graphics.DrawString(inputString, new Font("Courier New", 12), Brushes.Black, 0, 0);
}

I suppose I could figure out the length of a character, and wrap the text manually, but if there is a built in way to do this, I'd rather do that. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时间海 2024-10-24 14:04:53

是的,有 DrawString 能够自动换行文本。您可以使用 MeasureString 方法来检查指定的字符串是否可以完全绘制页面与否以及需要多少空间。

还有一个 TextRenderer 专门用于此目的的类。

这是一个示例:

         Graphics gf = e.Graphics;
         SizeF sf = gf.MeasureString("shdadj asdhkj shad adas dash asdl asasdassa", 
                         new Font(new FontFamily("Arial"), 10F), 60);
         gf.DrawString("shdadj asdhkj shad adas dash asdl asasdassa", 
                         new Font(new FontFamily("Arial"), 10F), Brushes.Black,
                         new RectangleF(new PointF(4.0F,4.0F),sf), 
                         StringFormat.GenericTypographic);

这里我指定了最大 60 像素作为宽度,然后测量字符串将给出绘制该字符串所需的大小。现在,如果您已经有了一个 Size,那么您可以与返回的 Size 进行比较,看看它是否会被正确绘制或截断

Yes there is the DrawString has ability to word wrap the Text automatically. You can use MeasureString method to check wheather the Specified string can completely Drawn on the Page or Not and how much space will be required.

There is also a TextRenderer Class specially for this purpose.

Here is an Example:

         Graphics gf = e.Graphics;
         SizeF sf = gf.MeasureString("shdadj asdhkj shad adas dash asdl asasdassa", 
                         new Font(new FontFamily("Arial"), 10F), 60);
         gf.DrawString("shdadj asdhkj shad adas dash asdl asasdassa", 
                         new Font(new FontFamily("Arial"), 10F), Brushes.Black,
                         new RectangleF(new PointF(4.0F,4.0F),sf), 
                         StringFormat.GenericTypographic);

Here i have specified maximum of 60 Pixels as width then measure string will give me Size that will be required to Draw this string. Now if you already have a Size then you can compare with returned Size to see if it will be Drawn properly or Truncated

哀由 2024-10-24 14:04:53

我发现了这个:如何:在 Windows 窗体中打印多页文本文件< /a>

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
    int charactersOnPage = 0;
    int linesPerPage = 0;

    // Sets the value of charactersOnPage to the number of characters 
    // of stringToPrint that will fit within the bounds of the page.
    e.Graphics.MeasureString(stringToPrint, this.Font,
        e.MarginBounds.Size, StringFormat.GenericTypographic,
        out charactersOnPage, out linesPerPage);

    // Draws the string within the bounds of the page
    e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
        e.MarginBounds, StringFormat.GenericTypographic);

    // Remove the portion of the string that has been printed.
    stringToPrint = stringToPrint.Substring(charactersOnPage);

    // Check to see if more pages are to be printed.
    e.HasMorePages = (stringToPrint.Length > 0);
}

I found this : How to: Print a Multi-Page Text File in Windows Forms

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
    int charactersOnPage = 0;
    int linesPerPage = 0;

    // Sets the value of charactersOnPage to the number of characters 
    // of stringToPrint that will fit within the bounds of the page.
    e.Graphics.MeasureString(stringToPrint, this.Font,
        e.MarginBounds.Size, StringFormat.GenericTypographic,
        out charactersOnPage, out linesPerPage);

    // Draws the string within the bounds of the page
    e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
        e.MarginBounds, StringFormat.GenericTypographic);

    // Remove the portion of the string that has been printed.
    stringToPrint = stringToPrint.Substring(charactersOnPage);

    // Check to see if more pages are to be printed.
    e.HasMorePages = (stringToPrint.Length > 0);
}
凌乱心跳 2024-10-24 14:04:53

老兄,我正在为 HTML 打印而烦恼,这简直就是噩梦。我想说,在我看来,您应该尝试使用其他东西来打印文本等,将参数传递给报告服务并弹出用户可以打印的 PDF。

或者您可能需要计算字符数并显式指定换行符!

Dude im suffering with printing in HTML, total nightmare. I would say that in my opinion you should try use something else to print out the text etc pass parameters to reporting services and popup a PDF which the user can print off.

Or potentially you might need to count out the number of characters and explicitly specify a line break!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文