Java to Word Document Table Color - java

Hi I can write a table into a word document as the code below... I wonder how do you add table cell background colour?
IDocument myDoc = new Document2004();
//myDoc.addEle(BreakLine.times(1).create());
Table tbl = new Table();
tbl.addTableEle(TableEle.TH, "Jira Number", "Description","asd","ee");
myDoc.addEle(tbl);

yes you can.
Try to instantiate a TableV2 rather than Table.
take a look at the example:
http://java2word.blogspot.com.au/2012/02/huge-ms-word-documents-with-java2word.html
you'll have to do something like this "withStyle().bgColor("00FFFF")" in your fluent interface code.
cheers
Leonardo Correa

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;
}

Using iText7 (Java) to add a table to an existing PDF and continue on additional pages

I am attempting to complete a project with almost identical requirements as those associated with this question asked in 2015.
The answer provided by Bruno was perfect, but related to iText5. I am relatively new to iText, and am desperately trying to get up-to-speed to complete a current project.
I need to populate the fields of a PDF document
I need to add a table below the populated section, and the table needs to span multiple pages thereafter
Can anyone assist with the translation of Bruno's answer from iText5 to iText7?
Thanks so much in advance for any/all assistance!
You should write something like that:
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
Document doc = new Document(pdfDoc);
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
Map<String, PdfFormField> fields = form.getFormFields();
fields.get("Name").setValue("Jeniffer");
fields.get("Company").setValue("iText's next customer");
fields.get("Country").setValue("No Man's Land");
form.flattenFields();
Table table = new Table(UnitValue.createPercentArray(new float[]{1, 15}));
table.addHeaderCell("#");
table.addHeaderCell("description");
for (int i = 1; i <= 150; i++) {
table.addCell(String.valueOf(i));
table.addCell("test " + i);
}
doc.setRenderer(new DocumentRenderer(doc) {
#Override
protected LayoutArea updateCurrentArea(LayoutResult overflowResult) {
LayoutArea area = super.updateCurrentArea(overflowResult);
if (area.getPageNumber() == 1) {
area.getBBox().decreaseHeight(266);
}
return area;
}
});
doc.add(table);
doc.close();
Probably the most interesting part is about extending DocumentRenderer. The instance of this class associated with document handles its layout and overrided method (updateCurrentArea), as the name stands for, updates area for layout.
What is important to mention: All iText5 SO answers are ported in iText7 and you can find them on iText's website: https://developers.itextpdf.com/content/itext-7-examples .

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.

Insert new row into LibreOffice writer table using UNO

I have recently tried to code a small java file which will insert a row into an already existing table in a .odt document. The table itself has 4 rows and 3 column, but I would like to implement a check which will expand that table if the content to be inserted is larger than 4. However, every time I try to get the table's rows, it returns a null pointer. I am not that familiar with UNO api, but as far as i read through the documentation, the class XColumnsAndRowRange should be used in this situation. My code is as follows:
XTextTablesSupplier xTablesSupplier = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, xTextDocument);
XNameAccess xNamedTables = xTablesSupplier.getTextTables();
try {
Object table = xNamedTables.getByName(tblName);
XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, table);
XCellRange xCellRange = (XCellRange) UnoRuntime.queryInterface(XCellRange.class, table);
if(flag){
XColumnRowRange xCollumnAndRowRange =(XColumnRowRange)
UnoRuntime.queryInterface(XColumnRowRange.class, xCellRange);
XTableRows rows = xCollumnAndRowRange.getRows();
System.out.println("Testing if this works");
rows.insertByIndex(4, size-4);
}
I am not sure if I am missing something here or if I should be using a different function.
As Lyrl suggested, this works:
XTableRows rows = xTable.getRows();
Apparently XColumnRowRange is only used for spreadsheets.
Note: With Basic or Python you would not have this problem, because those languages do not need queryInterface. The code would simply be:
table = tables.getByName(tblName)
rows = table.getRows()

How to hyphenate text?

I generate an PDF file with iText in Java.
My table columns have fixed widths and text, which is too long for one line is wrapped in the cell.
But hyphenation is not used. The word "Leistungsscheinziffer" is shown as:
Leistungssc //Break here
heinziffer
My code where I use hyphenation:
final PdfPTable table = new PdfPTable(sumCols);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
table.getDefaultCell().setPadding(4f);
table.setWidths(widthsCols);
table.setWidthPercentage(100);
table.setSpacingBefore(0);
table.setSpacingAfter(5);
final Phrase result = new Phrase(text, font);
result.setHyphenation(new HyphenationAuto("de", "DE", 2,2));
final PdfPCell cell = new PdfPCell(table.getDefaultCell());
cell.setPhrase(result);
table.addCell(cell);
...
Hyphen is activated and the following test results "Lei-stungs-schein-zif-fer
"
Hyphenator h = new Hyphenator("de", "DE", 2, 2);
Hyphenation s = h.hyphenate("Leistungsscheinziffer");
System.out.println(s);
Is there anything I forgot to set to the table that hyphen is working there?
Thanks for your help. If you need more information about my code, I will tell you.
First a remark that is irrelevant to the problem: you create a PdfPCell object, but you don't use it. You add the phrase to the table instead of using the cell object.
Now for your question: normally hyphenation is set on the Chunk level:
Chunk chunk = new Chunk("Leistungsscheinziffer");
chunk.setHyphenation(new HyphenationAuto("de", "DE", 2,2));
table.addCell(new Phrase(chunk));
If you want to set the hyphenation on the Phrase level, you can do so, but this will only work for all subsequent Chunks that are added. It won't work for the content that is already stored inside the Phrase. In other words: you need to create an empty Phrase object and then add one or more Chunk objects:
Phrase phrase = new Phrase();
phrase.setHyphenation(new HyphenationAuto("de", "DE", 2,2));
phrase.add(new Chunk("Leistungsscheinziffer"));
I've made an example based on your code (HyphenationExample); the word "Leistungsscheinziffer" is hyphenated in the resulting PDF: hyphenation_table.pdf.

Categories