如何在itextpdf中缩放内容,使其适合页面大小?
我必须使用itextpdf将一些html格式的报告转换成pdf格式。Mi客户端的打印机使用4x11英寸,因此报告必须具有此大小 问题在于,他希望4x11中通常需要一页半的报告可以放在4x11的一页中。就像我们在缩放图像一样 在关于如何使用pdfHtml创建单页内容的itext7教程之后,我以4xcontent高度的页面大小创建了报告 问题本身是,是否有任何方法来缩放内容,使其适合4x11英寸,而不考虑内容的高度 下面是一个将html代码转换为pdf的示例如何在itextpdf中缩放内容,使其适合页面大小?,pdf,itext7,pdfhtml,Pdf,Itext7,Pdfhtml,我必须使用itextpdf将一些html格式的报告转换成pdf格式。Mi客户端的打印机使用4x11英寸,因此报告必须具有此大小 问题在于,他希望4x11中通常需要一页半的报告可以放在4x11的一页中。就像我们在缩放图像一样 在关于如何使用pdfHtml创建单页内容的itext7教程之后,我以4xcontent高度的页面大小创建了报告 问题本身是,是否有任何方法来缩放内容,使其适合4x11英寸,而不考虑内容的高度 下面是一个将html代码转换为pdf的示例 #头衔 html,正文{ 填充:0;
#头衔
html,正文{
填充:0;
保证金:0;
}
@媒体印刷品{
@页面{
保证金:2倍;
}
}
身体{
字体系列:Verdana,无衬线;
字体大小:10px;
}
有限责任公司
公司名称
美国,123344
紧急联系人:12345667
信息
产品类型:
产品1
编号#:
1234556
卡车司机:
有限责任公司
门票:
123243355
接受日期/时间:
04/28/2000 22:07
分票#w/#:
确认:
1233223423
收据#:
做收据#:
信息
资料:
有限责任公司
姓名:
A公司2,3,4
其他名称:
公司
有些东西:
公司
抵达日期及;时间::
04/28/2000 22:39
有些东西:
加载时间:
00:08
法定说明:
等待时间:
00:00
纬度:
1212324343
某物日期&;时间:
04/28/2000 22:47
经度:
123322344
装载里程:
100
县、州:
公司
等待时间注意事项:
拒绝通知:
其他说明:
生产中的某物;
拿起
公司
接受
公司
公司类型
公司
理学学士及;W(%)
0.1
公司
公司
公司
90
公司
0
公司
90
公司
0.01
公司
90
公司
0英尺0英寸
在
(0.0英寸)
公司
53
公司
0英尺0英寸
在
(0.0英寸)
公司
49.9
公司
194
公司
公司
公司
190.53
公司
04/28/2000
22:39
公司
190.72
公司
121313
公司
0英尺0英寸
在
(0.0英寸)
公司
04/28/2000
22:47
拿起
公司
接受
拒绝理由
公司
公司
理学学士及;W(%)
0.1
公司
公司
公司
90
公司
0
公司
90
公司
0.01
公司
90
公司
0英尺0英寸
在
(0.0英寸)
公司
53
公司
0英尺0英寸
在
(0.0英寸)
公司
49.9
公司
194
公司
公司
公司
190.53
公司
04/28/2000
22:39
公司
190.72
公司
108712121215
公司
0英尺0英寸
在
(0.0英寸)
公司
04/28/2000
22:47
公司
公司:
公司:
公司:
抵达日期及;时间:
公司:
公司:
纬度:
等等,Ti
private void scaleFontSizeRecursively(IElement element, float scale) {
if (element.hasOwnProperty(Property.FONT_SIZE)) {
UnitValue fontSize = element.getOwnProperty(Property.FONT_SIZE);
fontSize.setValue(fontSize.getValue() * scale);
}
if (element instanceof com.itextpdf.layout.element.AbstractElement) {
for (Object child : ((AbstractElement) element).getChildren()) {
scaleFontSizeRecursively((IElement) child, scale);
}
}
}
PdfDocument pdf = new PdfDocument(pdfWriter);
pdf.setDefaultPageSize(new PageSize(new Rectangle(72 * 4, 72 * 11)));
// Important to set last boolean parameter to false so that added elements are not drawn immediately
Document document = new Document(pdf, pdf.getDefaultPageSize(), false);
List<IElement> elements = HtmlConverter.convertToElements(html);
float lFontSizeScale = 0.001f;
float rFontSize = 1;
// Initially adding all the elements into the document. They will be document's children from now on
for (IElement element : elements) {
if (element instanceof IBlockElement) {
document.add((IBlockElement) element);
} else {
throw new IllegalStateException("Unexpected situation that needs to be additionally handled in code");
}
}
for (int i = 0; i < 20; i++) {
float midFontSize = (lFontSizeScale + rFontSize) / 2;
for (IElement element : elements) {
scaleFontSizeRecursively(element, midFontSize);
}
// Relayouting all the elements with the new font size
document.relayout();
// See if one page was enough
if (document.getPdfDocument().getNumberOfPages() == 1) {
lFontSizeScale = midFontSize;
} else {
rFontSize = midFontSize;
}
// Roll back our font size scaling
for (IElement element : elements) {
scaleFontSizeRecursively(element, 1 / midFontSize);
}
// Remove extra created pages
while (pdf.getNumberOfPages() > 1) {
pdf.removePage(2);
}
}
// Finally setting the font scale that was the match for us and layouting again
for (IElement element : elements) {
scaleFontSizeRecursively(element, lFontSizeScale);
}
document.relayout();
System.out.println("Resultant scale: " + lFontSizeScale);
document.close();
Resultant scale: 0.6297823