Aspose Java - Add Table below a list value - java

The following is the format I want to generate:
From my limited experience of the API I know how to make a list
ListFormat listFormat = builder.getListFormat();
listFormat.setList(document.getLists().add(ListTemplate.NUMBER_DEFAULT));
builder.writeln("Component Specification")
listFormat.setListLevelNumber(1);
builder.writeln("Raw Material Specification")
Now how do I transition from the list to the table below?
writing a new line adds numbers, even in the cells of the table.
If I set the listformat to null then the indentation is lost. I want to maintain the indentation of the list but also add the table below that list value
Anyway to do that?

#Furqan,
You need to set Table LeftIndent property for your requirements. Please check following sample code snippet.
I am Tilal Ahmad, developer evangelist at Aspose.
Document doc= new Document();
DocumentBuilder builder= new DocumentBuilder(doc);
ListFormat listFormat = builder.getListFormat();
listFormat.setList(doc.getLists().add(ListTemplate.NUMBER_DEFAULT));
builder.writeln("Component Specification");
builder.getListFormat().listIndent();
builder.getListFormat().getListLevel().setNumberStyle(NumberStyle.ARABIC);
builder.getListFormat().getListLevel().setNumberFormat("\u0000.\u0001");
builder.writeln("Raw Material Specification");
builder.getListFormat().removeNumbers();
Table table = new Table(doc);
// Add the table to the document.
doc.getFirstSection().getBody().appendChild(table);
Row row1 = new Row(doc);
row1.getRowFormat().setAllowBreakAcrossPages(true);
table.appendChild(row1);
// Create a cell and add it to the row
Cell cell1 = new Cell(doc);
cell1.getCellFormat().getShading()
.setBackgroundPatternColor(Color.LIGHT_GRAY);
cell1.getCellFormat().setWidth(50);
// Add a paragraph to the cell as well as a new run with some text.
cell1.appendChild(new Paragraph(doc));
cell1.getFirstParagraph().appendChild(new Run(doc, "Col1"));
// Add the cell to the row.
row1.appendChild(cell1);
// We would then repeat the process for the other cells and rows in the
// table.
// We can also speed things up by cloning existing cells and rows.
row1.appendChild(cell1.deepClone(false));
row1.getLastCell().appendChild(new Paragraph(doc));
row1.getLastCell().getFirstParagraph()
.appendChild(new Run(doc, "Col2"));
row1.appendChild(cell1.deepClone(false));
row1.getLastCell().appendChild(new Paragraph(doc));
row1.getLastCell().getFirstParagraph()
.appendChild(new Run(doc, "Col3"));
Row row = new Row(doc);
row.getRowFormat().setAllowBreakAcrossPages(true);
table.appendChild(row);
// Create a cell and add it to the row
Cell cell = new Cell(doc);
cell.getCellFormat().setWidth(50);
// Add a paragraph to the cell as well as a new run with some text.
cell.appendChild(new Paragraph(doc));
cell.getFirstParagraph()
.appendChild(new Run(doc, "Row 1, Cell 1 Text"));
// Add the cell to the row.
row.appendChild(cell);
row.appendChild(cell.deepClone(false));
row.getLastCell().appendChild(new Paragraph(doc));
row.getLastCell().getFirstParagraph()
.appendChild(new Run(doc, "Row 1, Cell 2 Text"));
row.appendChild(cell.deepClone(false));
row.getLastCell().appendChild(new Paragraph(doc));
row.getLastCell().getFirstParagraph()
.appendChild(new Run(doc, "Row 1, Cell 3 Text"));
// Add left indent to the table.
table.setLeftIndent(60);
doc.save("E:/Data/Test1.docx");

Related

Splitting rows of Nested Table OR Inner Table using iTextPDF

