我正在尝试添加一个选项,以便在使用iTextSharp 5生成的PDF中向下移动文本。以前,用户会单击一个按钮,然后生成PDF。现在,我添加一个选项,他们点击相同的按钮,选择顶部,中间或底部,生成相同的PDF,但文本向下移动到中间,或者如果他们这样做,则接近页面底部不要选择Top。我已经有弹出工作,并且为if语句设置了变量,我只需要在iText5中找到正确的命令,根据Top,Middle或Bottom变量的if语句将所有添加到文档中的文本向下移动。
这是我当前的代码,一切正常,除了“moveText”命令,这是在线推荐,似乎只有iText7支持(我也不确定该命令是否可以用于整个页面,或者需要无论如何都要指出具体的段落)。
iTextSharp.text.Document document = new
iTextSharp.text.Document(PageSize.A4, 25, 25, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
// Create an instance to the PDF file by creating an instance of the PDF
// Writer class using the document and the filestrem in the constructor.
// Open the document to enable you to write to the document
document.Open();
// Set values of paragraphs
Chunk glue = new Chunk(new VerticalPositionMark());
Paragraph p1 = new Paragraph("CaseType: " + type, fontLb);
p1.Add(new Chunk(glue));
p1.Add("Date Filed: " + datefiled);
Chunk glue2 = new Chunk(new VerticalPositionMark());
Paragraph p2 = new Paragraph("Case No: " + caseno, fontLb);
p2.Add(new Chunk(glue2));
p2.Add("Amount of Suit: " + claim);
Paragraph p3 = new Paragraph("Section: " + divsec + "\n", fontLb);
p3.Alignment = Rectangle.ALIGN_LEFT;
Paragraph p4 = new Paragraph("AUSTIN BADON, CLERK", fontS);
p4.Alignment = Rectangle.ALIGN_CENTER;
Paragraph p5 = new Paragraph(caption, fontSb);
p5.Alignment = Rectangle.ALIGN_CENTER;
//Add previously defined paragraphs
document.Add(p1);
document.Add(p2);
document.Add(p3);
document.Add(p4);
document.Add(p5);
// Check if Middle or Bottom were selected, if so, move text down appropriate distance.
if (btnFolderLblBot.Checked)
{
moveText(0, -400);
}
else if (btnFolderLblMid.Checked)
{
moveText(0, -200);
}
else
{
moveText(0, 0);
}
// Close the document
document.Close();
// Close the writer instance
writer.Close();
// Always close open filehandles explicity
fs.Close();
显而易见的预期结果是,当btnFolderBot.Checked = true时,生成的PDF的整个文本向下移动到页面底部,当btnFolderMid.Checked = True时,生成的PDF的整个文本向下移动到中间的页面。如果btnFolderTop.Checked = true,或者由于某种原因无法正常工作,则文本根本不会移动。
我提出的解决方案(很可能不是最优雅的,因为我对iTextSharp很新,而且一般是C#),在正确设置3个复选框值后使用了以下内容:
if (btnFolderLblBot.Checked)
{
document.SetMargins(40, 40, 522, 66);
}
else if (btnFolderLblMid.Checked)
{
document.SetMargins(40, 40, 306, 288);
}
else
{
document.SetMargins(40, 40, 81, 510);
}
这简单地改变了iTextSharp用来放置矩形的边距,有效地上下移动它。在我的情况下,我试图将文本定位到顶部,中间或底部,以便在打印时适合Avery标签,因此使页面的其余部分无法使用对我来说实际上是理想的。它可能不适合其他想要移动文本但仍能将整个页面用于其他文本的人。