不知道如何创建居中的“图像”文本”使用 iText (Java) 在 pdf 文件中添加水印

发布于 2024-12-08 11:34:31 字数 1019 浏览 0 评论 0原文

我正在使用 iText 库,并且尝试在页面底部添加水印。水印很简单,它必须居中,左侧有图像,右侧有文本。

此时,我已经有了 png 格式的图像和文本。我可以计算要放置图像的位置(居中),计算页面大小和图像大小,但现在我想将文本包含为文本(更好的易读性等)。

我可以将图像和文本嵌入到某个组件中,然后像我现在所做的那样计算位置吗?其他解决方案或想法?

这是我的实际代码:

try {
        PdfReader reader = new PdfReader("example.pdf");
        int numPages = reader.getNumberOfPages();
        PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("pdfWithWatermark.pdf"));

        int i = 0;
        Image watermark = Image.getInstance("watermark.png");
        PdfContentByte addMark;

        while (i < numPages) {
            i++;
            float x = reader.getPageSizeWithRotation(i).getWidth() - watermark.getWidth();
            watermark.setAbsolutePosition(x/2, 15);
            addMark = stamp.getUnderContent(i);
            addMark.addImage(watermark);
        }
        stamp.close();

    }
    catch (Exception i1) {
        logger.info("Exception adding watermark.");
        i1.printStackTrace();
    }

提前谢谢您!

I'm using the iText library, and I'm trying to add a watermark at the bottom of the page. The watermark is simple, it has to be centered an has an image on the left and a text on the right.

At this point, I have the image AND the text in a png format. I can calculate the position where I want to put the image (centered) calculating the page size and image size, but now I want to include the text AS text (better legibility, etc.).

Can I embed the image and the text in some component and then calculate the position like I'm doing now? Another solutions or ideas?

Here is my actual code:

try {
        PdfReader reader = new PdfReader("example.pdf");
        int numPages = reader.getNumberOfPages();
        PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("pdfWithWatermark.pdf"));

        int i = 0;
        Image watermark = Image.getInstance("watermark.png");
        PdfContentByte addMark;

        while (i < numPages) {
            i++;
            float x = reader.getPageSizeWithRotation(i).getWidth() - watermark.getWidth();
            watermark.setAbsolutePosition(x/2, 15);
            addMark = stamp.getUnderContent(i);
            addMark.addImage(watermark);
        }
        stamp.close();

    }
    catch (Exception i1) {
        logger.info("Exception adding watermark.");
        i1.printStackTrace();
    }

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日情怀 2024-12-15 11:34:31

你最好检查一下:

import com.lowagie.text.*;
import java.io.*;
import com.lowagie.text.pdf.*;
import java.util.*;

class  pdfWatermark
{
  public static void main(String args[])
  {  
    try 
    {
      PdfReader reader = new PdfReader("text.pdf");
      int n = reader.getNumberOfPages();

      // Create a stamper that will copy the document to a new file
      PdfStamper stamp = new PdfStamper(reader, 
        new FileOutputStream("text1.pdf"));
      int i = 1;
      PdfContentByte under;
      PdfContentByte over;

      Image img = Image.getInstance("watermark.jpg");
      BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, 
        BaseFont.WINANSI, BaseFont.EMBEDDED);

      img.setAbsolutePosition(200, 400);

      while (i < n) 
      {
        // Watermark under the existing page
        under = stamp.getUnderContent(i);
        under.addImage(img);

        // Text over the existing page
        over = stamp.getOverContent(i);
        over.beginText();
        over.setFontAndSize(bf, 18);
        over.showText("page " + i);
        over.endText();

        i++;
      }

      stamp.close();

    }
    catch (Exception de) 
    {}
  }
}

(来源)

you better check this:

import com.lowagie.text.*;
import java.io.*;
import com.lowagie.text.pdf.*;
import java.util.*;

class  pdfWatermark
{
  public static void main(String args[])
  {  
    try 
    {
      PdfReader reader = new PdfReader("text.pdf");
      int n = reader.getNumberOfPages();

      // Create a stamper that will copy the document to a new file
      PdfStamper stamp = new PdfStamper(reader, 
        new FileOutputStream("text1.pdf"));
      int i = 1;
      PdfContentByte under;
      PdfContentByte over;

      Image img = Image.getInstance("watermark.jpg");
      BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, 
        BaseFont.WINANSI, BaseFont.EMBEDDED);

      img.setAbsolutePosition(200, 400);

      while (i < n) 
      {
        // Watermark under the existing page
        under = stamp.getUnderContent(i);
        under.addImage(img);

        // Text over the existing page
        over = stamp.getOverContent(i);
        over.beginText();
        over.setFontAndSize(bf, 18);
        over.showText("page " + i);
        over.endText();

        i++;
      }

      stamp.close();

    }
    catch (Exception de) 
    {}
  }
}

(source)

清醇 2024-12-15 11:34:31

有点难看,但是,你不能将图像和文本添加到表格中然后居中吗?

is a bit ugly but, can't you add the image and the text to a table and then center it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文