简体   繁体   English

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

[英]Don't know how to create a centered “image + text” watermark in pdf files with iText (Java)

I'm using the iText library, and I'm trying to add a watermark at the bottom of the page. 我正在使用iText库,并且试图在页面底部添加水印。 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. 在这一点上,我有图像和png格式的文本。 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.). 我可以计算要放置图像的位置(居中),以计算页面尺寸和图像尺寸,但是现在我想包含文本AS文本(更好的可读性等)。

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! 先感谢您!

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 ) 来源

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM