iText横向定位和定位? [英] iText landscape orientation and positioning?

查看:475
本文介绍了iText横向定位和定位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用iText(5.4.2,最新版本),有两件事我还没有成功。

I've just started to work with iText (5.4.2, latest version) and there are two things that I haven't yet managed to get straight.


  • 以横向创建文档。所有页面都呈现为纵向。

  • 在给定位置插入图像(从顶部和左侧开始的毫米数)。

我有以下代码。

Document d = new Document(PageSize.A4_LANDSCAPE,0,0,0,0);
PdfWriter writer = PdfWriter.getInstance(d, new FileOutputStream("C:/test.pdf"));
document.open();
document.newPage();
Image img = Image.getInstance(String.format("C:/file.png"));
img.scaleToFit(400,240);
document.left(100f);
document.top(150f);
document.add(img);
document.close();

但是页面以纵向(非横向)渲染,图像放在左上角(根据要求,不是10和15个单位)。我做错了什么?

But the page is rendered in Portrait (not Landscape) and the image is put in the top left corner (not 10 and 15 units away from it as requested). What am I doing wrong?

推荐答案

你正在使用 PageSize.A4_LANDSCAPE ,一个由贡献者引入的变量,应该从未添加到主要版本中。请改用 PageSize.A4.rotate()

You're using PageSize.A4_LANDSCAPE, a variable that was introduced by a contributor and that should have never been added to the main release. Please use PageSize.A4.rotate() instead.

目前还不清楚你想用线条达到什么目的:

It's not clear what you want to achieve with the lines:

document.left(100f);
document.top(150f);

这些是吸气剂,而非吸尘剂。看起来好像你假设PDF与HTML类似。这个假设是错误的。

Those are getters, not setters. It looks as if you're assuming that PDF is similar to HTML. That assumption is wrong.

如果你想让图像从左边放10个用户单元,从顶部放15个用户单元(在这种情况下100和150是错误的值),您可以替换Document构造函数中的0值来定义10个用户单位的左边距和15个用户单位的上边距。

If you want the image to be put 10 user units from the left and 15 user units from the top (in which case 100 and 150 are the wrong values), you could replace the 0 values in your Document constructor to define a left margin of 10 user units and the top margin 15 user units.

另一种方式是使用方法 setAbsolutePosition()定义图像的绝对位置。在这种情况下,您需要注意坐标系的定向方式使得页面的左下角对于从头开始创建的文档具有坐标x = 0,y = 0。

Another way would be to define an absolute position for the image with the method setAbsolutePosition(). In that case, you need to be aware that the coordinate system is oriented in such a way that the lower-left corner of the page has the coordinate x=0 , y=0 for documents created from scratch.

这篇关于iText横向定位和定位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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