itext setRotateContent标志用法不明确 [英] itext setRotateContent Flag usage not clear

查看:119
本文介绍了itext setRotateContent标志用法不明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pdfstamper 水印添加到现有的pdf中.当我保留标志 setRotateContent(true)时,水印位于正确的位置,但是当我将其保留为false时,水印放错了位置. 由于某些限制,我无法共享代码.

I am using pdfstamper to add the watermark to an existing pdf. When i keep the flag setRotateContent(true), the watermark comes at the right position but when i keep it false, watermark is misplaced. I cant share the code due to some restrictions.

我正在分享案件.

原始PDF

使用setRotateContent(false)

使用setRotateContent(true)

所以我的问题是 setRotateContent()是如何工作的.我也尝试过Api页面.但是所有示例都带有 setRotateContent(false).

So my question is how exactly does the setRotateContent() works. I have tried the Api page as well. But all the examples are with setRotateContent(false).

推荐答案

所以我的问题是setRotateContent()到底如何工作

So my question is how exactly does the setRotateContent() works

作为背景知识,您需要知道每个PDF页面都包含一个属性 Rotate ,该属性指定为"显示时页面应顺时针旋转的度数或值应该是90的倍数.默认值是0."

As a bit of background you need to know that each PDF page contains an attribute Rotate which is specified as "The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90. Default value: 0."

如果您要向页面中添加具有旋转值(即360的倍数)的值,则有两种不同的情况:

If you want to add something to a page which has a non-trivial Rotate value (i.e. a multiple of 360), therefore, there are two distinct situations:

  • 无论您最终要如何旋转页面,都想要在相对于页面坐标系的位置和方向添加一些东西,
  • 或者您要在相对于页面显示方式的位置添加一些内容.

虽然前者是微不足道的,但您只需使用给定的坐标和方向,后者就需要读取 Rotate 值并将其计算为您的坐标和角度.

While the former is trivial, you simply use the given coordinates and orientation, the latter requires you to read the Rotate value and calculate it into your coordinates and angles.

此处的iText试图为您提供帮助,对于setRotateContent(true),它首先为过度和不足提供了一种转换,使您可以简单地继续选择坐标和角度,就像不涉及页面旋转一样.

iText here tries to help you and, for setRotateContent(true), first adds a transformation to overcontent and undercontent, allowing you to simply go ahead choose coordinates and angles as if no page rotation was involved.

似乎后一种情况比前一种情况更常见.因此,默认的RotateContent值为true.因此,在前一种情况下,您实际上必须使用setRotateContent(false)将其关闭.

Seemingly the latter situation has been perceived to occur more often than the former. Thus, the default RotateContent value is true. In the former situation, therefore, you actually have to switch it off using setRotateContent(false).

问题在于它是如何工作的:这是初始化不足和过度内容ByteBuffer表示形式的方法:

As the question is how that works exactly: This is the method executed to initialize the undercontent and overcontent ByteBuffer representation:

void applyRotation(PdfDictionary pageN, ByteBuffer out) {
    if (!rotateContents)
        return;
    Rectangle page = reader.getPageSizeWithRotation(pageN);
    int rotation = page.getRotation();
    switch (rotation) {
        case 90:
            out.append(PdfContents.ROTATE90);
            out.append(page.getTop());
            out.append(' ').append('0').append(PdfContents.ROTATEFINAL);
            break;
        case 180:
            out.append(PdfContents.ROTATE180);
            out.append(page.getRight());
            out.append(' ');
            out.append(page.getTop());
            out.append(PdfContents.ROTATEFINAL);
            break;
        case 270:
            out.append(PdfContents.ROTATE270);
            out.append('0').append(' ');
            out.append(page.getRight());
            out.append(PdfContents.ROTATEFINAL);
            break;
    }
}

(PdfStamperImp)

使用

static final byte ROTATE90[] = DocWriter.getISOBytes("0 1 -1 0 ");
static final byte ROTATE180[] = DocWriter.getISOBytes("-1 0 0 -1 ");
static final byte ROTATE270[] = DocWriter.getISOBytes("0 -1 1 0 ");
static final byte ROTATEFINAL[] = DocWriter.getISOBytes(" cm\n");

(PdfContents)

PS :尽管RotateContent属性控制是否将这些转换添加到内容过多和内容不足的地方,但是对于注释,存在类似的机制,该机制不能被其禁用属性,参见. 此答案.

PS: While the RotateContent attribute controls whether or not these transformations are added to the overcontent and undercontent or not, there is a similar mechanism for annotations which cannot be disabled by that attribute, cf. this answer.

这篇关于itext setRotateContent标志用法不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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