我可以使用SharpPDF添加段落,而无需指定确切的坐标吗?我不能把第一段放在另一个之下吗?
请告诉我你是否使用过图书馆。
答案 0 :(得分:1)
不能在不指定坐标的情况下一个接一个地添加段落,但是我确实编写了这个样本,它会将段落向下移动并在必要时创建新页面。在这个想要你可以写出文字,段落,绘图,并始终知道“光标”的位置。
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;
}
Y在此API中是向后的,因此792是TOP,0是BOTTOM。我使用Graphics对象来测量字符串的高度,因为Graphics以像素为单位而Pdf以点为单位进行估算以使它们相似。然后我从剩余的Y值中减去高度。
在这个示例中,我一遍又一遍地添加paragraph1
和paragraph2
,在我继续时更新我的Y位置。当我到达页面底部时,我创建了一个新页面并重置了我的Y位置。
这个项目多年来没有看到任何更新,但源代码可用,使用与我所做的类似的东西,你可以自己创建函数,允许你连续添加段落将跟踪它认为下一步应该去的位置的CURSOR位置。