iText column width wrong - java

Im using iText in android to build a pdf file with a table . It seems easy to do but the result is wrong. Help appreciated!
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(output));
Document documento = new Document(pdfDoc);
float[] columnWidths = new float[]{40, 160, 50, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25};
Table table = new Table(columnWidths);
table.setWidth(500);
com.itextpdf.layout.element.Cell cell = new com.itextpdf.layout.element.Cell(1, columnWidths.length).add("(Continuação)");
table.addHeaderCell(cell);
cell = new com.itextpdf.layout.element.Cell(1, columnWidths.length).add("Continua...");
table.addFooterCell(cell);
table.setSkipFirstHeader(true);
table.setSkipLastFooter(true);
Cell cellA;
for (int i = 0; i < 100; i++) {
cellA = new Cell(1, 1).add(String.valueOf(i+1));
cellA.setTextAlignment(TextAlignment.CENTER);
cellA.setWidth(40);
cellA.setFontSize(7);
table.addCell(cellA);
cellA = new Cell(1, 1).add("ALYNE BORGES MADEIRA");
cellA.setTextAlignment(TextAlignment.LEFT);
cellA.setWidth(160);
cellA.setFontSize(7);
table.addCell(cellA);
cellA = new Cell(1, 1).add("100.00");
cellA.setTextAlignment(TextAlignment.CENTER);
cellA.setWidth(50);
cellA.setFontSize(7);
table.addCell(cellA);
for (int j = 0; j < 10; j++) {
cellA = new Cell(1, 1).add("10.00");
cellA.setTextAlignment(TextAlignment.CENTER);
cellA.setWidth(25);
cellA.setFontSize(7);
table.addCell(cellA);
}
}
The problem is in the last column where width is not equal to prior 9.

