-1

我正在使用 iText 并创建一个动态表,该表在方法中有一个重复出现的标题createTabularHeader

PdfPTable table = new PdfPTable(6);
// fill it with some basic information
table.setHeaderRows(1);

然而,在第一页我想显示不同的信息。(但表结构/大小保持不变)

由于以不同方法获得的动态内容,我不能说新页面何时开始。

我尝试了最原始的变体——只是在文本上添加一个白色矩形并插入不同的文本。因为它只是在第一页上,所以我要做的就是在两种方法之间创建那个矩形。但是白色矩形没有任何不透明度,不能覆盖任何东西。

然而,通过尝试,我找到了 writer.getDirectContent().setColorStroke(BaseColor.WHITE);将文本设置为白色的方法。后来我只是BaseColor手动将我的单元格设置为黑色。但是,即使在调用 my createTabularHeader-method 之后应用了新文本,它的图层也在原始文本的图层之下,并且字母部分覆盖了新文本。

使用如何在 PDF 中插入不可见文本的答案?使我想到使用的想法myPdfContentByte.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE);并没有太大帮助,因为无论我做什么,它都只会在第二页重置,并且第一页上的常规文本保持不可见。

我找不到合适的解决方案...如何仅在第一页修改表头?

4

1 回答 1

0

该解决方案不是很好,但可以工作......作为某种奖励,我想添加如何修改第一页上的缩进。

public void createPdf() {
    document = new Document();
    try {
        PdfWriter writer = PDFHead.getWriter(document);
        //If it's a letter we have a different indention on the top
        if (letterPDF) {
            document.setMargins(36, 36, 100, 36);
        } else {
            document.setMargins(36, 36, 36, 36);
        }
        document.open();
        document.add(createTabularContent());
        document.close();

    } catch (DocumentException | FileNotFoundException ex) {
        try {
            document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream(FILENAME));
            document.open();
            document.add(new Phrase(ex.getLocalizedMessage()));
            document.close();
            Logger.getLogger(Etikette.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException | DocumentException ex1) {
            Logger.getLogger(Etikette.class.getName()).log(Level.SEVERE, null, ex1);
        }
    }
}

PDFHead 用于创建常规标题(出现在每一页上的标题,而不仅仅是在带有表格的页面上):

public static PdfWriter getWriter(Document document) throws FileNotFoundException, DocumentException {
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
    HeaderFooter event = new HeaderFooter("Ing. Mario J. Schwaiger", type + " " + DDMMYYYY.format(new java.util.Date()), 835, isLetterPDF(), customerNumber);
    writer.setBoxSize("art", new Rectangle(36, 54, 559, 788));
    writer.setPageEvent(event);
    return writer;
}

在那个 HeaderFooter-Event 中,我使用了在基本上创建 PDF 之后调用该函数的事实(例如,对于页码):

@Override
    public void onEndPage(PdfWriter writer, Document document) {
        if (isLetter) {
            //That's only for the first page, apparently 1 is too late
            //I'm open for improvements but that works fine for me
            if (writer.getPageNumber() == 0) {

            //If it's a letter we use the different margins
            document.setMargins(36, 36, 100, 36);
            }
            if (writer.getPageNumber() == 1) {
            PdfContentByte canvas = writer.getDirectContent();

            float llx = 460;
            float lly = 742;

            float urx = 36;
            float ury = 607;

            //As I add the rectangle in the event here it's 
            //drawn over the table-header. Seems the tableheader
            //is rendered afterwards
            Rectangle rect1 = new Rectangle(llx, lly, urx, ury);
            rect1.setBackgroundColor(BaseColor.WHITE);
            rect1.setBorder(Rectangle.NO_BORDER);
            rect1.setBorderWidth(1);
            canvas.rectangle(rect1);

            ColumnText ct = new ColumnText(canvas);
            ct.setSimpleColumn(rect1);
            PdfPTable minitable = new PdfPTable(1);
            PdfPCell cell = PDFKopf.getKundenCol(PDFHeader.getCustomer(customerNumber));
            cell.setBorder(Rectangle.NO_BORDER);
            minitable.addCell(cell);

            //A single cell is not accepted as an "Element"
            //But a table including only a single cell is
            ct.addElement(minitable);
            try {
                ct.go();
            } catch (DocumentException ex) {
                Logger.getLogger(HeaderFooter.class.getName()).log(Level.SEVERE, null, ex);
            }

        //In any other case we reset the margins back to normal
        //This could be solved in a more intelligent way, feel free
        } else {
            document.setMargins(36, 36, 36, 36);
        }
    }

    //The regular header of any page...
    PdfPTable table = new PdfPTable(4);

    try {
        table.setWidths(new int[]{16, 16, 16, 2});
        table.setWidthPercentage(100);

        table.setTotalWidth(527);
        table.setLockedWidth(true);

        table.getDefaultCell().setFixedHeight(20);
        table.getDefaultCell().setBorder(Rectangle.BOTTOM);
        table.addCell(header);
        PdfPCell cell;

        cell = new PdfPCell(new Phrase(mittelteil));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBorder(Rectangle.BOTTOM);
        table.addCell(cell);

        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(String.format("Page %d of ", writer.getPageNumber()));
        cell = new PdfPCell(Image.getInstance(total));
        cell.setBorder(Rectangle.BOTTOM);
        table.addCell(cell);
        table.writeSelectedRows(0, -1, 34, y, writer.getDirectContent());
    } catch (DocumentException de) {
        throw new ExceptionConverter(de);
    }
}
于 2016-11-26T12:54:11.990 回答