Change the width of the cell content in java using PDFCell - java

Hi I have an app which generates PDF page using java iText, for a single column the width is changing so some times if there are more than 16 character the rest of the character go below in the PDF page
For Example I have a customer Acct which varies from 10 - 16 characters it appears like on my PDF page like
GW1BKCADT402814
1
instead i need it on the same line GW1BKCADT4028141.
The last character goes below in that column.
PdfPTable tTable = new PdfPTable( 13 );
PdfPCell c1 = PDFConstants.getDefaultPdfPCell( PdfPCell.ALIGN_LEFT, 2 );
c1.setPhrase( new Phrase( new Chunk( t.getCustomerAcc(), StyleConstantsPDF.FONT_NORAMAL_8 ) ));
tTable.addCell( c1 );

Related

How to specify the row index when I want to append the specified data in a specified row and column (lowagie)

I am currently using the com.lowagie to generate all my record into a pdf file. Then I want to add certain data in a specify row and column. How to do this? My code shown below is fail.
Picture - My desired output will be like the table in the picture when I generate the table in the pdf file.
for(int i =0; i<AcademicSystem.allStdInfo.size();i++){
Student std = AcademicSystem.allStdInfo.get(i);
Cell c1 = new Cell(std.getName());
Cell c2 = new Cell(std.getGender());
t.addCell(c1,0,i);
}

Insert page number with text in footer aspose word java

I want to insert page number in footer alongside with text, but when footer contains some text, page number and text in footer switch places.
I am doing this while converting document from html to word using aspose words java library.
Text in footer is sent from html and I just want to add page number.
Code for add page number in footer:
log.debug("Add page number");
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert PAGE field into the footer
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.insertField("PAGE", null);
builder.write("/");
builder.insertField("NUMPAGES", null);
Also, is there any way to replace whole text in footer?
You can add page numbers and text together in footer using table as following:
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert PAGE field into the footer
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.startTable();
// Clear table borders
builder.getCellFormat().clearFormatting();
builder.insertCell();
// Set first cell to 1/3 of the page width.
builder.getCellFormat().setPreferredWidth(
PreferredWidth.fromPercent(100 / 3));
// Insert page numbering text here.
// It uses PAGE and NUMPAGES fields to auto calculate current page
// number and total number of pages.
builder.insertField("PAGE", null);
builder.write("/");
builder.insertField("NUMPAGES", null);
// Align this text to the left.
builder.getCurrentParagraph().getParagraphFormat()
.setAlignment(ParagraphAlignment.LEFT);
builder.insertCell();
// Set the second cell to 2/3 of the page width.
builder.getCellFormat().setPreferredWidth(
PreferredWidth.fromPercent(100 * 2 / 3));
builder.write("(C) 2017 Aspose Pty Ltd. All rights reserved.");
// Align this text to the right.
builder.getCurrentParagraph().getParagraphFormat()
.setAlignment(ParagraphAlignment.RIGHT);
builder.endRow();
builder.endTable();
To replace whole text of footer you may access the footer, clear all text and add new contents as following:
DocumentBuilder builder = new DocumentBuilder(doc);
Section currentSection = builder.getCurrentSection();
com.aspose.words.HeaderFooter primaryHeader = currentSection.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
primaryHeader.getParagraphs().clear();
...
I am Tilal Ahmad, developer evangelist at Aspose.

Stop iText table from spliting on new page

I am developing an app for android that generates pdf.
I am using itextpdf to generate the pdf.
I have the following problem:
I have a table that has 3 rows and when this table is near the end of a page sometimes it puts one row on one page and two rows on the next page.
Is there a way to force this table to start on the next page so I can have the full table on the next page?
Thanks
As an alternative to Bruno's approach of nesting the table in a 1-cell table to prevent splitting, you can also use PdfPTable.setKeepTogether(true) to start the table on a new page when it doesn't fit the current page.
Using a similar example:
Paragraph p = new Paragraph("Test");
PdfPTable table = new PdfPTable(2);
for (int i = 1; i < 6; i++) {
table.addCell("key " + i);
table.addCell("value " + i);
}
for (int i = 0; i < 40; i++) {
document.add(p);
}
// Try to keep the table on 1 page
table.setKeepTogether(true);
document.add(table);
Both approaches (nesting in a 1-cell table and using setKeepTogether()) behave exactly the same in my tests. This includes when the table is too large to fit on the new page and still needs to be split, e.g. when adding 50 instead of 5 rows in the example above.
Please take a look at the Splitting example:
Paragraph p = new Paragraph("Test");
PdfPTable table = new PdfPTable(2);
for (int i = 1; i < 6; i++) {
table.addCell("key " + i);
table.addCell("value " + i);
}
for (int i = 0; i < 40; i++) {
document.add(p);
}
document.add(table);
We have a table with 5 rows, and in this case, we're adding some paragraphs so that the table is added at the end of a page.
By default, iText will try not to split rows, but if the full table doesn't fit, it will forward the rows that don't fit to the next page:
You want to avoid this behavior: you don't want the table to split.
Knowing that iText will try to keep full rows intact, you can work around this problem by nesting the table you don' want to split inside another table:
PdfPTable nesting = new PdfPTable(1);
PdfPCell cell = new PdfPCell(table);
cell.setBorder(PdfPCell.NO_BORDER);
nesting.addCell(cell);
document.add(nesting);
Now you get this result:
There was sufficient space on the previous page to render a couple of rows, but as we've wrapped the full table inside a row with a single column, iText will forward the complete table to the next page.

