如何在 SharpPDF 中创建段落而不指定每个段落的确切坐标?

发布于 2024-11-05 15:22:03 字数 82 浏览 0 评论 0原文

我可以使用 SharpPDF 添加段落,而无需指定确切的坐标吗?我不能将一个段落放在另一个段落下面吗?

请告诉我您是否使用过该图书馆。

Can I add paragraphs with SharpPDF, without having to specify the exact coordinates? Can't I just place paragraphs one under the other?

Please tell me if you used the library.

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

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

发布评论

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

评论(1

稚气少女 2024-11-12 15:22:03

不可能在不指定坐标的情况下逐个添加段落,但是我确实编写了此示例,它将在页面中向下移动段落并在必要时创建一个新页面。在这种情况下,您可以写出文本、段落、绘图,并且始终知道“光标”的位置。

const int WIDTH = 500;
const int HEIGHT = 792;

pdfDocument myDoc;
pdfPage currentPage;

private void button1_Click(object sender, EventArgs e)
{
    int height = 0;

    myDoc = new pdfDocument("TUTORIAL", "ME");
    currentPage = myDoc.addPage(HEIGHT, WIDTH);

    string paragraph1 = "All the goats live in the land of the trees and the bushes, " 
        + " when a person lives in the land of the trees and the bushes they wonder about the sanity" 
        + " of it all. Whatever.";

    string paragraph2 =  "Redwood National and State Parks is located in northernmost coastal "
        + "California — about 325 miles north of San Francisco, Calif. Roughly 50 miles long, the parklands"
        + "stretch from near the Oregon border in the north to the Redwood Creek watershed southeast of"
        + "Orick, Calif. Five information centers are located along this north-south corrdior. Park "
        + "Headquarters is located in Crescent City, Calif. (95531) at 1111 Second Street.";

    int iYpos = HEIGHT;

    for (int ix = 0; ix < 10; ix++)
    {
        height = GetStringHeight(paragraph1, new Font("Helvetica", 12), WIDTH);
        iYpos = CheckHeight(height, iYpos);
        currentPage.addParagraph(paragraph1, 0, iYpos, sharpPDF.Enumerators.predefinedFont.csHelvetica, 12, WIDTH);
        iYpos -= height;

        height = GetStringHeight(paragraph2, new Font("Helvetica", 12), WIDTH);
        iYpos = CheckHeight(height, iYpos);
        currentPage.addParagraph(paragraph2, 0, iYpos, sharpPDF.Enumerators.predefinedFont.csHelvetica, 12, WIDTH);
        iYpos -= height;
    }

    string tmp = Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + ".pdf";
    myDoc.createPDF(tmp);
}

private int GetStringHeight(string text, Font font, int width)
{
    Bitmap b = new Bitmap(WIDTH, HEIGHT);
    Graphics g = Graphics.FromImage((Image)b);
    SizeF size = g.MeasureString(text, font, (int)Math.Ceiling((float)width / 72F * g.DpiX));
    return (int)Math.Ceiling(size.Height)
}

private int CheckHeight(int height, int iYpos)
{
    if (height > iYpos)
    {
        currentPage = myDoc.addPage(HEIGHT, WIDTH);
        iYpos = HEIGHT;
    }
    return iYpos;
}

此 API 中的 Y 是向后的,因此 792 是顶部,0 是底部。我使用 Graphics 对象来测量字符串的高度,因为 Graphics 以像素为单位,而 Pdf 以点为单位,所以我进行了估计以使它们相似。然后我从剩余的 Y 值中减去高度。

在这个例子中,我不断地一遍又一遍地添加 paragraph1paragraph2,不断更新我的 Y 位置。当我到达页面底部时,我创建一个新页面并重置我的 Y 位置。

这个项目已经很多年没有看到任何更新了,但是源代码是可用的,使用与我所做的类似的东西,您可以创建自己的函数,允许您连续添加将跟踪的段落它认为接下来应该进行某事的游标位置。

It is not possible to just add paragraphs one after another without specifying coordinates, however I did write this sample which will move the paragraphs down the page and create a new page when necessary. In this want you could write out text, paragraphs, drawing, and always know the position of the "cursor."

const int WIDTH = 500;
const int HEIGHT = 792;

pdfDocument myDoc;
pdfPage currentPage;

private void button1_Click(object sender, EventArgs e)
{
    int height = 0;

    myDoc = new pdfDocument("TUTORIAL", "ME");
    currentPage = myDoc.addPage(HEIGHT, WIDTH);

    string paragraph1 = "All the goats live in the land of the trees and the bushes, " 
        + " when a person lives in the land of the trees and the bushes they wonder about the sanity" 
        + " of it all. Whatever.";

    string paragraph2 =  "Redwood National and State Parks is located in northernmost coastal "
        + "California — about 325 miles north of San Francisco, Calif. Roughly 50 miles long, the parklands"
        + "stretch from near the Oregon border in the north to the Redwood Creek watershed southeast of"
        + "Orick, Calif. Five information centers are located along this north-south corrdior. Park "
        + "Headquarters is located in Crescent City, Calif. (95531) at 1111 Second Street.";

    int iYpos = HEIGHT;

    for (int ix = 0; ix < 10; ix++)
    {
        height = GetStringHeight(paragraph1, new Font("Helvetica", 12), WIDTH);
        iYpos = CheckHeight(height, iYpos);
        currentPage.addParagraph(paragraph1, 0, iYpos, sharpPDF.Enumerators.predefinedFont.csHelvetica, 12, WIDTH);
        iYpos -= height;

        height = GetStringHeight(paragraph2, new Font("Helvetica", 12), WIDTH);
        iYpos = CheckHeight(height, iYpos);
        currentPage.addParagraph(paragraph2, 0, iYpos, sharpPDF.Enumerators.predefinedFont.csHelvetica, 12, WIDTH);
        iYpos -= height;
    }

    string tmp = Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + ".pdf";
    myDoc.createPDF(tmp);
}

private int GetStringHeight(string text, Font font, int width)
{
    Bitmap b = new Bitmap(WIDTH, HEIGHT);
    Graphics g = Graphics.FromImage((Image)b);
    SizeF size = g.MeasureString(text, font, (int)Math.Ceiling((float)width / 72F * g.DpiX));
    return (int)Math.Ceiling(size.Height)
}

private int CheckHeight(int height, int iYpos)
{
    if (height > iYpos)
    {
        currentPage = myDoc.addPage(HEIGHT, WIDTH);
        iYpos = HEIGHT;
    }
    return iYpos;
}

The Y is backwards in this API, so 792 is the TOP and 0 is the BOTTOM. I use a Graphics object to measure the height of the string, since the Graphics is in pixels and the Pdf is in points I do an estimation to make them similar. I then subtract the height from my remaining Y value.

In this example I keep adding paragraph1 and paragraph2 over and over, updating my Y position as I go along. When I get to the bottom of the page I create a new page and reset my Y position.

This project hasn't seen any updates for a number of years, but the source code is available, using something similar to what I've done you could make your own function that allows you to successively add paragraphs which will keep track of a CURSOR position of where it thinks something should go next.

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