应用于段落的 itext spacingBefore 属性 导致新页面
itext spacingBefore property applied to Paragraph causes new page
我的 itext 库有一个问题,描述如下:
我想在第二段中使用 spacingBefore 属性 在两个段落之间放置一个垂直的 space。
问题是,从 space 单位的某个值(默认点单位)开始,itext 导致第二段显示在新页面上,即使显然有足够的 space将 2 个段落放在同一页上。
这段代码说明了这种情况:
public static void main(String[] args) throws Exception {
Document document = new Document();
OutputStream result = new FileOutputStream("output.pdf");
PdfWriter.getInstance(document, result);
document.open();
Paragraph paragraph1 = new Paragraph("First paragraph");
Paragraph paragraph2 = new Paragraph("Second paragraph");
//380 causes the new page...
paragraph2.setSpacingBefore(380f);
//...whereas 370 does not
// paragraph2.setSpacingBefore(370f);
document.add(paragraph1);
document.add(paragraph2);
document.close();
}
有人对这种奇怪的行为有解释吗?
提前致谢
我已将您的代码复制到一个独立示例中。你可以在这里找到这个例子:ParagraphSpacingBefore
public void createPdf(String filename) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
Paragraph paragraph1 = new Paragraph("First paragraph");
Paragraph paragraph2 = new Paragraph("Second paragraph");
paragraph2.setSpacingBefore(380f);
document.add(paragraph1);
document.add(paragraph2);
document.close();
}
我认为这与您所做的没有什么不同,但就我而言,这两段内容在一个页面上。请查看 paragraph_spacebefore.pdf 自行查找。
也许您还在 paragraph1
之前应用了间距,或者您使用的是旧版本的 iText(例如 2.1.7,一个过时的版本,不应再使用)或者您可能正在使用 iText 的非官方版本(例如 iText 4,一个由第三方创建的版本,没有人知道它是否合法使用)。
简而言之:问题无法解释,因为问题无法复现
正如 Bruno 所述,我使用的是 "old" 有缺陷的 itext 版本:2012 年发布的 5.1.2。这个错误已由 5.5.1 版本修复,如变更日志中所述 http://itextpdf.com/changelog/551 :
Bugfix regarding spacing before and after when a new page is created.
使用较新版本时,一切正常:)
我的 itext 库有一个问题,描述如下:
我想在第二段中使用 spacingBefore 属性 在两个段落之间放置一个垂直的 space。
问题是,从 space 单位的某个值(默认点单位)开始,itext 导致第二段显示在新页面上,即使显然有足够的 space将 2 个段落放在同一页上。
这段代码说明了这种情况:
public static void main(String[] args) throws Exception {
Document document = new Document();
OutputStream result = new FileOutputStream("output.pdf");
PdfWriter.getInstance(document, result);
document.open();
Paragraph paragraph1 = new Paragraph("First paragraph");
Paragraph paragraph2 = new Paragraph("Second paragraph");
//380 causes the new page...
paragraph2.setSpacingBefore(380f);
//...whereas 370 does not
// paragraph2.setSpacingBefore(370f);
document.add(paragraph1);
document.add(paragraph2);
document.close();
}
有人对这种奇怪的行为有解释吗?
提前致谢
我已将您的代码复制到一个独立示例中。你可以在这里找到这个例子:ParagraphSpacingBefore
public void createPdf(String filename) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
Paragraph paragraph1 = new Paragraph("First paragraph");
Paragraph paragraph2 = new Paragraph("Second paragraph");
paragraph2.setSpacingBefore(380f);
document.add(paragraph1);
document.add(paragraph2);
document.close();
}
我认为这与您所做的没有什么不同,但就我而言,这两段内容在一个页面上。请查看 paragraph_spacebefore.pdf 自行查找。
也许您还在 paragraph1
之前应用了间距,或者您使用的是旧版本的 iText(例如 2.1.7,一个过时的版本,不应再使用)或者您可能正在使用 iText 的非官方版本(例如 iText 4,一个由第三方创建的版本,没有人知道它是否合法使用)。
简而言之:问题无法解释,因为问题无法复现
正如 Bruno 所述,我使用的是 "old" 有缺陷的 itext 版本:2012 年发布的 5.1.2。这个错误已由 5.5.1 版本修复,如变更日志中所述 http://itextpdf.com/changelog/551 :
Bugfix regarding spacing before and after when a new page is created.
使用较新版本时,一切正常:)