Large table in table cell invoke page break

I have single PdfPTable with single column. One page fit 50 rows. If I add some text data to table (for an example, 300 rows), report work fine. When I add a PdfPTable into cell (for example, 20 string cells, PdfPTable(with 20 rows in it or less), and 270 string cells), all work fine too:
/--------
20 string rows
inner table (20 rows)
10 string rows
/-------
...additional rows
But, when my inner table have more rows (mainTable[20 string rows, innerTable[90 string rows], 270 string rows], report break first page, and start innerTable output on the second page.
/---------
20 string rows
whitespace for 30 rows
/---------
inner table (50 rows from 90)
/---------
inner table (40 rows from 90)
..additional data
And I need this:
/---------
20 string rows
inner table (30 rows from 90)
/---------
inner table (50 rows from 90)
/---------
inner table (10 rows from 80)
..additional data
Anybody know solution?
ps itext version - 2.1.7
Please take a look at the NestedTables2 example. In this example, we tell iText that it's OK to split cells as soon as a row doesn't fit on a page:
This is not the default: by default, iText will try to keep a row intact by forwarding it to the next page. Only if the row doesn't fit the page, the row will be split.
Changing the default involves adding a single line to your code. That line is called: table.setSplitLate(false);
This is the full method that created the PDF shown in the screen shot:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(2);
table.setSplitLate(false);
table.setWidths(new int[]{1, 15});
for (int i = 1; i <= 20; i++) {
table.addCell(String.valueOf(i));
table.addCell("It is not smart to use iText 2.1.7!");
}
PdfPTable innertable = new PdfPTable(2);
innertable.setWidths(new int[]{1, 15});
for (int i = 0; i < 90; i++) {
innertable.addCell(String.valueOf(i + 1));
innertable.addCell("Upgrade if you're a professional developer!");
}
table.addCell("21");
table.addCell(innertable);
for (int i = 22; i <= 40; i++) {
table.addCell(String.valueOf(i));
table.addCell("It is not smart to use iText 2.1.7!");
}
document.add(table);
document.close();
}
Note that you should not use iText 2.1.7. If you do, you could have a problem with your employer, customer, investor. Read more about this in the legal section of the free ebook The Best iText Questions on StackOverflow

iText: Table(com.​lowagie.​text) created only occupies 80% of the entire page , How to make it utilise the entire page

I am writing the following code in order to create a PDF file with a table in it.
Document document = new Document(PageSize.A4, 20, 20, 20, 80);
Font myfont = new Font(FontFactory.getFont(FontFactory.COURIER_BOLD, 13, Font.NORMAL));
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(request.getRealPath("/") + "SAMPLE.pdf"));
document.open();
Table table = new Table(2);
Cell c2 = new Cell();
int[] widths = {8, 150}; //Tried different values, but no change
table.setBorder(Rectangle.BOX);
table.setAlignment(Element.ALIGN_LEFT);
table.setSpacing(0);
table.setPadding(0);
table.setTableFitsPage(true); //Tried with 'false', even removed it, but no change
table.setWidths(widths);
c2 = new Cell(new Paragraph("1. ", myfont));
c2.setBorder(Rectangle.NO_BORDER);
table.addCell(c2);
c2 = new Cell(new Paragraph("TEST DATA ", myfont));
c2.setBorder(Rectangle.NO_BORDER);
table.addCell(c2);
c2 = new Cell(new Paragraph("2. ", myfont));
c2.setBorder(Rectangle.NO_BORDER);
table.addCell(c2);
c2 = new Cell(new Paragraph("TEST DATA", myfont));
c2.setBorder(Rectangle.NO_BORDER);
table.addCell(c2);
c2 = new Cell(new Paragraph("3. ", myfont));
c2.setBorder(Rectangle.NO_BORDER);
table.addCell(c2);
c2 = new Cell(new Paragraph("TEST DATA", myfont));
c2.setBorder(Rectangle.NO_BORDER);
table.addCell(c2);
document.add(table);
document.close();
But the file created contains a table that occupies around 80-85% of the page. I want it to utilise the entire page.
I tried making some adjustments to the code like changing the table.setTableFitsPage(true); to table.setTableFitsPage(false); and even tried removing it.
also altered around with the widths assigned. But in vain as in all cases it only gave me the file with a table occupying only 80-85% of page.
Is there something I am missing to add to my code or is there an attribute that is stopping the Table from taking up 100% of page.
It creates problem when the content is large as I end up getting long tables with spaces on the page still unoccupied.
Attached a screen-shot of the actual PDF file generated here!
Screen-shot of the PDF generated
You should rewrite your table code to use PdfPTable instead. You can find some examples of its use online. The entire 4th chapter of iText in Action 2nd ed is about tables, PdfPTables to be precise.
Lots of example code. Enjoy.
Use the PdfPTable class instead, and you can set the column widths on the entire table. Making it span the entire page can be done by constructing a
float delta = document.getPageSize().getWidth() / numberOfColumns;
// add delta numberOfColumn times
PdfPTable table = new PdfPTable(new float[] {delta, delta, delta, delta });
table.setWidthPercentage(100f);
The widths passed in the constructor determine how much space a given column gets relative to the width of the entire table.

Categories

Resources