Summary
If you are only using it to generate PDF tables instead of needing RTF and HTML output, then it might be better to use the PdfPTable class [Table is now unsupported] which is better supported and less prone to quirks (as explained here: PdfpTable vs. Table (vs. SimpleTable?)).
Sizing
If you use PdfPTable with setWidths(float[]) then you should be able to do absolute widths without needing to specify the width in each cell for code readability. (You can also use percentage widths, which, for a basic table, might be easier to get aligned properly on the page.)
Headers & Footers
Since you are using headers & footers, PdfPTable has setHeaderRows(int) and setFooterRows(int), but note that:
The number of footer rows are subtracted from the header rows. For example, for a table with two header rows and one footer row the code would be:
table.setHeaderRows(3);
table.setFooterRows(1);
PdfPCell Porting
Also note that PdfPCell has changed setTextAlignment to be setHorizontalAlignment.
Examples and Documentation
Some examples of using it are here (note the usage of setLockedWidth: https://developers.itextpdf.com/examples/tables-itext5/cell-and-table-widths
The JavaDoc for PdfPTable is here: http://itextsupport.com/apidocs/itext5/5.5.9/com/itextpdf/text/pdf/PdfPTable.html
And the JavaDoc for PdfPCell is here: http://itextsupport.com/apidocs/itext5/5.5.9/com/itextpdf/text/pdf/PdfPCell.html

Related

Text not getting center aligned horizontally in itext? [duplicate]

I have a C# application that generates a PDF invoice. In this invoice is a table of items and prices. This is generated using a PdfPTable and PdfPCells.
I want to be able to right-align the price column but I cannot seem to be able to - the text always comes out left-aligned in the cell.
Here is my code for creating the table:
PdfPTable table = new PdfPTable(2);
table.TotalWidth = invoice.PageSize.Width;
float[] widths = { invoice.PageSize.Width - 70f, 70f };
table.SetWidths(widths);
table.AddCell(new Phrase("Item Name", tableHeadFont));
table.AddCell(new Phrase("Price", tableHeadFont));
SqlCommand cmdItems = new SqlCommand("SELECT...", con);
using (SqlDataReader rdrItems = cmdItems.ExecuteReader())
{
while (rdrItems.Read())
{
table.AddCell(new Phrase(rdrItems["itemName"].ToString(), tableFont));
double price = Convert.ToDouble(rdrItems["price"]);
PdfPCell pcell = new PdfPCell();
pcell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
pcell.AddElement(new Phrase(price.ToString("0.00"), tableFont));
table.AddCell(pcell);
}
}
Can anyone help?
I'm the original developer of iText, and the problem you're experiencing is explained in my book.
You're mixing text mode and composite mode.
In text mode, you create the PdfPCell with a Phrase as the parameter of the constructor, and you define the alignment at the level of the cell. However, you're working in composite mode. This mode is triggered as soon as you use the addElement() method. In composite mode, the alignment defined at the level of the cell is ignored (which explains your problem). Instead, the alignment of the separate elements is used.
How to solve your problem?
Either work in text mode by adding your Phrase to the cell in a different way.
Or work in composite mode and use a Paragraph for which you define the alignment.
The advantage of composite mode over text mode is that different paragraphs in the same cell can have different alignments, whereas you can only have one alignment in text mode. Another advantage is that you can add more than just text: you can also add images, lists, tables,... An advantage of text mode is speed: it takes less processing time to deal with the content of a cell.
private static PdfPCell PhraseCell(Phrase phrase, int align)
{
PdfPCell cell = new PdfPCell(phrase);
cell.BorderColor = BaseColor.WHITE;
// cell.VerticalAlignment = PdfCell.ALIGN_TOP;
//cell.VerticalAlignment = align;
cell.HorizontalAlignment = align;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
return cell;
}
Here is my derivation of user2660112's answer - one method to return a cell for insertion into a bordered and background-colored table, and a similar, but borderless/colorless variety:
private static PdfPCell GetCellForBorderedTable(Phrase phrase, int align, BaseColor color)
{
PdfPCell cell = new PdfPCell(phrase);
cell.HorizontalAlignment = align;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
cell.BackgroundColor = color;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
return cell;
}
private static PdfPCell GetCellForBorderlessTable(Phrase phrase, int align)
{
PdfPCell cell = new PdfPCell(phrase);
cell.HorizontalAlignment = align;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
cell.BorderWidth = PdfPCell.NO_BORDER;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
return cell;
}
These can then be called like so:
Font timesRoman9Font = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 9, BaseColor.BLACK);
Font timesRoman9BoldFont = FontFactory.GetFont(FontFactory.TIMES_BOLD, 9, BaseColor.BLACK);
Phrase phrasesec1Heading = new Phrase("Duckbills Unlimited", timesRoman9BoldFont);
PdfPCell cellSec1Heading = GetCellForBorderedTable(phrasesec1Heading, Element.ALIGN_LEFT, BaseColor.YELLOW);
tblHeadings.AddCell(cellSec1Heading);
Phrase phrasePoisonToe = new Phrase("Poison Toe Toxicity Level (Metric Richter Scale, adjusted for follicle hue)", timesRoman9Font);
PdfPCell cellPoisonToe = GetCellForBorderlessTable(phrasePoisonToe, Element.ALIGN_LEFT);
tblFirstRow.AddCell(cellPoisonToe);
I ended up here searching for java Right aligning text in PdfPCell. So no offense if you are using java please use given snippet to achieve right alignment.
private PdfPCell getParagraphWithRightAlignCell(Paragraph paragraph) {
PdfPCell cell = new PdfPCell(paragraph);
cell.setBorderColor( BaseColor.WHITE);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
return cell;
}
In getParagraphWithRightAlignCell pass paragraph
Thanks
Perhaps its because you are mixing the different ways to add the cells? Have you tried explicitly creating a cell object, massaging it however you want then adding it for every cell?
Another thing you could try is setting the vertical alignment as well as the horizontal.
cell.HorizontalAlignment = Element.ALIGN_RIGHT;

Optimize writing ods files with simple-odf

