如何使用虚线作为单元格边框? [英] How to use a dotted line as a cell border?

查看:202
本文介绍了如何使用虚线作为单元格边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用iText 2.1.0在表格的中间获得虚线边框(使用的单元格边框)。下面的代码即使在表格中间也会生成虚线边框。

I am trying to get dotted border (used cell border) in middle of the table by using iText 2.1.0. Below code would be generating dotted border even after middle of the table.

你可以帮助我单独为特定单元格添加活动吗?

import java.io.FileOutputStream;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPCellEvent;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class DottedLine {

    public static final String RESULT = "Dotted.pdf";

    public static void main(String[] args) throws Exception {
        new DottedLine().createPdf(RESULT);
    }

    public void createPdf(String filename) throws Exception {

        Document document = new Document(PageSize.A4, 50, 50, 50, 50);

        PdfWriter.getInstance(document, new FileOutputStream(filename));

        document.open();

        PdfPTable tab = new PdfPTable(1);
        for (int j = 1; j <= 10; j++) {
            if (j == 5) {
                PdfPCell c1 = new PdfPCell(new Phrase("Test" + j));
                c1.setBorder(Rectangle.TOP);
                c1.setCellEvent(new dot());
                tab.addCell(c1);
            } else {
                PdfPCell c2 = new PdfPCell(new Phrase("Test " + j));
                c2.setBorder(Rectangle.TOP);
                tab.addCell(c2);
            }
        }

        document.add(tab);
        document.close();
    }

    // Cell Event to make dotted Line
    class dot implements PdfPCellEvent {

        @Override
        public void cellLayout(PdfPCell arg0, Rectangle arg1, PdfContentByte[] arg2) {
            arg0.setBorder(Rectangle.TOP);
            PdfContentByte cb = arg2[PdfPTable.LINECANVAS];
            cb.setLineDash(3f, 3f);
            cb.restoreState();
        }
    }
}


推荐答案

您正在使用单元格事件,但您的代码非常差。您还引入了PDF语法错误,如果您只使用更新版本的iText,则会收到警告。 (关于过时的iText版本的警告是有原因的。人们不应该忽视它们!)

You're using a cell event, but your code is very poor. You're also introducing a PDF syntax error for which you would have received a warning if only you would use a more recent version of iText. (The warnings about obsolete iText versions are there for a reason. People shouldn't ignore them!!!)

这就是说,我做了一个例子来解决您的问题: DottedLineCell

This being said, I've made an example that solves your problem: DottedLineCell

结果PDF是一个包含两个表的文档: dotted_line_cell.pdf

The resulting PDF is a document with two tables: dotted_line_cell.pdf

对于第一个表,我们使用表事件:

For the first table, we use a table event:

class DottedCells implements PdfPTableEvent {
    @Override
    public void tableLayout(PdfPTable table, float[][] widths,
        float[] heights, int headerRows, int rowStart,
        PdfContentByte[] canvases) {
        PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
        canvas.setLineDash(3f, 3f);
        float llx = widths[0][0];
        float urx = widths[0][widths[0].length -1];
        for (int i = 0; i < heights.length; i++) {
            canvas.moveTo(llx, heights[i]);
            canvas.lineTo(urx, heights[i]);
        }
        for (int i = 0; i < widths.length; i++) {
            for (int j = 0; j < widths[i].length; j++) {
                canvas.moveTo(widths[i][j], heights[i]);
                canvas.lineTo(widths[i][j], heights[i+1]);
            }
        }
        canvas.stroke();
    }
}

这是绘制单元格边框的最优雅方式,因为它只对所有行使用一个 stroke()运算符。如果您有包含rowpans的表(但您可能不关心rowspans,因为您使用的是不支持rowspans的过时版本的iText),这不是一个选项。

This is the most elegant way to draw the cell borders, as it uses only one stroke() operator for all the lines. This isn't an option if you have tables with rowspans (but you probably don't care about rowspans, because you're using an obsolete version of iText that didn't support rowspans).

第二个表使用单元格事件:

The second table uses a cell event:

class DottedCell implements PdfPCellEvent {
    @Override
    public void cellLayout(PdfPCell cell, Rectangle position,
        PdfContentByte[] canvases) {
        PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
        canvas.setLineDash(3f, 3f);
        canvas.rectangle(position.getLeft(), position.getBottom(),
            position.getWidth(), position.getHeight());
        canvas.stroke();
    }
}

对于单元格事件,每个周围都会绘制一个边框细胞。这意味着你将有多个 stroke()运算符和重叠行。

With a cell event, a border is drawn around every cell. This means you'll have multiple stroke() operators and overlapping lines.

我可以再次恳求你升级到更新的版本?前iText 5版本有一个特殊的错误,导致表行在十亿行中消失一次。

May I plead once more that you upgrade to a more recent version? Pre-iText 5 versions have a peculiar error that leads to table rows that disappear once in a billion rows.

这篇关于如何使用虚线作为单元格边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