2010-11-11 1754 views
47

我正在使用iText創建PDF。我想在段落和表格之間插入空行。如何在PDF中插入空白行?

我該如何做到這一點?

+0

你試過實際插入空字符串時使用?字面上''「''。 – BeemerGuy 2010-11-11 23:03:14

+0

@Beemer:這是我第一次嘗試,但空白的字符串不顯示。我嘗試將它們添加爲裸字符串和新空段落。我在下面添加的解決方案是我發現的第一件作品。 – 2010-11-17 19:12:54

+0

@蜥蜴B;我想iText忽略任何空白或空白字符串。很高興知道。謝謝! – BeemerGuy 2010-11-17 21:35:06

回答

82

您可以通過在文檔中插入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的一部分。

Hello PDF with blank lines inserted

+0

'public static final Chunk NEWLINE = new Chunk(「\ n」);'。傑斯是對的,你也是。順便說一句:添加(新的塊(「\ n \ n \ n」)應該產生三個新行,雖然我還沒有測試過。 – 2010-11-22 22:28:34

21

並插入表之間的空行,你可以使用這些兩種方法

table.setSpacingBefore();  

table.setSpacingAfter();  
+0

至少在5.1版本的iText#中,這些方法似乎不存在;但是有一些屬性:'SpacingBefore'和'SpacingAfter' – JYelton 2011-08-23 14:53:16

+0

如果你正在使用表,這比'Chunk.NEWLINE'更有用 – 2014-04-23 15:09:35

7

你可以嘗試空白短語:

document.add(new Phrase("\n")); 
2

您可以添加空行;

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(" ")); 
    } 
    } 
4
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........"); 
    } 

} 
+1

很好的解釋 – 2014-07-03 07:34:08

0

而不是使用:

document.add(Chunk.NEWLINE); 

使用本:

document.add(new Paragraph("")); 

這讓小空間

+0

在創建新的段落時添加空間document.add(new Paragraph 「」)); – abh22ishek 2016-02-08 05:15:04

1
document.add(new Paragraph("")) 

它是以上有效,必須添加一個空字符串,像這樣:

document.add(new Paragraph(" ")); 
0

我有一個表後添加空白行,我管理它增加了許多的div,因爲我需要它與填充,頂部的CSS樣式設置它, 喜歡這個。我使用了模板引擎(下劃線)來遍歷我需要添加的行數。

<% var maxRow = 30; var pos = items.models.length; %> 
<% for(pos; pos < maxRow; pos++){ %> 
     <div class="blankRow"></div> 
<% }; %> 

我的CSS文件:

.blankRow:{ padding-top: 15px;} 

希望這有助於你。

4

您可以使用 「\ n」 個段落

document.add(new Paragraph("\n\n")); 
0

您也可以分隔符之前使用

document.add(new Paragraph()); 
document.add(new Paragraph()); 

,如果您使用的是它是好的。

0

我在另一個問題中發佈了這個,但是我發現在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可以直接與表