横向页面的ItextPdf宽度不会扩展 [英] ItextPdf Width Does Not Expand for Landscape Page

查看:434
本文介绍了横向页面的ItextPdf宽度不会扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的'PdfPageEventHelper'中针对横向旋转的PDF页面放置一个表格:

I am trying to put a table on a PDF page that has been rotated for landscape in my 'PdfPageEventHelper':

public void onStartPage(PdfWriter writer_,Document document_) {
        writer_.addPageDictEntry(PdfName.ROTATE,PdfPage.LANDSCAPE);
    }

这部分似乎有效,但当我用10列创建我的表时,设置宽度似乎不起作用:

That part seems to be working but when I create my table with 10 columns, setting the width does not seem to work:

float sizeY=page.getWidth();
PdfPTable table=new PdfPTable(10);
table.setTotalWidth(sizeY);
float[] widths=new float[] {1.f,1.5f,4.f,1.f,1.f,1.f,1.f,1.f,1.f,1.f};
table.setWidths(widths);

表格宽度似乎总是默认值(纵向页面宽度的80%)。如何让表格使用横向页面宽度? TIA。

The table width always seems to be the default (80% of the portrait page width). How do I get the table to use the landscape page width? TIA.

推荐答案

您使用的是旧的iText 5. iText 5中有很多可以在iText 7中解决的怪癖。

You are using the old iText 5. There are plenty of quirks in iText 5 that are solved in iText 7.

在iText 7中,我们有 UnitValue 的概念,这有助于iText确定某些东西是否是绝对值(用户单位/点数)或相对值(百分比)。

In iText 7, we have the concept of the UnitValue which helps iText determine whether something is an absolute value (in user units / points) or a relative value (a percentage).

在iText 5中,这个概念是未知的。 PdfPTable 类有两个表格宽度参数:

In iText 5, this concept is unknown. The PdfPTable class has two parameters for the width of a table:


  • 宽度百分比:默认为80%,

  • 绝对宽度

默认情况下,使用宽度作为百分比。如果你想使用绝对宽度,你需要用 setLockedWidth()方法拉开关:

By default, the width as a percentage is used. If you want to use the absolute width, you need to "pull the switch" with the setLockedWidth() method:

table.setTotalWidth(sizeY);
table.setLockedWidth(true);

我知道这很奇怪,但话说再来一次,你正在使用 iText版本。如果您想要更一致的API,请使用iText 7.升级非常重要。我们花费了大量的时间和金钱来改进iText,所以看到新的iText开发人员选择使用iText 5而不是使用iText 7是令人沮丧的。

I know this is odd, but then again, you are using the old iText version. If you want a more consistent API, please use iText 7. That upgrade is really significant. We've spent an awful amount of time and money on improving iText, so it's kind of frustrating to see that new iText developers choose to work with iText 5 rather than with iText 7.

这篇关于横向页面的ItextPdf宽度不会扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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