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

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

问题描述

在我的项目中,我使用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.pdf watermark_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天全站免登陆