Keep cell content on one page - java

I have a table, with one column. Each cell contains a paragraph.
How can I stop paragraphs from splitting across two pages?
PdfPtable table = new PdfPTable(1);
//report must be printed as compat as possible
table.setSplitLate(false);
//I can't set keep together, because table can be larger than page size
//table.setKeepTogether(true);
for (int i = 0; i < 100; i++) {
//Random text. Can contain ~400 chars.
String text = "aaaaaaaaaaaaaaa sssssssssssss ddddddddddd ffffffffff";
Paragraph p = new Paragraph(text);
//That instruction does not work. I don't know why, may be because paragraph printed in cell.
p.setKeepTogether(true);
table.addCell(p);
}

Change
table.setSplitLate(false);
into
table.setSplitLate(true);
This way, your cell will not be split unless the complete cell doesn't fit on a single page.

Related

iText shrink table column to fit contents

In my iText document, I have a lot of tables scattered around, each with only one row of two columns. I would like to automatically shrink the leftmost column to fit its contents, and expand the rightmost column to fill the remaining space.
The exact contents of these two columns varies greatly, so there's no way to determine ahead of time what the exact width should be.
All of the content in this screenshot is wrapped in one outer table. Each nested table has its two columns highlighted red and blue. I would like to shrink the red columns as narrow as they can get without forcing the text to take up more lines than it has to.
In this case, the contents of the red cells are just a paragraph each, but it's possible they may contain a further-nested table with two cells of its own (which probably faces the same problem).
Is there a simple way to expand one column and shrink another without specifying exact or relative widths?
If you're using iText7 (and ditching the table for layout altogether), you can achieve this look and layout by building on the following example:
Output looks like this:
Code used to generate output above:
public void createPdf(String dest) throws IOException, FileNotFoundException{
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdfDoc = new PdfDocument(writer);
Document doc = new Document(pdfDoc);
Paragraph p = new Paragraph();
Text t = new Text("Date:").setBold();
p.add(t);
t= new Text("10/12/17").setUnderline();
p.add(t);
p.add(new Tab());
p.add(createTwoPartBorderedText("Catalog Year: ","2017"));
p.add(new Tab());
p.add(createTwoPartBorderedText("L Number","2019284"));
doc.add(p);
doc.close();
}
public Paragraph createTwoPartBorderedText(String contentOne, String contentTwo){
Paragraph container= new Paragraph();
Text one = new Text(contentOne).setBold();
Border solidRed = new SolidBorder(Color.RED,1f);
one.setBorder(solidRed);
container.add(one);
Text two =new Text(contentTwo);
two.setUnderline();
Border solidBlue = new SolidBorder(Color.BLUE,1f);
two.setBorder(solidBlue);
container.add(two);
return container;
}

iText: List of images in a cell

