【问题标题】:How to insert blank lines in PDF?如何在PDF中插入空行?
【发布时间】:2011-05-08 16:25:21
【问题描述】:

我正在使用 iText 创建 PDF。我想在段落和表格之间插入空行。

我怎样才能做到这一点?

【问题讨论】:

  • 您是否尝试过实际插入空白字符串?字面意思是" "
  • @Beemer:这也是我尝试的第一件事,但是空白字符串没有出现。我尝试将它们添加为裸字符串和新的空段落。我在下面添加的解决方案是我发现的第一件事。
  • @蜥蜴比尔;我猜 iText 会忽略任何空字符串或空格字符串。很高兴知道。谢谢!
  • 只需插入“\n”,就可以了。
  • 遇到PDFAWriter怎么办?

标签: itext


【解决方案1】:

您可以通过在文档中插入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");。杰斯是对的,你也是。顺便说一句:add(new Chunk("\n\n\n") 应该产生三个新行,虽然我还没有测试过。
【解决方案2】:

要在表格之间插入空行,您可以使用这两种方法

table.setSpacingBefore();      

table.setSpacingAfter();       

【讨论】:

  • 至少在 iText# 的 5.1.1 版本中,这些方法似乎不存在;但是有属性:SpacingBeforeSpacingAfter
  • 如果您使用的是表格,这比Chunk.NEWLINE 有用得多。
【解决方案3】:

您可以尝试一个空白短语:

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

【讨论】:

    【解决方案4】:

    你可以添加空行;

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

    【讨论】:

      【解决方案5】:

      您可以在itextPdf 中添加Blank Line throw PdfContentByte 类。如下图:

      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 = "ranvijay9286@gmail.com";
              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........");
          }
      
      }
      

      【讨论】:

        【解决方案6】:

        而不是使用:

        document.add( Chunk.NEWLINE );
        

        使用这个:

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

        空间小

        【讨论】:

        • 在创建新段落时添加空格 document.add(new Paragraph(" "));
        【解决方案7】:
        document.add(new Paragraph("")) 
        

        上面无效,必须加一个空字符串,像这样:

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

        【讨论】:

          【解决方案8】:

          我必须在表格后添加空白行,并根据需要使用带有 padding-top 设置的 css 样式来管理它添加许多 div,就像这样。我使用了一个模板引擎(下划线)来遍历我需要添加的行数。

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

          我的 css 文件:

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

          【讨论】:

            【解决方案9】:

            段落中可以使用“\n”

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

            【讨论】:

              【解决方案10】:

              你也可以使用

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

              在分隔符之前,如果你使用任何一个都可以。

              【讨论】:

                【解决方案11】:

                我在另一个问题中发布了这个,但我发现使用带有 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

                【讨论】:

                  【解决方案12】:

                  直接使用

                  paragraph.add("\n");
                  

                  添加一个空行。

                  【讨论】:

                    猜你喜欢
                    • 2010-10-26
                    • 1970-01-01
                    • 2022-01-02
                    • 2021-04-26
                    • 2012-12-22
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多