在 iText 中页面大小不同的 pdf 文件中插入水印时出现问题

发布于 2024-10-05 23:59:14 字数 168 浏览 6 评论 0原文

当我尝试在绝对位置添加图像作为水印时,绝对 X=10f 且绝对 y =矩形.getLeft()+ (rectangle.getWidth()-image.getPlainWidth());

对于某些页面大小,在绝对 X=10f 时不会添加图像,必须将其增加到 300f 或更高。如何正确获取左侧的绝对位置。

When I am trying to add a image as watermark at absolute position while absoluteX=10f and absolute y = rectangle.getLeft()+ (rectangle.getWidth()-image.getPlainWidth());

For some page-sizes, the image does not get added at absoluteX=10f have to increase it to 300f or more. How to correctly fetch the absolute position for the left.

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

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

发布评论

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

评论(1

假面具 2024-10-12 23:59:14

您需要检查页面的裁剪框并相对于该框放置文本。另请注意,左下角不必为 0,0,并且页面可以旋转

我见过以三种方式生成横向页面:

11"x8.5"

8.5x11 @ 90 度
8.5x11 @ 270 度

(幸运的是,我从未见过 11x8.5 @ 180 度,否则我可能不得不在一般原则上严重伤害某人)

“肖像”页面几乎普遍生成为 8.5“x11”(或任何您的测量值)是)。在我的>使用 PDF 十年了,我还没有看到旋转的纵向页面。合法,但愚蠢。

因此,为了获得正确的框并将文本旋转到位,您需要类似的东西:

final double TOP_OFFSET = 10.0 + IMAGE_HEIGHT; // or whatever
final double RIGHT_OFFSET = 15.0 + IMAGE_WIDTH;

PdfReader reader = new PdfReader( path );
PdfStamper stamper = new PdfStamper( reader, outStream );

Rectangle cropBox = reader.getCropBox( 1 );
int rotation = reader.getPageRotation( 1 );
PdfContentByte content = stamper.getOverContent( 1 ); // first page

AffineTransform transform;
double xOffset, yOffset;
switch( rotation ) {
  case 0:
    xOffset = cropBox.getRight() - RIGHT_OFFSET;
    yOffset = cropBox.getTop() - TOP_OFFSET;
    transform = AffineTransform.getTranslateInstance( xOffset, yOffset );
    break;
  case 90:
    // some other transformations here
    break;
  case 180:
    // and here
    break;
  case 270:
    // and here.
    break;
};

content.transform( transform );
content.addImage( waterMarkImage );

stamper.close();

我经常发现我必须将页面大小放大相当多才能找出我的转换出了问题的地方。对媒体框上的每个边框说+1000(如果存在则进行裁剪)。

PdfDictionary pageDict = reader.getPageN(1);
PdfArray boxes[] = {pageDict.getAsArray( PdfName.MEDIABOX ), pageDict.getAsArray( PdfName.CROPBOX ) };
float mods[] = {-1000, -1000, 1000, 1000 }
for (int i = 0; i < boxes.length; ++i) {
  if (boxes[i] == null)
    continue; // crop boxes are optional
  for (int j = 0; j < 4; ++j) {
    PdfNumber curVal = boxes[i].getAsNumber(j);
    PdfNumber newVal = curVal.getFloat() + mods[j];
    boxes[i].set( j, newVal );
  }
}

现在,当你搞砸了你的转变时(我不可避免地会这样做),你将能够看到你是如何搞砸的,并修复它。

You need to check the page's crop box and position your text relative to that. Note also that the bottom left corner need not be 0,0, and that pages can be rotated.

I've seen landscape pages generated three ways:

11"x8.5"

8.5x11 @ 90 degrees
8.5x11 @ 270 degrees

(fortunately I've never seen 11x8.5 @ 180, or I might have to Severely Harm someone on general principles)

"Portrait" pages are nigh-universally generated as 8.5"x11" (or whatever your measurements might be). In my > a decade working with PDF, I have yet to see a rotated portrait page. Legal, but stupid.

So, to get your correct bbox and rotate your text into position, you'd need something like:

final double TOP_OFFSET = 10.0 + IMAGE_HEIGHT; // or whatever
final double RIGHT_OFFSET = 15.0 + IMAGE_WIDTH;

PdfReader reader = new PdfReader( path );
PdfStamper stamper = new PdfStamper( reader, outStream );

Rectangle cropBox = reader.getCropBox( 1 );
int rotation = reader.getPageRotation( 1 );
PdfContentByte content = stamper.getOverContent( 1 ); // first page

AffineTransform transform;
double xOffset, yOffset;
switch( rotation ) {
  case 0:
    xOffset = cropBox.getRight() - RIGHT_OFFSET;
    yOffset = cropBox.getTop() - TOP_OFFSET;
    transform = AffineTransform.getTranslateInstance( xOffset, yOffset );
    break;
  case 90:
    // some other transformations here
    break;
  case 180:
    // and here
    break;
  case 270:
    // and here.
    break;
};

content.transform( transform );
content.addImage( waterMarkImage );

stamper.close();

I often find I have to blow the page size up by Quite A Bit in order to find out where I'm going wrong with my transformations. Say +1000 to each border on the media box (and crop if present).

PdfDictionary pageDict = reader.getPageN(1);
PdfArray boxes[] = {pageDict.getAsArray( PdfName.MEDIABOX ), pageDict.getAsArray( PdfName.CROPBOX ) };
float mods[] = {-1000, -1000, 1000, 1000 }
for (int i = 0; i < boxes.length; ++i) {
  if (boxes[i] == null)
    continue; // crop boxes are optional
  for (int j = 0; j < 4; ++j) {
    PdfNumber curVal = boxes[i].getAsNumber(j);
    PdfNumber newVal = curVal.getFloat() + mods[j];
    boxes[i].set( j, newVal );
  }
}

Now, when you screw up your transformations (I inevitably do), you'll be able to see HOW you screwed up, and fix it.

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