C# PrintDocument - 表格/网格布局问题

发布于 2024-12-22 23:49:55 字数 2446 浏览 1 评论 0原文

我正在尝试以表格/网格格式将一些数据打印到页面上。 目前我已经可以正常工作了,但一切都是手动绘制的。 每个单元格的宽度和高度的相当硬编码的值。

我想做的是将其转变为更通用的解决方案。创建某种包含所有数据的对象并让它基本上呈现自身。

我遇到的问题是找到一种方法来确定每个单元格的高度,确保它足够大以容纳其中包含的数据。

我现在的想法是这样的:

protected override void OnPrintPage(PrintPageEventArgs e)
{
    DrawPageHeader();

    List<ItemDTO> ItemsForPage = GetItemsToRenderOnThisPage();

    for (int i = 1; i < ItemsForPage.Count; i ++)
    {
        //Each column is rendered in this method. The X and Y start positions are passed, plus the width of the column.
        DrawColumn(g, ItemsForPage[0], column1X, totalWidth / ItemsForPage.Count, itemStartY);
    }
}

private int DrawColumnForItem(Graphics g, ItemDTO item, int startPoint, int columnWidth, int rowPosition)
{
    //Draw the vertical Lines
    g.DrawLine(linePen, lineLeft, rowPosition, lineLeft, lineHeight);
    g.DrawLine(linePen, lineRight, rowPosition, lineRight, lineHeight);
    g.DrawLine(linePen, startPoint - 2, rowPosition, startPoint - 2, lineHeight);

    // (85 characters max field) 
    g.DrawLine(linePen, lineLeft, rowPosition, lineRight, rowPosition);
    g.DrawString("Description", smallBoldFont, Brushes.Black, 3, ++rowPosition);
    g.DrawString(item.Desc, smallFont, Brushes.Black, new Rectangle(startPoint, rowPosition, columnWidth, rowIncrement * 2));
    rowPosition += rowIncrement;
    rowPosition += rowIncrement;

    //Details (250 characters max)
    g.DrawLine(linePen, lineLeft, rowPosition, lineRight, rowPosition);
    g.DrawString("Detailed Desc", smallBoldFont, Brushes.Black, 3, ++rowPosition);
    g.DrawString(item.DetailedDescription, smallFont, Brushes.Black, new Rectangle(startPoint, rowPosition, columnWidth, rowIncrement * 2));
    rowPosition += rowIncrement;
    rowPosition += rowIncrement;
    rowPosition += rowIncrement;

    //Gender (M or F)
    g.DrawLine(linePen, lineLeft, rowPosition, lineRight, rowPosition);
    g.DrawString("Gender", smallBoldFont, Brushes.Black, 3, ++rowPosition);
    g.DrawString(item.Gender.ToString(), smallFont, Brushes.Black, startPoint, rowPosition);
    rowPosition += rowIncrement;
}

你明白这个想法了。 我正在画每一条线。 然后是文本,然后递增该行的位置以移动到下一行。

所以上面有3个字段。 每个单元都有不同的最大尺寸,因此每个单元都可能需要更大或更小的“单元”。

所以我想我真正需要的是一种找到给定文本片段将占用多少空间的方法。 g.MeasureString 会给我宽度,但不是高度(如果它换行到多行)。所以我不知道要给它多少行。

如此手动测量的另一个问题是,每次我进行更改时,我都必须重新进行并开始调整以确保一切都一致。

我将制作类似的文档,所以如果大部分都可以自动化那就太好了。

希望这是有道理的。

I'm trying to print out some data to a page in a table / grid format.
At the moment i have it working, but everything is drawn out manually.
Fairly hard-coded values for the width and height of each cell.

What I'm trying to do is turn it onto a more generic solution. Create some kind of object containing all the data and have it basically render itself.

The problem I am having is finding a way to determine the height of each cell, making sure it is big enough for the data contained within it.

What I have at the moment is something like:

protected override void OnPrintPage(PrintPageEventArgs e)
{
    DrawPageHeader();

    List<ItemDTO> ItemsForPage = GetItemsToRenderOnThisPage();

    for (int i = 1; i < ItemsForPage.Count; i ++)
    {
        //Each column is rendered in this method. The X and Y start positions are passed, plus the width of the column.
        DrawColumn(g, ItemsForPage[0], column1X, totalWidth / ItemsForPage.Count, itemStartY);
    }
}

private int DrawColumnForItem(Graphics g, ItemDTO item, int startPoint, int columnWidth, int rowPosition)
{
    //Draw the vertical Lines
    g.DrawLine(linePen, lineLeft, rowPosition, lineLeft, lineHeight);
    g.DrawLine(linePen, lineRight, rowPosition, lineRight, lineHeight);
    g.DrawLine(linePen, startPoint - 2, rowPosition, startPoint - 2, lineHeight);

    // (85 characters max field) 
    g.DrawLine(linePen, lineLeft, rowPosition, lineRight, rowPosition);
    g.DrawString("Description", smallBoldFont, Brushes.Black, 3, ++rowPosition);
    g.DrawString(item.Desc, smallFont, Brushes.Black, new Rectangle(startPoint, rowPosition, columnWidth, rowIncrement * 2));
    rowPosition += rowIncrement;
    rowPosition += rowIncrement;

    //Details (250 characters max)
    g.DrawLine(linePen, lineLeft, rowPosition, lineRight, rowPosition);
    g.DrawString("Detailed Desc", smallBoldFont, Brushes.Black, 3, ++rowPosition);
    g.DrawString(item.DetailedDescription, smallFont, Brushes.Black, new Rectangle(startPoint, rowPosition, columnWidth, rowIncrement * 2));
    rowPosition += rowIncrement;
    rowPosition += rowIncrement;
    rowPosition += rowIncrement;

    //Gender (M or F)
    g.DrawLine(linePen, lineLeft, rowPosition, lineRight, rowPosition);
    g.DrawString("Gender", smallBoldFont, Brushes.Black, 3, ++rowPosition);
    g.DrawString(item.Gender.ToString(), smallFont, Brushes.Black, startPoint, rowPosition);
    rowPosition += rowIncrement;
}

You see the idea.
I'm drawing each line.
Then the text, then incrememnting the position of the row to move onto the next line.

So there are 3 fields above.
Each one has a different max size, and hence each one will potentially need a larger or smaller 'cell'.

So I gues what I really need is a method of finding how much space a given piece of text will take up.
g.MeasureString will give me the width, but not the height (if it wraps to multiple lines). So I dont know how many rows to give it.

Also an issue with it being so manually measured is every time I make a change I have to go through and start tweaking again to make sure everything lines up.

I'm going to be doing similar documents, so it would be great if mostly it could be automated.

Hope this makes sense.

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

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

发布评论

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

评论(1

如梦初醒的夏天 2024-12-29 23:49:56

是的,我知道这已经很旧了,但这并不是让它在没有答案的情况下闲逛的理由。

您需要使用GetHeightMeasureString方法。任何一种都需要您想要测量的字体,因此在可以定义它的地方使用您的smallFont(出于示例目的)

smallFont = New Font("Arial", 8, FontStyle.Regular)

获取高度:

smallFont.GetHeight(g)

您可以通过以下方式

g.MeasureString("TestString", smallFont)

Yeah I know this is old, but that is no reason to have it hanging around without an answer.

You need to use the GetHeight or MeasureString method. Either one requires the font you wish to measure, so using your smallFont where it can be defined (for example purposes)

smallFont = New Font("Arial", 8, FontStyle.Regular)

You can get the height by:

smallFont.GetHeight(g)

alternately

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