2010-11-11 670 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可以直接与表