- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当表格列用其相对大小指定时,如何从iText获取列的绝对宽度?
我指定了 3 列,其相对宽度为 float ,如下所示:
PdfPCell cell2;
PdfPTable table2 = new PdfPTable(new float[]{(float) 4.5, (float) 0.5, 9});
table2.setWidthPercentage(100);
我尝试使用table2.getAbsoluteWidths()[2]
,但结果是浮点0.0
。
在 PDF 上动态计算表格宽度后,我想获取每列的绝对宽度(已最大化)。
------------------------ 编辑 ----------------------
实际上,我需要在添加文档之前确定列的宽度,然后替换我的硬编码 float 变量限制。
我有很多数据字符串,并且想在获取最大宽度页面时在另一列中设置并在函数restrictWidthOfFont中进行分割
String fullAlamatPP = "test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test ";
float restrictions = (float) 330.12103; // this is width of a column from table
String alamatSesuaiKtpPP[] = restrictWidthOfFont(fullAlamatPP, font_body, restrictions);
Phrase p_alamat1_ct = new Phrase(alamatSesuaiKtpPP[0], font_body);
p_alamat1_ct.add(dottedline);
cell2 = new PdfPCell(p_alamat1_ct);
cell2.setBorder(PdfPCell.NO_BORDER);
table2.addCell(cell2);
Phrase p_alamat2_ct = new Phrase(alamatSesuaiKtpPP[1], font_body);
p_alamat2_ct.add(dottedline);
cell2 = new PdfPCell(p_alamat2_ct);
cell2.setBorder(PdfPCell.NO_BORDER);
table2.addCell(cell2);
private static String[] restrictWidthOfFont(String character, Font font, float restrictions) {
String results[];
results = new String[] {"",""};
String concatChar = " ";
float widthOfString = 0;
for (int i = 0; i < character.length();i++) {
concatChar += character.charAt(i);
widthOfString = font.getCalculatedBaseFont(true).getWidthPoint(concatChar, font.getCalculatedSize());
// System.out.println("widthOfString " + widthOfString);
if (widthOfString > restrictions) {
results[0] = concatChar.substring(0, concatChar.length() - 1);
results[1] = character.substring(i, character.length());
break;
}
}
if (results[0] == "") {
results[0] = character;
}
return results;
}
在添加表格文档之前可以获取列的宽度吗?
最佳答案
函数table.getAbsoluteWidths()
依赖于必须设置表的 totalWidth 的要求,因此 table.getTotalWidth() > 0
.
这是因为 com.itextpdf.text.pdf.PdfPTable
里面类方法calculateWidths()
调用它然后计算所有列的绝对宽度:
this.absoluteWidths[k] = this.totalWidth * this.relativeWidths[k] / total;
为了确保在检索relativeWidths数组之前设置了totalWidth,您需要:
以下代码结合您的算法,根据跨单元格的限制分割文本(即长地址或alamat) >列的最大宽度:
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
public class TableWidthSample {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("TableColumnWidth.pdf"));
document.open();
PdfPTable table = createTableWithRelativeColumnWidths();
// needed for calculation of getAbsoluteWidths: (A) totalWidth is set for table
table.setTotalWidth(calculateTableWidthBasedOnPageA4(document, table));
// now it prints width, because (A)
printTableWidth(table, "after totalWidth set manually"); // absoluteWidths: [134.4857, 14.942857, 268.9714]
// long text, that needs to be split among cells (fullAlamatPP)
String text = "this is a very long text. The text contains a full address. Because the text is too long to be shown in the last cell, it is split.";
// your CODE to fill text into cells based on restrictions
addTextAcrossTableCells(table, text);
document.add(table);
/*
// need to add filled table to doc (B)
addToDocumentCellsAndTable(document, table);
// now it prints width, because (B)
printTableWidth(table, "after added to doc"); // absoluteWidths: [134.4857, 14.942857, 268.9714]
*/
document.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static PdfPTable createTableWithRelativeColumnWidths() {
float[] relativeWidths = {4.5F, 0.5F, 9F};
PdfPTable table = new PdfPTable(relativeWidths);
table.setWidthPercentage(100F);
printTableWidth(table, "after table created"); // absoluteWidths: [0.0, 0.0, 0.0]
return table;
}
private static float calculateTableWidthBasedOnPageA4(Document document, PdfPTable table) {
return (PageSize.A4.getWidth() - document.leftMargin() - document.rightMargin())
* table.getWidthPercentage() / 100;
}
private static void addToDocumentCellsAndTable(Document document, PdfPTable table) throws DocumentException {
// needed for calculation of getAbsoluteWidths: (B.1) add cells to table
table.addCell("Hello");
table.addCell("World");
table.addCell("!");
printTableWidth(table, "after cells added to table"); // absoluteWidths: [0.0, 0.0, 0.0]
// needed for calculation of getAbsoluteWidths: (B.2) add table to doc
document.add(table);
}
private static void addTextAcrossTableCells(PdfPTable table, String text) throws IOException, DocumentException {
// restrictions; this is width of a column from table
float maxColumnWidth = table.getAbsoluteWidths()[2]; // 330.12103F;
System.out.println("use width of third column as max: " + maxColumnWidth);
// sample font used for calculation of text-width
Font font = new Font(BaseFont.createFont("Courier", BaseFont.CP1250, true), 12);
// alamatSesuaiKtpPP
String splitText[] = getTextPartsUsingFontWithMaxWidth(text, font, maxColumnWidth);
String dottedLine = "..";
table.addCell("Alamat / Address:");
// p_alamat1_ct
Phrase phrase1 = new Phrase(splitText[0], font);
phrase1.add(dottedLine);
PdfPCell cell1 = new PdfPCell(phrase1);
cell1.setBackgroundColor(BaseColor.LIGHT_GRAY);
cell1.setBorder(PdfPCell.NO_BORDER);
table.addCell(cell1);
// p_alamat2_ct
Phrase phrase2 = new Phrase(splitText[1], font);
phrase2.add(dottedLine);
PdfPCell cell2 = new PdfPCell(phrase2);
cell2.setBorder(PdfPCell.NO_BORDER);
table.addCell(cell2);
}
private static String[] getTextPartsUsingFontWithMaxWidth(String text, Font font, float maxWidth) {
String results[] = new String[] {"",""};
String firstPartOfText = " ";
float widthOfText = 0;
for (int i = 0; i < text.length();i++) {
firstPartOfText += text.charAt(i);
widthOfText = font.getCalculatedBaseFont(true).getWidthPoint(firstPartOfText, font.getCalculatedSize());
System.out.printf("%d: widthOfText: %f\n", i, widthOfText);
if (widthOfText > maxWidth) {
results[0] = firstPartOfText.substring(0, firstPartOfText.length() - 1);
results[1] = text.substring(i); // second argument "text.length()" is not needed
break;
}
}
if (results[0] == "") {
results[0] = text;
}
return results;
}
private static void printTableWidth(PdfPTable table, String labelText) {
float[] absoluteWidths = table.getAbsoluteWidths();
System.out.println(labelText + "> getAbsoluteWidths: " + Arrays.toString(absoluteWidths));
}
}
关于java - 从 PdfPTable 列(iText)获取绝对宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54977787/
我的理解是 width: 100% 让元素的宽度与其父元素的宽度相同,而 width: inherit 只有在明确指定父元素的宽度时才这样做.这种理解是否正确? 如果是这样,在我看来,当 width:
并设置“高度”为全屏的 1/2。 这是我的代码: div{ background:red; } 最佳答案 我会结合使用 css 和 javascript(使
编辑 2: 问题似乎出在规则的“bigTable”元素上。显然,在布局模板上使用时继承了错误的最小宽度。我仍在调查此事。 不过,我将再尝试一次 div。一个大问题是使用固定导航和动态内容,但我已经为此
我的网站需要显示宽表。在它上面是标题,它应该和整个页面一样宽(在这种情况下,和表格一样宽)。但是,它的宽度与视口(viewport)(屏幕尺寸)一样宽,因此显示时看起来还不错,但是一旦用户滚动到侧面,
我有一个小问题。我总是使用 float 来安排我的元素。我正在转向 flexbox,我做了一些例子,一切都很好,但我正在做一个事情进展不顺利的例子。 我有一个包含 1 到 12 种产品的容器,每行 4
例如,它们在自动边距方面会导致完全不同的行为。 看看这个 fiddle :https://jsfiddle.net/L1rk46xy/ .fixed { display:fixed;
我尝试在帖子中将段落的宽度设置为 75%,并将图像的响应宽度设置为 100%。然而,总是在 默认。 Some texts Some texts Some texts Some texts 目前,我只
HTML 元素可以有 width/height 属性,也可以有 CSS width/height 属性: HTML 属性和 CSS 属性有什么区别,它们应该具有相同的效果吗? 最佳答案 有关该主题的
我有一个流动的 table ,现在需要一个固定的 thead。问题是当你固定 thead 时,th-s 不尊重 tbody 的 td-s 的宽度。列的大小都由 BootStrap 处理。我已经阅读了很
我想像这样布置一个区域: ---- ---- |A | |B | | | | | ---- ---- --------- |C | --------- 三个盒子中的每一个都是 .盒子
我遇到了很多问题。 1) 我正在使用 Bootstrap-Select 来获得具有搜索功能的现代选择框,但无论我尝试什么,我似乎都无法获得填充 col-span 的选择。 2) 我已将该行拆分为 2
http://jsfiddle.net/95EtZ/1/ 问题在行动中解决了一半。 现在它是用 javascript 中硬编码的容器宽度设置的。 我需要 js 来获取容器 div 的宽度——使用窗口滚
我想要两个宽度和高度均为 100% 的 div。我知道子 div 不会工作,因为父 div 没有特定的高度,但有没有办法解决这个问题? HTML: CSS: body
我需要使用 jQuery 更改 的高度和宽度 我尝试了以下代码 jQuery('#chart_popup').css('height','600px'); jQuery('#chart_popup')
在自定义 WPF 控件中,我想将控件的宽度设置为高度的函数。例如:Width = Height/3 * x; 实现此目的的最佳方法是什么,以便控件正确且流畅地调整大小(和初始大小)? 最佳答案 您可以
我正在使用igraph在R中绘制图形,执行plot(mygraph, vertex.color = "green")之类的操作。 有没有办法改变顶点边界的颜色和/或宽度? 最佳答案 查看下面的代码;
有没有办法使用jquery设置图像的高度和宽度?以下是我的代码 var img = new Image(); // Create image $(img).load(function(){
这个问题类似于 how-to-find-the-actual-width-of-grid-component-with-scrollbar-in-delphi 但我无法获取 CalcDrawInfo(
这里是 HTML/CSS 新手。 试图将我在 Codeacademy 上学到的知识付诸实践,但我遇到了一个问题,即我设置为 width:100% 的 header 最终离开了页面。我相信这是因为边框,
这里是 HTML/CSS 新手。 试图将我在 Codeacademy 上学到的知识付诸实践,但我遇到了一个问题,即我设置为 width:100% 的 header 最终离开了页面。我相信这是因为边框,
我是一名优秀的程序员,十分优秀!