回答
您可以通過在文檔中插入Chunk.NEWLINE
來觸發換行。這是一個例子。
public static void main(String args[]) {
try {
// create a new document
Document document = new Document(PageSize.A4, 20, 20, 20, 20);
PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
document.open();
document.add(new Paragraph("Hello, World!"));
document.add(new Paragraph("Hello, World!"));
// add a couple of blank lines
document.add(Chunk.NEWLINE);
document.add(Chunk.NEWLINE);
// add one more line with text
document.add(new Paragraph("Hello, World!"));
document.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
下面是一個屏幕截圖,顯示了上面代碼生成的PDF的一部分。
'public static final Chunk NEWLINE = new Chunk(「\ n」);'。傑斯是對的,你也是。順便說一句:添加(新的塊(「\ n \ n \ n」)應該產生三個新行,雖然我還沒有測試過。 – 2010-11-22 22:28:34
並插入表之間的空行,你可以使用這些兩種方法
table.setSpacingBefore();
table.setSpacingAfter();
至少在5.1版本的iText#中,這些方法似乎不存在;但是有一些屬性:'SpacingBefore'和'SpacingAfter' – JYelton 2011-08-23 14:53:16
如果你正在使用表,這比'Chunk.NEWLINE'更有用 – 2014-04-23 15:09:35
你可以嘗試空白短語:
document.add(new Phrase("\n"));
您可以添加空行;
Paragraph p = new Paragraph();
// add one empty line
addEmptyLine(p, 1);
// add 3 empty line
addEmptyLine(p, 3);
private static void addEmptyLine(Paragraph paragraph, int number) {
for (int i = 0; i < number; i++) {
paragraph.add(new Paragraph(" "));
}
}
you can add Blank Line threw PdfContentByte class in itextPdf
package com.pdf.test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class Ranvijay {
public static final String RESULT = "d:/printReport.pdf";
public void createPdf(String filename) throws Exception {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(filename));
document.open();
Font bold = new Font(Font.FontFamily.HELVETICA, 8f, Font.BOLD);
Font normal = new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL);
PdfPTable tabletmp = new PdfPTable(1);
tabletmp.getDefaultCell().setBorder(Rectangle.NO_BORDER);
tabletmp.setWidthPercentage(100);
PdfPTable table = new PdfPTable(2);
float[] colWidths = { 45, 55 };
table.setWidths(colWidths);
String imageUrl = "http://ssl.gstatic.com/s2/oz/images/logo/2x/googleplus_color_33-99ce54a16a32f6edc61a3e709eb61d31.png";
Image image2 = Image.getInstance(new URL(imageUrl));
image2.setWidthPercentage(60);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_TOP);
PdfPCell cell = new PdfPCell();
cell.setBorder(Rectangle.NO_BORDER);
cell.addElement(image2);
table.addCell(cell);
String email = "[email protected]";
String collectionDate = "09/09/09";
Chunk chunk1 = new Chunk("Date: ", normal);
Phrase ph1 = new Phrase(chunk1);
Chunk chunk2 = new Chunk(collectionDate, bold);
Phrase ph2 = new Phrase(chunk2);
Chunk chunk3 = new Chunk("\nEmail: ", normal);
Phrase ph3 = new Phrase(chunk3);
Chunk chunk4 = new Chunk(email, bold);
Phrase ph4 = new Phrase(chunk4);
Paragraph ph = new Paragraph();
ph.add(ph1);
ph.add(ph2);
ph.add(ph3);
ph.add(ph4);
table.addCell(ph);
tabletmp.addCell(table);
PdfContentByte canvas = writer.getDirectContent();
canvas.saveState();
canvas.setLineWidth((float) 10/10);
canvas.moveTo(40, 806 - (5 * 10));
canvas.lineTo(555, 806 - (5 * 10));
canvas.stroke();
document.add(tabletmp);
canvas.restoreState();
PdfPTable tabletmp1 = new PdfPTable(1);
tabletmp1.getDefaultCell().setBorder(Rectangle.NO_BORDER);
tabletmp1.setWidthPercentage(100);
document.add(tabletmp1);
document.close();
}
/**
* Main method.
*
* @param args
* no arguments needed
* @throws DocumentException
* @throws IOException
*/
public static void main(String[] args) throws Exception {
new Ranvijay().createPdf(RESULT);
System.out.println("Done Please check........");
}
}
很好的解釋 – 2014-07-03 07:34:08
而不是使用:
document.add(Chunk.NEWLINE);
使用本:
document.add(new Paragraph(""));
這讓小空間
在創建新的段落時添加空間document.add(new Paragraph 「」)); – abh22ishek 2016-02-08 05:15:04
document.add(new Paragraph(""))
它是以上有效,必須添加一個空字符串,像這樣:
document.add(new Paragraph(" "));
我有一個表後添加空白行,我管理它增加了許多的div,因爲我需要它與填充,頂部的CSS樣式設置它, 喜歡這個。我使用了模板引擎(下劃線)來遍歷我需要添加的行數。
<% var maxRow = 30; var pos = items.models.length; %>
<% for(pos; pos < maxRow; pos++){ %>
<div class="blankRow"></div>
<% }; %>
我的CSS文件:
.blankRow:{ padding-top: 15px;}
希望這有助於你。
您可以使用 「\ n」 個段落
document.add(new Paragraph("\n\n"));
您也可以分隔符之前使用
document.add(new Paragraph());
document.add(new Paragraph());
,如果您使用的是它是好的。
我在另一個問題中發佈了這個,但是我發現在iTextSharp中使用表提供了很高的精度。
document.Add(BlankLineDoc(16));
public static PdfPTable BlankLineDoc(int height)
{
var table = new PdfPTable(1) {WidthPercentage = 100};
table = BlankLineTable(table, height);
return table;
}
public static PdfPTable BlankLineTable(PdfPTable table, int height, int border = Rectangle.NO_BORDER)
{
var cell = new PdfPCell(new Phrase(" "))
{
Border = border,
Colspan = table.NumberOfColumns,
FixedHeight = height
};
table.AddCell(cell);
return table;
}
BlankLineTable可以直接與表
- 1. 如何在RichTextBox中插入空白
- 2. 防止在表中插入空白行
- 3. SQL Server - 在行後插入空白行
- 4. ASP.NET:如何在aspx頁面中插入空白行?
- 5. 如何正確插入XSL-FO中的空白行?
- 6. 避免在數據庫中插入任何空白行
- 7. EPplus要插入空白行數
- 8. 在創建表格之前在文檔中插入空白行
- 9. 如何在VIM中的每個評論(例如「#」)之前插入空白行?
- 10. 如何在SQL查詢中插入空白列?
- 11. QLineEdit InputMask和空白插入
- 12. matplotlib figure插入空白
- 13. For循環 - 在ORACLE中插入空白行
- 14. 你可以在已經轉換的PDF中插入空行嗎?
- 15. 空白PDF生成
- 16. PDF到bytearrayoutpustream空白
- 17. 如何減少Crystal Reports中的空白行/空白行?
- 18. ASP.NET DetailsView在插入後返回空白
- 19. std :: stringstream中的空白插入失敗
- 20. 自動插入空白空間?
- 21. 使用iTextSharp在PDF中添加空白
- 22. 如何退出CKEDITOR中的空白段落插入?
- 23. 如何防止將空白數據插入到MySQL表中?
- 24. 在html中插入兩個單詞之間的空白空間
- 25. 在添加空行時插入空行
- 26. 使用ItemAggregate將空白行插入到Quickbooks採購訂單中
- 27. 在SQLite中插入PDF
- 28. 如何匹配空白行
- 29. 如何刪除空白行?
- 30. 如何在輸入後空白JTextfield
你試過實際插入空字符串時使用?字面上''「''。 – BeemerGuy 2010-11-11 23:03:14
@Beemer:這是我第一次嘗試,但空白的字符串不顯示。我嘗試將它們添加爲裸字符串和新空段落。我在下面添加的解決方案是我發現的第一件作品。 – 2010-11-17 19:12:54
@蜥蜴B;我想iText忽略任何空白或空白字符串。很高興知道。謝謝! – BeemerGuy 2010-11-17 21:35:06