gpt4 book ai didi

java - 在 iText 元素上添加阴影效果

转载 作者:行者123 更新时间:2023-11-30 08:45:27 25 4
gpt4 key购买 nike

我在使用 Java 中的 iText 时遇到一些问题。我需要像这个例子一样生成一个带有阴影的单元格或矩形:

enter image description here

460 在某种带有阴影的单元格或矩形中。我不知道该怎么做。有什么帮助吗?

最佳答案

最简单的方法可能是使用带有通用标签和 PdfPageEventChunk。这样,当 Chunk 位于页面上时,您将获得事件回调。回调将为您提供 Chunk 的坐标(矩形),允许您在正确的位置绘制边框和阴影。

绘制黑色边框和灰色阴影的此类事件处理程序的示例:

class ShadowEvent extends PdfPageEventHelper {
@Override
public void onGenericTag(PdfWriter writer, Document document,
Rectangle rect, String text) {
PdfContentByte canvas = writer.getDirectContent();
// Paddings for the border
int paddingHorizontal = 20;
int paddingVertical = 5;
// Width of the shadow
int shadowwidth = 5;
// Calculate border location and size
float left = rect.getLeft() - paddingHorizontal;
float bottom = rect.getBottom() - paddingVertical;
float width = rect.getWidth() + 2*paddingHorizontal;
float height = rect.getHeight() + 2*paddingVertical;
canvas.saveState();
canvas.setColorFill(BaseColor.GRAY);
// Draw the shadow at the bottom
canvas.rectangle(left + shadowwidth, bottom - shadowwidth, width, shadowwidth);
canvas.fill();
// Draw the shadow at the right
canvas.rectangle(left + width, bottom - shadowwidth, shadowwidth, height);
canvas.fill();
canvas.setColorStroke(BaseColor.BLACK);
// Draw the border
canvas.rectangle(left, bottom, width, height);
canvas.stroke();
canvas.restoreState();
}
}

这显示了如何使用通用标签:

Document doc = new Document();
PdfWriter pdfWriter = PdfWriter.getInstance(doc, outfile);
pdfWriter.setPageEvent(new ShadowEvent());
doc.open();
Chunk c = new Chunk("60");
c.setGenericTag("shadow");
doc.add(c);
doc.close();

(请注意,onGenericTag 方法的 text 参数将包含设置为 ChunkString > 使用 setGenericTag。这是上面示例中的 “shadow”。它允许区分不同的标签。因为我们在这里只使用 1 个标签,所以我没有使用 text 参数。)

示例的结果如下所示:

Border with shadow

关于java - 在 iText 元素上添加阴影效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33290487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com