iText矩形与黑色字体

问题描述 投票:0回答:1

我正在尝试在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个回答
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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.