如何使用iText旋转PDF格式的水印(文本)? [英] How to rotate watermark (text) in PDF using iText?

查看:213
本文介绍了如何使用iText旋转PDF格式的水印(文本)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iText在PDF文件上标记水印(文字:SuperEasy You Done),如



正如你所看到的那样,水印是居中和水平的。



我想保持居中位于页面中间,但将其旋转为45度,因此它会逆时针旋转。这样的事情:





这是用于在给定字节数组上标记水印的代码(仅适用于我的pdf文档)

  / ** 
*返回带有水印标记的同一文档。
* @param documentBytes将使用水印
* @return byte []返回的pdf的字节数组,其中提供了相同的字节数组,但现在标记了水印。
* @throws IOException如果在添加水印时发生任何IO异常
* @throws DocumentException如果在添加水印时发生任何DocumentException异常
* /
private byte [] getDocumentWithWaterMark( byte [] documentBytes)抛出IOException,DocumentException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// pdf
PdfReader reader = new PdfReader(documentBytes);
int n = reader.getNumberOfPages();
PdfStamper压模= new PdfStamper(reader,outputStream);
//文本水印
Font font = new Font(Font.HELVETICA,60);
短语短语=新短语(SuperEasy You Done,font);
//透明度
PdfGState gs1 = new PdfGState();
gs1.setFillOpacity(0.06f);
//属性
PdfContentByte结束;
矩形页面大小;
浮动x,y;
//遍历每一页(如果多于一页)
for(int i = 1; i< = n; i ++){
pagesize = reader.getPageSizeWithRotation(i) ;
x =(pagesize.getLeft()+ pagesize.getRight())/ 2;
y =(pagesize.getTop()+ pagesize.getBottom())/ 2;
over = stamper.getOverContent(i);
over.saveState();
over.setGState(gs1);
//添加文本
ColumnText.showTextAligned(over,Element.ALIGN_CENTER,phrase,x,y,0);
over.restoreState();
}
stamper.close();
reader.close();
返回outputStream.toByteArray();
}

PS:我看过这个,但没有帮助:




解决方案

您只需要将所需的旋转角度指定为这一行

  ColumnText.showTextAligned(over,Element.ALIGN_CENTER,phrase,x,y,0); //在这种情况下旋转0等级

如果指定值为正(> 0),则旋转为逆时针旋转,否则(< 0)旋转是顺时针旋转。



在这种特殊情况下,为了将水印逆时针旋转45度,您只需要写上一行喜欢这样

  ColumnText.showTextAligned(over,Element.ALIGN_CENTER,phrase,x,y,45f ); // 45f表示将水印旋转45度逆时针旋转

通过应用同样的原则,我们可以实现任何旋转任何方向。






整个文档在这里: https://developers.itextpdf.com/apis nofollow noreferrer>第5版和第7版


I'm using iText for stamping a watermark (text: "SuperEasy You Done") on PDF files as described in How to watermark PDFs using text or images? (TransparentWatermark2.java). See project source code on GitHub.

Now an example of the PDF I'm getting is this one (the rest of the document is omitted):

As you can see the watermark is centered and horizontal.

I'd like to keep it centered in the middle of the page, but rotate it "45" degrees, so it rotates anticlockwise. Something like this:

This is the code for stamping the watermark on a given byte array (pdf documents only for me right now)

/**
 * Returns the same document with the watermark stamped on it.
 * @param documentBytes Byte array of the pdf which is going to be returned with the watermark
 * @return byte[] with the same byte array provided but now with the watermark stamped on it.
 * @throws IOException If any IO exception occurs while adding the watermark
 * @throws DocumentException If any DocumentException exception occurs while adding the watermark
 */
private byte[] getDocumentWithWaterMark(byte[] documentBytes) throws IOException, DocumentException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    // pdf
    PdfReader reader = new PdfReader(documentBytes);
    int n = reader.getNumberOfPages();
    PdfStamper stamper = new PdfStamper(reader, outputStream);
    // text watermark
    Font font = new Font(Font.HELVETICA, 60);
    Phrase phrase = new Phrase("SuperEasy You Done", font);
    // transparency
    PdfGState gs1 = new PdfGState();
    gs1.setFillOpacity(0.06f);
    // properties
    PdfContentByte over;
    Rectangle pagesize;
    float x, y;
    // loop over every page (in case more than one page)
    for (int i = 1; i <= n; i++) {
        pagesize = reader.getPageSizeWithRotation(i);
        x = (pagesize.getLeft() + pagesize.getRight()) / 2;
        y = (pagesize.getTop() + pagesize.getBottom()) / 2;
        over = stamper.getOverContent(i);
        over.saveState();
        over.setGState(gs1);
        // add text
        ColumnText.showTextAligned(over, Element.ALIGN_CENTER, phrase, x, y, 0);
        over.restoreState();
    }
    stamper.close();
    reader.close();
    return outputStream.toByteArray();
}

PS: I read this, but it didn't help:

解决方案

You just need to specify the desired rotation angle as the 6th parameter in this line:

ColumnText.showTextAligned(over, Element.ALIGN_CENTER, phrase, x, y, 0); // rotate 0 grades in this case

If the specified value is positive ( > 0) the rotation is anticlockwise, otherwise (< 0) the rotation is clockwise.

In this particular case, for rotating the watermark 45 degrees anticlockwise you just need to write the previous line like this:

ColumnText.showTextAligned(over, Element.ALIGN_CENTER, phrase, x, y, 45f); // 45f means rotate the watermark 45 degrees anticlockwise

By applying this same principle we can achieve any rotation in any direction.


The whole documentation is here: https://developers.itextpdf.com/apis under the links for version 5 and version 7.

这篇关于如何使用iText旋转PDF格式的水印(文本)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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