This is my code to write my file:
SpreadsheetDocument ods = SpreadsheetDocument.newSpreadsheetDocument();
Table table = Table.newTable(ods, 4000, 20, 0, 0);
table.setTableName("foo");
Border border = new Border(Color.BLACK, 1, StyleTypeDefinitions.SupportedLinearMeasure.PT);
Font font = new Font("Arial", FontStyle.BOLD, 7, Color.BLACK);
List<Row> rows = table.getRowList();
for (Row r : rows) {
for (int a = 0; a < 20; a++) {
Cell cell = r.getCellByIndex(a);
cell.setStringValue("Foo " + a);
cell.setBorders(CellBordersType.ALL_FOUR, border);
cell.setCellBackgroundColor(Color.valueOf("#A5A5A5"));
cell.setFont(font);
cell.setHorizontalAlignment(HorizontalAlignmentType.CENTER);
}
}
ods.save("K://foo.ods");
In this code I set the style at the cell level. To optimize the writing I want to know if there is any way to do for row or table level. Or create a style for border, font, size, etc ... in the document and set style with function setCellStyleName. I can do something like this?
The reason is because I get this error:
java.lang.OutOfMemoryError: Java heap space at
java.util.ArrayList.iterator(ArrayList.java:814) at
sun.nio.ch.WindowsSelectorImpl.updateSelectedKeys(WindowsSelectorImpl.java:496)
at
sun.nio.ch.WindowsSelectorImpl.doSelect(WindowsSelectorImpl.java:172)
at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87) at
sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98) at
org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:1050)
at java.lang.Thread.run(Thread.java:745)
If I delete the format (border, font ...), I can write more rows.
If I open the content.xml, I can see that I have many defined styles that are equal.
I'm using this version:
<dependency>
<groupId>org.apache.odftoolkit</groupId>
<artifactId>simple-odf</artifactId>
<version>0.7-incubating</version>
</dependency>
Here is the sample code of apply ODF style to cell. I cannot find a easy solution to create style. What I do is createing a ods file, check the child element of office:automatic-styles in content.xml then convert it to java code.
SpreadsheetDocument ods = SpreadsheetDocument.newSpreadsheetDocument();
Table table = Table.newTable(ods, 4000, 20, 0, 0);
table.setTableName("foo");
//create style
OdfOfficeAutomaticStyles astyles = ods.getContentDom().getOrCreateAutomaticStyles();
StyleStyleElement ele = astyles.newStyleStyleElement(OdfStyleFamily.TableCell.getName(), "myss");
StyleTableCellPropertiesElement styleTableCellPropertiesElement = ele.newStyleTableCellPropertiesElement();
styleTableCellPropertiesElement.setFoBackgroundColorAttribute("#A5A5A5");
styleTableCellPropertiesElement.setFoBorderAttribute("1.0pt solid #000000");
ele.newStyleParagraphPropertiesElement().setFoTextAlignAttribute(HorizontalAlignmentType.CENTER.toString());
StyleTextPropertiesElement styleTextPropertiesElement = ele.newStyleTextPropertiesElement(null);
styleTextPropertiesElement.setStyleFontNameAttribute("Arial");
styleTextPropertiesElement.setFoFontSizeAttribute("7.0pt");
styleTextPropertiesElement.setFoColorAttribute(Color.BLACK.toString());
styleTextPropertiesElement.setFoFontWeightAttribute("bold");
List<Row> rows = table.getRowList();
for (Row r : rows) {
for (int a = 0; a < 10; a++) {
Cell cell = r.getCellByIndex(a);
cell.setStringValue("Foo " + a);
cell.setCellStyleName("myss");
}
}

Need to make PDF sample with boxes as table columns by android app

