Java 使用iText如何更改新行之间的距离?
我在摆弄itext,我改变了fontsize,结果在我的pdf中出现了一些奇怪的间隔文本: 我想把它变成这样:(原谅图像编辑不好) 这是我用来输入文本的代码:Java 使用iText如何更改新行之间的距离?,java,kotlin,itext,Java,Kotlin,Itext,我在摆弄itext,我改变了fontsize,结果在我的pdf中出现了一些奇怪的间隔文本: 我想把它变成这样:(原谅图像编辑不好) 这是我用来输入文本的代码: private fun setBaseInfo(info: ArrayList<String>): PdfPCell { val cell = PdfPCell() val glue = Chunk(VerticalPositionMark()) val p = Paragraph() p
private fun setBaseInfo(info: ArrayList<String>): PdfPCell
{
val cell = PdfPCell()
val glue = Chunk(VerticalPositionMark())
val p = Paragraph()
p.font.size = 8.0f
for (str in info)
{
p.add(glue)
p.add(str)
p.add("\n")
}
cell.border = Rectangle.NO_BORDER
cell.addElement(p)
return cell
}
private fun setBaseInfo(info:ArrayList):PdfPCell
{
val cell=PdfPCell()
val glue=Chunk(垂直位置标记())
val p=段落()
p、 font.size=8.0f
for(信息中的str)
{
p、 添加(胶水)
p、 添加(str)
p、 添加(“\n”)
}
cell.border=矩形。无边框
单元加法(p)
返回单元
}
这是我提供给它的信息:
private fun foo(): ArrayList<String>
{
val array = ArrayList<String>()
array.add("Hi")
array.add("StackOverflow")
array.add("I'd Like")
array.add("This")
array.add("text")
array.add("to be closer")
array.add("together!")
return array
}
private fun foo():ArrayList
{
val数组=ArrayList()
数组。添加(“Hi”)
add(“StackOverflow”)
add(“我想要”)
数组。添加(“此”)
数组。添加(“文本”)
array.add(“更接近”)
add(“一起!”)
返回数组
}
删除p.add(“\n”)
时,这是输出:
完全披露:这里的前iText员工 我会这样做:
public static void main(String[] args) throws IOException {
// create a temp file to hold the output
File outputFile = File.createTempFile("stackoverflow",".pdf");
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outputFile));
Document layoutDocument = new Document(pdfDocument);
String[] lines = {"Hi","StackOverflow","I'd like","this","text","to","be","closer","together!"};
for(String line : lines){
layoutDocument.add(new Paragraph(line)
.setMultipliedLeading(0.5f)); // this is where the magic happens
}
layoutDocument.close();
pdfDocument.close();
// display the temp file with the default PDF viewer
Desktop.getDesktop().open(outputFile);
}
我改变了两件事:
- 尽可能使用最新版本的iText。您希望从几年的错误修复和更新的体系结构中获益
- 不要使用表格来解决布局问题
- 对段落对象使用前导(MultipledLeading或FixedLeading)来解决问题
public static void main(String[] args) throws IOException {
// create a temp file to hold the output
File outputFile = File.createTempFile("stackoverflow",".pdf");
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outputFile));
Document layoutDocument = new Document(pdfDocument);
String[] lines = {"Hi","StackOverflow","I'd like","this","text","to","be","closer","together!"};
for(String line : lines){
layoutDocument.add(new Paragraph(line)
.setMultipliedLeading(0.5f)); // this is where the magic happens
}
layoutDocument.close();
pdfDocument.close();
// display the temp file with the default PDF viewer
Desktop.getDesktop().open(outputFile);
}
我改变了两件事:
- 尽可能使用最新版本的iText。您希望从几年的错误修复和更新的体系结构中获益
- 不要使用表格来解决布局问题
- 对段落对象使用前导(MultipledLeading或FixedLeading)来解决问题
胶水(垂直位置标记
),这不是创建你所看到的空间的原因吗?胶水是用来将其对齐最右边的,我想是这样的?来源:“可用于添加水平或垂直分隔符。除非实现绘图方法,否则不会绘制任何东西。”,所以我认为你在创造水平分离器?但不确定,如果移除它会发生什么?移除胶水只会将位置更改回页面左侧,但不幸的是,它不会更改间距。如果移除add(“\n”)
部分,它也不会固定吗<代码>添加
,将字符串
作为段落的参数
在内部创建一个块
包装,该包装可能已经包含新行或填充,您可以在每行之间添加粘合(垂直位置标记
),这不是创造你所看到的空间的原因吗?胶水是用来把它对准我想的最右边的?来自:“可以用来添加水平或垂直分隔符。除非你实现绘制方法,否则不会绘制任何东西。”所以我想你是在创建水平分隔符?但不确定,如果移除它会发生什么?移除胶水只会将位置更改回页面左侧,但不幸的是,它不会更改间距。如果移除add(“\n”)
部分,它也不会固定吗add
withString
作为段落的参数
在内部创建一个Chunk
包装器,它可能已经包含了一行新行或paddinghanks,是的,我知道我应该使用最新的版本,但这只是一个我想乱弄一点的版本,非常感谢你的帮助!桌子确实是一个拐杖,但它们真的很有用,但我同意我应该停止使用它们。谢谢,是的,我知道我应该使用最新的版本,但这只是一个版本,因为我想乱弄一点,非常感谢你的帮助!桌子确实是一种拐杖,但它们确实很有用,但我同意我应该停止使用它们。