如何在图片中添加文字? [英] How to add text to an image?

查看:37
本文介绍了如何在图片中添加文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我使用 iText 生成 PDF 文档.

In my project I use iText to generate a PDF document.

假设页面的高度为 500pt(1 个用户单位 = 1 磅),并且我在页面上写入一些文本,然后是图像.

Suppose that the height of a page measures 500pt (1 user unit = 1 point), and that I write some text to the page, followed by an image.

如果内容和图片要求小于 450pt,则文本在图片之前.如果内容和图片超过450pt,则将文本转发到下一页.

If the content and the image require less than 450pt, the text preceded the image. If the content and the image exceed 450pt, the text is forwarded to the next page.

我的问题是:如何在写入图像之前获取剩余可用空间?

My question is: how can I obtain the remaining available space before writing an image?

推荐答案

第一件事:向页面添加文本和图像时,iText 有时会更改文本内容和图像的顺序.您可以使用以下方法避免这种情况:

First things first: when adding text and images to a page, iText sometimes changes the order of the textual content and the image. You can avoid this by using:

writer.setStrictImageSequence(true);

如果您想知道光标"的当前位置,可以使用方法getVerticalPosition().不幸的是,这个方法不是很优雅:它需要一个布尔参数来添加一个换行符(如果 true)或给你当前行的位置(如果 false).

If you want to know the current position of the "cursor", you can use the method getVerticalPosition(). Unfortunately, this method isn't very elegant: it requires a Boolean parameter that will add a newline (if true) or give you the position at the current line (if false).

我不明白您为什么要获得垂直位置.是不是因为你想要一个标题后跟一个图像,并且你希望标题和图像在同一页面上?

I do not understand why you want to get the vertical position. Is it because you want to have a caption followed by an image, and you want the caption and the image to be at the same page?

在这种情况下,您可以将文本和图像放在表格单元格中,并指示 iText 不要拆分行.在这种情况下,如果内容不适合当前页面,iText 将按正确顺序将文本和图像转发到下一页.

In that case, you could put your text and images inside a table cell and instruct iText not to split rows. In this case, iText will forward both text and image, in the correct order to the next page if the content doesn't fit the current page.

更新:

根据评论中添加的额外信息,现在很明显 OP 想要添加带有水印的图像.

Based on the extra information added in the comments, it is now clear that the OP wants to add images that are watermarked.

有两种方法可以实现这一点,具体取决于实际需求.

There are two approaches to achieve this, depending on the actual requirement.

方法 1:

第一种方法在 WatermarkedImages1 示例中进行了说明.在这个例子中,我们创建了一个 PdfTemplate,我们将一个图像以及一些写在该图像之上的文本添加到其中.然后我们可以将这个 PdfTemplate 包装在一个图像中,并使用单个 document.add() 语句将该图像与其水印一起添加.

The first approach is explained in the WatermarkedImages1 example. In this example, we create a PdfTemplate to which we add an image as well as some text written on top of that image. We can then wrap this PdfTemplate inside an image and add that image together with its watermark using a single document.add() statement.

这是执行所有魔术的方法:

This is the method that performs all the magic:

public Image getWatermarkedImage(PdfContentByte cb, Image img, String watermark) throws DocumentException {
    float width = img.getScaledWidth();
    float height = img.getScaledHeight();
    PdfTemplate template = cb.createTemplate(width, height);
    template.addImage(img, width, 0, 0, height, 0, 0);
    ColumnText.showTextAligned(template, Element.ALIGN_CENTER,
            new Phrase(watermark, FONT), width / 2, height / 2, 30);
    return Image.getInstance(template);
}

这是我们添加图像的方式:

This is how we add the images:

PdfContentByte cb = writer.getDirectContentUnder();
document.add(getWatermarkedImage(cb, Image.getInstance(IMAGE1), "Bruno"));
document.add(getWatermarkedImage(cb, Image.getInstance(IMAGE2), "Dog"));
document.add(getWatermarkedImage(cb, Image.getInstance(IMAGE3), "Fox"));
Image img = Image.getInstance(IMAGE4);
img.scaleToFit(400, 700);
document.add(getWatermarkedImage(cb, img, "Bruno and Ingeborg"));

如您所见,我们有一张非常大的图片(我和妻子的照片).我们需要缩放此图像以使其适合页面.如果您想避免这种情况,请查看第二种方法.

As you can see, we have one very large image (a picture of my wife and me). We need to scale this image so that it fits the page. If you want to avoid this, take a look at the second approach.

方法 2:

第二种方法在 WatermarkedImages2 示例中进行了说明.在这种情况下,我们将每个图像添加到 PdfPCell.此 PdfPCell 将缩放图像,使其适合页面的宽度.要添加水印,我们使用单元格事件:

The second approach is explained in the WatermarkedImages2 example. In this case, we add each image to a PdfPCell. This PdfPCell will scale the image so that it fits the width of the page. To add the watermark, we use a cell event:

class WatermarkedCell implements PdfPCellEvent {
    String watermark;

    public WatermarkedCell(String watermark) {
        this.watermark = watermark;
    }

    public void cellLayout(PdfPCell cell, Rectangle position,
        PdfContentByte[] canvases) {
        PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
        ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase(watermark, FONT),
            (position.getLeft() + position.getRight()) / 2,
            (position.getBottom() + position.getTop()) / 2, 30);
    }
}

这个单元格事件可以这样使用:

This cell event can be used like this:

PdfPCell cell;
cell = new PdfPCell(Image.getInstance(IMAGE1), true);
cell.setCellEvent(new WatermarkedCell("Bruno"));
table.addCell(cell);
cell = new PdfPCell(Image.getInstance(IMAGE2), true);
cell.setCellEvent(new WatermarkedCell("Dog"));
table.addCell(cell);
cell = new PdfPCell(Image.getInstance(IMAGE3), true);
cell.setCellEvent(new WatermarkedCell("Fox"));
table.addCell(cell);
cell = new PdfPCell(Image.getInstance(IMAGE4), true);
cell.setCellEvent(new WatermarkedCell("Bruno and Ingeborg"));
table.addCell(cell);

如果所有图像的大小或多或少相同,并且您不想担心图像在页面上的适配问题,您将使用这种方法.

You will use this approach if all images have more or less the same size, and if you don't want to worry about fitting the images on the page.

考虑:

显然,由于所做出的设计选择,两种方法都有不同的结果.请比较生成的 PDF 以查看差异:watermark_template.pdfwatermark_table.pdf

Obviously, both approaches have a different result because of the design choice that is made. Please compare the resulting PDFs to see the difference: watermark_template.pdf versus watermark_table.pdf

这篇关于如何在图片中添加文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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