I need to make PDF sample file just like the below image but I didn't find any suitable guide to do exactly like this. I have followed some links
http://itextpdf.com/examples/iia.php?id=102
Is there a way to draw a rectangle into a PdfPCell in iText (the Java version)?
but from the second link I didn't understand how could I make more likely to my requirement.
Thanks in advance.
It is unclear to me why you refer to this example: http://itextpdf.com/examples/iia.php?id=102
The PDF that is created with that example shows me in a Superman outfit. What is the link with creating a table with rounded borders?
Please take a look at the NestedTableRoundedBorder example. It creates a PDF that looks like this: nested_table_rounded_border.pdf
This construction consists of nested tables. The outer table only has one column, but we use it to create the rounded corners:
class RoundRectangle implements PdfPCellEvent {
public void cellLayout(PdfPCell cell, Rectangle rect,
PdfContentByte[] canvas) {
PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
cb.roundRectangle(
rect.getLeft() + 1.5f, rect.getBottom() + 1.5f, rect.getWidth() - 3,
rect.getHeight() - 3, 4);
cb.stroke();
}
}
This cell event is used like this:
cell = new PdfPCell(innertable);
cell.setCellEvent(roundRectangle);
cell.setBorder(Rectangle.NO_BORDER);
cell.setPadding(8);
outertable.addCell(cell);
The inner tables are used to create cells with or without borders, for instance like this:
// inner table 1
PdfPTable innertable = new PdfPTable(5);
innertable.setWidths(new int[]{8, 12, 1, 4, 12});
// first row
// column 1
cell = new PdfPCell(new Phrase("Record Ref:"));
cell.setBorder(Rectangle.NO_BORDER);
innertable.addCell(cell);
// column 2
cell = new PdfPCell(new Phrase("GN Staff"));
cell.setPaddingLeft(2);
innertable.addCell(cell);
// column 3
cell = new PdfPCell();
cell.setBorder(Rectangle.NO_BORDER);
innertable.addCell(cell);
// column 4
cell = new PdfPCell(new Phrase("Date: "));
cell.setBorder(Rectangle.NO_BORDER);
innertable.addCell(cell);
// column 5
cell = new PdfPCell(new Phrase("30/4/2015"));
cell.setPaddingLeft(2);
innertable.addCell(cell);
// spacing
cell = new PdfPCell();
cell.setColspan(5);
cell.setFixedHeight(3);
cell.setBorder(Rectangle.NO_BORDER);
innertable.addCell(cell);
If some of the dimensions are quite like you want, it's sufficient to change parameters such as the widths array, the padding, the fixed height, etc.

Auto size height for rows in Apache POI

I am inputting values into a spreadsheet using Apache POI. These values have newlines, and I was able to use this code successfully:
CellStyle style = cell.getCellStyle()
style.setWrapText(true)
cell.setCellStyle(style)
Unfortunately, while the text is wrapping correctly, the rows are not always growing in height enough to show the content. How do I ensure that my rows are always the correct height?
currentRow.setHeight((short)-1)
Works for XSSFCell and Excel 2013
HSSFWorkbook workbook=new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("FirstSheet");
HSSFRow rowhead= sheet.createRow((short)0);
HSSFCellStyle style = workbook.createCellStyle();
style.setWrapText(true);
row.setRowStyle(style);
row.getCell(0).setCellStyle(style);
The above code will generate dynamic height of rows.
The only way I got this to work was write my own implementation to calculate the row height. The code is now released as the Taro project, so you could use that. It has numerous convenience methods to let you write an Excel file in far fewer lines of code.
If you prefer to put the implementation in your own code, you can find it in the SpreadsheetTab class. There is an autoSizeRow(int rowIndex) method half way down. It basically iterates down the row and for each cell finds the number of lines of text, then uses the font size to calculate the optimal cell height. It then sets the row height to the height of the tallest cell.
See all this link, which provides some code to manually calculate the correct height for a row, based on the column width and cell content. I've not personally tested it. Also pasted below for convenience:
// Create Font object with Font attribute (e.g. Font family, Font size, etc) for calculation
java.awt.Font currFont = new java.awt.Font(fontName, 0, fontSize);
AttributedString attrStr = new AttributedString(cellValue);
attrStr.addAttribute(TextAttribute.FONT, currFont);
// Use LineBreakMeasurer to count number of lines needed for the text
FontRenderContext frc = new FontRenderContext(null, true, true);
LineBreakMeasurer measurer = new LineBreakMeasurer(attrStr.getIterator(), frc);
int nextPos = 0;
int lineCnt = 0;
while (measurer.getPosition() < cellValue.length())
{
nextPos = measurer.nextOffset(mergedCellWidth); // mergedCellWidth is the max width of each line
lineCnt++;
measurer.setPosition(nextPos);
}
Row currRow = currSht.getRow(rowNum);
currRow.setHeight((short)(currRow.getHeight() * lineCnt));
// The above solution doesn't handle the newline character, i.e. "\n", and only
// tested under horizontal merged cells.
cell.getRow().setHeight((short) -1);
Worked for HSSFCell in apache poi 3.9 or above
It works in Excel 2010.
I set the limit of cell length of 50 characters
Row row = sheet.createRow(0);
CellStyle style = workbook.createCellStyle();
style.setWrapText(true);
if (data.length() > 50) {
for (int i = 1; i <= Math.abs(data.length() / 50); i++) {
data = data.substring(0, i * 50) + "\n" + data.substring(i * 50);
}
Cell cell = row.createCell(0);
row.setRowStyle(style);
cell.setCellStyle(style);
cell.setCellValue(data);
sheet.autoSizeColumn(0);
}
In my case a robust solution was to calculate the number of lines and set the row height to a multiple of the default row height:
int numberOfLines = cell.getStringCellValue().split("\n").length;
row.setHeightInPoints(numberOfLines*sheet.getDefaultRowHeightInPoints());
You can't adjust cell height directly.
But you can change the row's height
final HSSFSheet fs = wb.createSheet("sheet1");
final HSSFRow row0 = fs.createRow(0);
final HSSFCell cellA1 = row0.createCell(0);
row0.setHeight((short)700);
Row aitosize work for me:
cell.getRow().setHeight((short)0);
Here 0 for calculate autoheight.
Workaround for “LibreOffice Calc“ and “WPS Spreadsheet” with auto height for merged sells.
I add a column out to the right of a main document (In my case it was 32 column)
Set width as all merged cells with same text.
Set style WrapText to true
Set style to Align Top
Copy content which will be displayed in the merged cells
Set that column to be hidden
Set a row height = -1
A sample of code:
private void applyRowHightWorkaroundForMergedCells(HSSFCell cell0) {
HSSFSheet sheet = cell0.getSheet();
HSSFRow row = cell0.getRow();
String value = cell0.getStringCellValue();
HSSFCell cell = row.createCell(32);
sheet.setColumnWidth(32, 32000);
cell.getCellStyle().setWrapText(true);
cell.getCellStyle().setVerticalAlignment(VerticalAlignment.TOP);
cell.setCellValue(value);
sheet.setColumnHidden(32, true);
row.setHeight((short) -1);
}
//we can use column width for sheet
Ex: sheet.setColumnWidth(0, 2000);