How do I split the rows of the inner table. Problem description according to the image.
Here, I have table named PARENT_TABLE that has cell that contains two tables named HEADER_TABLE and CONTENT_TABLE
CONTENT_TABLE has a cell that contains INNER_TABLE. INNER_TABLE contains the dynamic rows/content whose height will be changed according to content.
So, If I make this table using one page it's working perfectly. But incase if I need to split the table in different pages it shows the error like image_2. Please help me out. As a result: The table should look like in the image_1 even if the page split into two or more pages.
Please checkout mentioned image to be more clear.
image_1
Attempt to read from field 'float com.itextpdf.text.pdf.ColumnText.maxY' on a null object reference
image_2
I just found out the solution. The issue occurs only in the case when trying to split the PDFPTable into two pages. I had used tableCell.setRowspan(2), so when trying to split the table above issue occurs.
SOLVED IT: I just made an empty cell in place of using tableCell.setRowspan(2).
private PdfPTable contentWithDateTitleAndDescription(String stringData1, String stringData2, String stringData3, String stringData4) throws IOException, BadElementException {
float relativeWidth[] = {2, 7};
PdfPTable table = new PdfPTable(relativeWidth);
table.setWidthPercentage(100);
PdfPCell cellA = new PdfPCell();
cellA.setBorder(Rectangle.NO_BORDER);
// dateCell.setRowspan(2);
cellA.setPaddingLeft(15);
cellA.setVerticalAlignment(Element.ALIGN_CENTER);
cellA.setPaddingTop(7);
Paragraph dateParagraph = new Paragraph(stringData1);
cellA.addElement(dateParagraph);
PdfPCell emptyCellB = new PdfPCell();
emptyCellB.setBorder(Rectangle.NO_BORDER);
PdfPCell cellC = new PdfPCell();
cellC.setBorder(Rectangle.NO_BORDER);
cellC.setVerticalAlignment(Element.ALIGN_LEFT);
Paragraph titleParagraph = new Paragraph(stringData2 + " / " + stringData3);
cellC.addElement(titleParagraph);
PdfPCell cellD = new PdfPCell();
cellD.setBorder(Rectangle.NO_BORDER);
cellD.setVerticalAlignment(Element.ALIGN_LEFT);
Paragraph descriptionParagraph = new Paragraph(stringData4);
cellD.addElement(descriptionParagraph);
cellD.setPaddingBottom(15);
table.addCell(cellA);
table.addCell(cellC);
table.addCell(emptyCellB);
table.addCell(cellD);
return table;
}

iText: Cell with Image doesn't apply Rowspan

I have a table (2 x 2).
If I add a Phrase as cell in the first cell with rowspan 2, it works. But if I use an Image as cell, rowspan never apply.
float[] rowwidths2 = {0.6f,0.4f};
PdfPTable felsoegyharmad = new PdfPTable(rowwidths2);
felsoegyharmad.setWidthPercentage(100);
PdfPCell kepcell = new PdfPCell(new Phrase("image place", fontAlap));
kepcell.setRowspan(2);
felsoegyharmad.addCell(kepcell);
felsoegyharmad.addCell(new Phrase("1", fontAlap));
felsoegyharmad.addCell(new Phrase("2", fontAlap));
Code above is works as advertise.
But this code under is never rowspanned:
PdfPCell kepcell = new PdfPCell();
kepcell.addElement(Image.getInstance(path));
kepcell.setRowspan(2);
felsoegyharmad.addCell(kepcell);
felsoegyharmad.addCell(new Phrase("1", fontAlap));
felsoegyharmad.addCell(new Phrase("2", fontAlap));
How can I put this image into the first column but in height as two rows?
In case you wonder why nobody is answering your question. That's simple: the problem you describe can not be reproduced. I have taken your code snippet, and I have created the following standalone example:
PdfPTable table = new PdfPTable(2);
PdfPCell imageCell = new PdfPCell();
imageCell.addElement(Image.getInstance(IMG));
imageCell.setRowspan(2);
table.addCell(imageCell);
table.addCell(new Phrase("1"));
table.addCell(new Phrase("2"));
For the full source code, see ImageRowspan
The resulting PDF looks like this:
As you can see, the table has two columns and two rows. The first cell (the one with the image) spans two rows. You can download cmp_image_rowspan.pdf from out git repository.

How to change font color of an existing table cell in Aspose Word in Java?