I would like to create a table that has a list of dots. I don't know ahead of time how many dots I have, but if they overflow the cell, I want them to wrap, just like text would. My code is something like this:
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(new float[]{80});
table.setLockedWidth(true);
Phrase listOfDots = new Phrase();
for (int i = 0; i < 40; i++) {
listOfDots.add(new Chunk(pdf.correct, 0, 0));
listOfDots.add(new Chunk(" "));
}
table.addCell(listOfDots);
outerCell.addElement(table);
The dots wrap, like I expect, but they don't all have the same size. There are 7 rows of 5 dots each, and all 35 dots have the same size. The last row of 5 dots are roughly half the size of the others.
(I tried to post an image, but I'm not veteran enough on this site.)
Is there a way to make all the images the same size?
Please take a look at the ImagesInChunkInCell example. Instead of a bullet, I took the image of a light bulb. I was able to reproduce the problem you described, but as you can see in list_with_images.pdf, I was able to add one extra line:
Image image = Image.getInstance(IMG);
image.setScaleToFitHeight(false);
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(new float[]{120});
table.setLockedWidth(true);
Phrase listOfDots = new Phrase();
for (int i = 0; i < 40; i++) {
listOfDots.add(new Chunk(image, 0, 0));
listOfDots.add(new Chunk(" "));
}
table.addCell(listOfDots);
The extra line is:
image.setScaleToFitHeight(false);
This prevents that the image is scaled.

JTable-Paint the content(text) in a cell

I have a JTable and i have a method which implements search in table rows and columns, i use regular expressions and i want to paint(eg yellow) the text which matches with the regular expression in the cell. I want to paint the text not the background of the cell and only the part of word which matches with reg expression.
The code for my search method is:
for (int row = 0; row <= table.getRowCount() - 1; row++) {
for (int col = 0; col <= table.getColumnCount() - 1; col++) {
Pattern p = Pattern.compile("(?i)" + search_txt.getText().trim());
Matcher m = p.matcher(table.getValueAt(row, col).toString().trim());
if (m.find()){
isFound = true;
table.scrollRectToVisible(table.getCellRect(row, 0, true));
table.setRowSelectionInterval(row, row);
break;
}
}
}
You will need a custom renderer to do this.
The default renderer is a JLabel. So the only way to do this would to wrap HTML around your text string and change the font color of the text you are searching for. You would need to pass the search text to the renderer so the renderer can determine which text to highlight.
The code you posted has a problem in that it will always scroll to the bottom of the table. So what is your exact requirement. Do you want to highlight all cells at one time. Or do you just want to have a "Next" button that will find the next cell with the text. In the first case you would not want to automatically scroll the table. In the second case you would scroll the table.
Also, depending on the requirement you would need to either repaint the entire table (if you show all occurrences at once) or only the current row (if you have next functionality).
Edit:
Normally when you add text to a label you use:
label.setText("user1005633");
If you want to highlight any text containing "100" then you would need to do:
label.setText("<html>user<font color="yellow">100</font>5633</html>");
This is what I mean by wrapping.

How to get height and width of excel sheet"named index" area from HSSFWorkbook POI API

How to get height and width of excel sheet named range area from HSSFWorkbook POI API.
I got one reference from google but not able to get height and width of named index area of excel sheet.
Thanks in advance.
Yes it's named range, and want height and width of cells aquired by named range. below code having "print_area" named range and want to height and width of cells.
int namedCellIdx = workbook.getNameIndex("Print_Area");
HSSFName aNamedCell = workbook.getNameAt(namedCellIdx);
// retrieve the cell at the named range and test its contents
String a = aNamedCell.getReference();
AreaReference aref = new AreaReference(aNamedCell.getRefersToFormula());
CellReference[] crefs = aref.getAllReferencedCells();
for (int i=0; i<crefs.length; i++) {
Sheet s = workbook.getSheet(crefs[i].getSheetName());
Row r = sheet.getRow(crefs[i].getRow());
System.out.println(r.getHeight());
System.out.println(sheet.getColumnWidth(crefs[i].getCol()));;
//here want height and width of "Print_area" cells
Cell c = r.getCell(crefs[i].getCol());
System.out.println(c.getStringCellValue());
// extract the cell contents based on cell type etc.
}
Read the API docs at http://poi.apache.org/apidocs/index.html
Specifically, look at org.apache.poi.ss.util.AreaReference, from which you can get the first and last cell references and determine the size of the range.
However, you have to cope with a number of special cases, such as areas that are an entire column or entire row, or even non-contiguous.

Using itext for printing at absolute positions

What is the recommended way of printing a text document as a pdf using absolute positioning ?
I am having a table that I have to print. I am also having the data type lengths and starting positions of the columns.
Since the existing table was a character based, there was no problem in its positioning. But even after using a monotype font (Courier, 10) I am not able to properly position the data and last column(s) of each row erroneously skip to the next line.
In order to present my data as close as the character one, I divided the page into different columns(based on its page size) and then add the contents at the desired place. I am adding chunks of data into the paragraph.
paragraph.add(new Chunk(new VerticalPositionMark(), columnNo*ptUnit, false));
I have tried to tweak the page size, font size and margin lengths, but the data is not properly displayed. Have you encountered any such problems ? please do share your thoughts.
Have you tried ColumnText
When i want to write a paragraph and I do know the amount of lines...I do a cycle incrementing (even it says incrementing and is minus is because the pdf is from "south" to "north" (0 - height) the y in a proportion of the fontsize, something like this
//_valueArray is my string[]
//fontSize is the value of the Size of the font...
//1.5 it's just a magic number :) that give me the space line that i need
//cbLocal is the PdfContentByte of the pdf
for (i = 0; i < _valueArray.Length; i++)
{
var p = new Phrase(_valueArray[i], font);
ColumnText.ShowTextAligned(cbLocal, align, p, x, y, 0);
if (i + 1 != _valueArray.Length)
{
y = y - (fontSize*1.5f);
}
}

Categories