【问题标题】:iText Rectangle with Black Font带有黑色字体的 iText 矩形
【发布时间】:2019-02-16 13:23:34
【问题描述】:

我正在尝试在 iText 中创建一个具有背景颜色和文本的矩形。

如果我按原样运行代码,我会得到文本但没有背景颜色。调用 canvas.fillStroke() 填充背景颜色,但不显示任何文本。

如何同时获得背景颜色和字体?

public void createPdf() {

    try(ByteArrayOutputStream os = new ByteArrayOutputStream()) {

        try(PdfWriter writer = new PdfWriter(os)) {
            try(PdfDocument pdf = new PdfDocument(writer)) {
                try (Document document = new Document(pdf)) {
                    PdfPage page = pdf.addNewPage();
                    PageSize ps = pdf.getDefaultPageSize();


                    Text green = new Text("This text is green. ")
                            .setFontColor(new DeviceRgb(27,255,0));

                    Paragraph p = new Paragraph("This is the text added in the rectangle.");
                    p.add(green);

                    PdfCanvas canvas = new PdfCanvas(pdf.getFirstPage());
                    Color orange = new DeviceRgb(255, 100, 20);
                    canvas.setFillColor(orange);

                    Rectangle rect = new Rectangle(1f,ps.getHeight()-101f,ps.getWidth()-1f,100f );

                    new Canvas(canvas, pdf, rect)
                            .add(p);
                    canvas.rectangle(rect);
                   // canvas.fillStroke();

                }
            }
        }
        Files.write(new File("C:\\users\\tim\\file.pdf").toPath(), os.toByteArray(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);

    }  catch(IOException e) {
        throw new RuntimeException(e);
    }
}

【问题讨论】:

  • 您是否尝试过先用颜色填充矩形,然后绘制文本(在彩色背景上),而不是先绘制文本然后填充矩形?

标签: java itext


【解决方案1】:

感谢 mkl 的评论

我所要做的就是先填充矩形,然后在后面添加段落

   public void createPdf() {

    try(ByteArrayOutputStream os = new ByteArrayOutputStream()) {

        try(PdfWriter writer = new PdfWriter(os)) {
            try(PdfDocument pdf = new PdfDocument(writer)) {
                try (Document document = new Document(pdf)) {
                    PdfPage page = pdf.addNewPage();
                    PageSize ps = pdf.getDefaultPageSize();


                    Text green = new Text("This text is green. ")
                            .setFontColor(new DeviceRgb(27,255,0));

                    Paragraph p = new Paragraph("This is the text added in the rectangle.");
                    p.add(green);

                    PdfCanvas canvas = new PdfCanvas(pdf.getFirstPage());
                    Color orange = new DeviceRgb(255, 100, 20);
                    canvas.setFillColor(orange);

                    Rectangle rect = new Rectangle(1f,ps.getHeight()-101f,ps.getWidth()-1f,100f );

                    Canvas rectangleCanvas = new Canvas(canvas, pdf, rect);
                    canvas.rectangle(rect);
                    canvas.fillStroke();
                    rectangleCanvas.add(p);
                }
            }
        }
        Files.write(new File("C:\\users\\tim\\file.pdf").toPath(), os.toByteArray(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);

    }  catch(IOException e) {
        throw new RuntimeException(e);
    }
}

【讨论】:

    猜你喜欢
    • 2015-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多