首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在不指定每个段落在SharpPDF中的确切坐标的情况下创建段落?

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

Stack Overflow用户
提问于 2011-05-06 21:11:23
回答 1查看 339关注 0票数 2

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

如果你用过图书馆,请告诉我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-27 07:38:40

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

代码语言:javascript
复制
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位置。

这个项目已经很多年没有看到任何更新了,但源代码是可用的,使用类似于我所做的事情,你可以制作自己的函数,允许你连续添加段落,它将跟踪光标位置,它认为下一步应该去哪里。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5911931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档