I have an existing table in Aspose word template, I want to change the font color of the text inside the cell. How can I achieve it?
CellCollection cellList = row.getCells()
def i = 0
Run run;
Cell cell = cellList.get(i)
I am new to Aspose Word.
Please use following code example to change the color of text inside table's cell. Hope this helps you.
//Load the document
Document doc = new Document(MyDir + "in.docx");
//Get the first table in the document
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
//Get the first cell of first row
Cell cell = (Cell)table.getRows().get(0).getCells().get(0);
//Change the color of text
for (Run run : (Iterable<Run>)cell.getChildNodes(NodeType.RUN, true))
{
run.getFont().setColor(Color.BLUE);
}
//Save the document
doc.save(MyDir + "Out.docx");
I work with Aspose as Developer evangelist.

iText - image breaks cell alignment [duplicate]

This question already has answers here:
Right aligning text in PdfPCell
(6 answers)
Closed 7 years ago.
I have a table where in the first row there are only images and in the second row there are only descriptions of the images. I handle this by creating a table with the size (columns) of the amount of images and then filling the cells with tables with size 1 (2 rows = 1st row the image, 2nd row the description). After setting cell alignment of the 1st row to center, the second row will not apply the align and the description stays on the left ... is this a bug?
Integer size = filepathArray.length;
PdfPTable pdfPTable = new PdfPTable(size);
for (int i = 0; i < size; i++) {
PdfPTable inner = new PdfPTable(1);
try {
PdfPCell image = new PdfPCell();
PdfPCell description = new PdfPCell();
PdfPCell cell = new PdfPCell();
image.setImage(Image.getInstance(getImageAsByteArray(filepathArray[i])));
image.setFixedHeight(32);
image.setBorder(Rectangle.NO_BORDER);
image.setHorizontalAlignment(Element.ALIGN_CENTER);
inner.addCell(image);
description.addElement(new Chunk(filepathArray[i], FontFactory.getFont("Arial", 8)));
description.setBorder(Rectangle.NO_BORDER);
description.setHorizontalAlignment(Element.ALIGN_CENTER);
inner.addCell(description);
cell = new PdfPCell();
cell.addElement(inner); // needed to actually remove the border from the cell which contains the inner table because tables have no setter for the border
cell.setBorder(Rectangle.NO_BORDER);
pdfPTable.addCell(cell);
} catch (Exception e) {
}
}
pdfPTable.setHorizontalAlignment(Element.ALIGN_LEFT);
Result: the image is centered, the text is not, no way, I've tried everything! Also addElement() removes all previously set alignments (table and cell elements, is this a bug?) so I have to set the alignment AFTER I added the content to the cell or table.
This is wrong:
PdfPCell description = new PdfPCell();
description.addElement(new Chunk(filepathArray[i], FontFactory.getFont("Arial", 8)));
description.setHorizontalAlignment(Element.ALIGN_CENTER);
It is wrong because you are mixing text mode:
PdfPCell description = new PdfPCell(new Phrase(filepathArray[i], FontFactory.getFont("Arial", 8)));
description.setHorizontalAlignment(Element.ALIGN_CENTER);
With composite mode:
PdfPCell description = new PdfPCell();
Paragraph p = new Paragraph(filepathArray[i], FontFactory.getFont("Arial", 8));
p.setAlignment(Element.ALIGN_CENTER);
description.addElement(p);
Seems like you have tried everything but using the approaches that are explained in the documentation ;-)

A new row in pdfptable

I am using iText library to print certain data in table format in a pdf file. I have 11 columns and can have multiple rows. After creating header for titles of each column, how do I create a new row in pdfptable so that I can print the real data on a separate row.
In the constructor of PdfPTable you specify the number of columns in a row.
PdfPTable table = new PdfPTable(4) // 4 Columns
PdfPCell cell;
cell = new PdfPCell( new Phrase("Cell with colspan of 4") ) ;
cell. setColspan(4) ; // an entire row
anotherCell = new PdfPCell( new Phrase("Cell with colspan of 4") );
anotherCell.setColspan(4); // a second row
As you can see, a new row is created when you reach the colspan in the current row.

Categories