iText - PdfPTable RowSpan using mytable.writeSelectedRows

I'm using iText 5.1.3, and I want to add a header to my Pdf Document. I used the known solution posted here: http://itextpdf.com/examples/iia.php?id=104
This solution used the PdfPageEventHelper class, and overridden the method onEndPage() to add the Header exactly after finishing every page. The example provided in the link above works fine as it adds a table as the Header of the document. I'm trying to do exactly the same with 1 difference, that I want some cells in that table to have Rowspan and/or Colspan.
I tried, and found that using table.writeSelectedRows() differs from document.add(table) when it comes to Rowspan. This is a sample of what I'm trying to do in onEndPage:
PdfPTable mytable = new PdfPTable(3);
mytable.setTotalWidth(527);
PdfPCell cell1 = new PdfPCell(new Phrase("Hello"));
cell1.setColspan(2);
cell1.setRowspan(2);
mytable.addCell(cell1);
PdfPCell cell2 = new PdfPCell(new Phrase("Girls !"));
mytable.addCell(cell2);
PdfPCell cell3 = new PdfPCell(new Phrase("Boys !"));
mytable.addCell(cell3);
mytable.writeSelectedRows(0, -1, 34, 803, writer.getDirectContent());
and instead of having a cell at the left as 2x2 with "Hello", I get the "Hello" cell as 1x2 not 2x2
Any ideas?
well .. I found the solution myself :D It's simply replacing mytable.writeSelectedRows(0, -1, 34, 803, writer.getDirectContent()); with the following:
ColumnText column = new ColumnText(writer.getDirectContent());
column.addElement(mytable);
column.setSimpleColumn(-12, -20, 604, 803); // set LLx, LLy, URx, and URy of the header
column.go();
That's it :) worked :)
iText writeSelectedRows not work with rowSpan when I use iText-2.1.7, The rowSpan cell not grow to other rows,But it has a workaround.
ArrayList tmp = table.getRows(0, table.getRows().size());
table.getRows().clear();
table.getRows().addAll(tmp);
table.writeSelectedRows(0, -1, x, y, directContent);

Categories