有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

iText旧版本中LineSeparator的java替代方案?

我试图用iText在文档中插入一个行分隔符(你知道,横穿文档的水平行)。我通过谷歌找到了一些使用com的资源。洛瓦吉。文本pdf。画LineSeparator,但是我使用的iText版本(1.4.2)似乎没有这个包

有人能给我的pdf添加一个好的行分隔符吗?请不要说更新。jar——我被锁定在1.4.2中

谢谢


共 (6) 个答案

  1. # 1 楼答案

    只需向pdf文档对象添加一个行分隔符对象。应该是这样

    LineSeparator objectName = new LineSeparator();              
    document.add(objectName);
    
  2. # 2 楼答案

    LineSeparator ls = new LineSeparator();
    document.add(new Chunk(ls));
    

    示例:iText in action

  3. # 3 楼答案

    我也面临类似的问题,因为我的公司也在使用旧版本的iText,即1.4.2。这是我为创建水平规则而提出的两种解决方案。第一个使用图形,第二个使用带底部边框的表格。这两种方法对我都适用

    解决方案1:

    protected static final Graphic HR = new Graphic();
      static {
        HR.setHorizontalLine(1f, 100f, Color.BLACK);
      }
    

    解决方案2:

        private static void addHorizontalLine(Document document, PdfWriter writer) throws DocumentException, IOException{
        PdfPTable myTable = new PdfPTable(1);
        myTable.setWidthPercentage(100.0f);
        PdfPCell cellOne = new PdfPCell();
        cellOne.setBorder(Rectangle.BOTTOM);
        document.add(new Paragraph(" "));
        document.add(myTable);
    }
    

    PS:我们不能更新JAR的原因是旧版本的iText可以免费用于商业用途,而新版本是付费的

    希望能有帮助

  4. # 4 楼答案

    我也赞成使用行元素而不是表。。。不要重复HTML格式错误

    final LineSeparator lineSeparator = new LineSeparator();
    lineSeparator.drawLine(pdfCB, leftX, rightX, y);
    
  5. # 5 楼答案

    Sean给出的解决方案在处理带有行分隔符下划线的文本时提供了更大的灵活性。 我不知道LineSeparator是否能做到这一点,它似乎只是一个行分隔符

    Paragraph ph = new Paragraph(new Phrase("My line separator", yourFont));
    PdfPCell cell = new PdfPCell(ph);
    cell.Border = Rectangle.BOTTOM_BORDER;
    cell.BorderColor = new BaseColor(44, 67, 144);
    cell.BorderWidth = 2f;
    
    PdfPTable table = new PdfPTable(1);                
    table.AddCell(cell);
    table.HorizontalAlignment = Element.ALIGN_LEFT;
    table.WidthPercentage = 100f;
    doc.Add(table);
    

    希望这能有所帮助。 应该打印这样的东西A line separator with text

  6. # 6 楼答案

    在iText的早期版本中,这一点有点混乱。如果将元素存储在PdfPCell中水平线上方,则可以将其边框设置为仅显示底部。(如果需要,该单元格也可以为空)

    PdfPCell myCell = new PdfPCell(new Paragraph("Hello World") );
    myCell.setBorder(Rectangle.BOTTOM);
    

    结果应该是(实线,而不是方格线)

    Hello World
    -----------
    

    这会给你你想要的。这不是最佳解决方案,但它是一种绕过旧罐子局限性的方法

    作为参考,如果您想执行此技巧,请在文本的上下各放一行,以给出

    -----------
    Hello World
    -----------
    

    setBorder()的参数是一个int,可以对其使用逐位运算来处理值。因此,上述示例可以通过

    myCell.setBorder(Rectangle.BOTTOM | Rectangle.TOP);
    

    编辑:示例

    //Create the table which will be 2 Columns wide and make it 100% of the page
    PdfPTable myTable = new PdfPtable(2);
    myTable.setWidthPercentage(100.0f);
    
    //create a 3 cells and add them to the table
    PdfPCell cellOne = new PdfPCell(new Paragraph("Hello World"));
    PdfPCell cellTwo = new PdfPCell(new Paragraph("Bottom Left"));
    PdfPcell cellThree = new PdfPCell(new Paragraph("Bottom Right"));
    
    cellOne.setColspan(2);
    cellOne.setBorder(Rectangle.BOTTOM);
    cellOne.setHorizontalAlignment(Element.ALIGN_LEFT);
    
    cellTwo.setBorder(Rectangle.NO_BORDER);
    cellTwo.setHorizontalAlignment(Element.ALIGN_LEFT);
    cellThree.setBorder(Rectangle.LEFT);
    cellThree.setHorizontalAlignment(Element.ALIGN_RIGHT);
    
    //Add the three cells to the table
    myTable.addCell(cellOne);
    myTable.addCell(cellTwo);
    myTable.addCell(cellThree);
    
    //Do something to add the table to your root document
    

    这将创建一个如下所示的表(假设您更正了我的输入错误)

    Hello World
    ------------------------------------
    Bottom Left      |      Bottom Right