Change Font of a Ref Field in a table

Hi Support Team,
DocTable.zip (42.8 KB)

I am sitting on a small challenge which I can’t get fixed.

Problem is the following:
I am reading a word file, getting the table and add rows to it which works almost very good already.
But in the last column I am adding a Ref Field which should point to a bookmark.
Works also very good already. But the Font layout (size and bold) is taken from the Bookmark itself.
I want to change the font and size in the table now but it seems like it does not work.

MS Word is also taking the font from the bookmark, but i can still change it afterwords.

Any ideas how to solve it?

Thats the source:

Document doc = new Document("G:\\Test\\Docx\\DocTable\\Table.docx");

	Table table = doc.getFirstSection().getBody().getTables().get(0);

	Row newRow = (Row) table.getLastRow().deepClone(true);
	for (int j = 0; j < newRow.getCells().getCount(); j++) {

		for (Cell cell : newRow.getCells()) {
			cell.removeAllChildren();
			cell.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);
			cell.appendChild(new Paragraph(doc));
			switch (j) {
			case 0:
				cell.getFirstParagraph().appendChild(new Run(doc, "Column 1"));
				break;
			case 1:
				cell.getFirstParagraph().appendChild(new Run(doc, "Column 2"));
				break;
			case 2:
				Run run = new Run(doc, "Column 3");
				Font f = run.getFont();
				f.setColor(new Color(114, 21, 208));
				cell.getFirstParagraph().appendChild(run);

				break;
			case 3:
				System.out.println("###" + cell.getFirstParagraph().getParagraphBreakFont().getSize());
				cell.getFirstParagraph().getParagraphBreakFont().setSize(10);
				cell.getFirstParagraph().insertField("Ref Book \\h ", null, true);

				break;

			}

			j++;

		}

	}

	table.appendChild(newRow);

	doc.save("G:\\Test\\Docx\\DocTable\\docx2.docx");

@kai20511,

You can fix the ‘error.docx’ by using the following code:

Document doc = new Document("D:\\DocTable\\error.docx");

Table tab = doc.FirstSection.Body.Tables[0];
Cell lastCell = tab.LastRow.LastCell;
foreach(Run run in lastCell.GetChildNodes(NodeType.Run, true))
{
    run.Font.ClearFormatting();
    run.Font.Size = 10;
    run.Font.Name = "Arial";
    run.Font.Bold = false;
}

doc.Save("D:\\DocTable\\18.10.docx");

@awais.hafeez
I tried to migrate this code to Java (seems like its c#) but Font does not exist as Method with Run.
Can you please share more on how to do it with Java?

Another question, can I call the “clear formatting” direclty after adding the ref field?
Or do I need to loop through the document before its stored?

Thank you.

@awais.hafeez
Many thanks, your hint was pointing me to the right direction!

I can directly use the following code now in my switch case and it works!

Many thanks.

NodeCollection run = table.getLastRow().getLastCell().getChildNodes(NodeType.RUN, true);
for (Run r : run) {
Font font = r.getFont();
font.clearFormatting();
font.setSize(10);
font.setBold(false);
}

@kai20511,

It is great you were able to find what you were looking for. Please let us know any time you have any further queries.

Yes, the previous code was in C#. The same code in Java is as follows:

Document doc = new Document("D:\\DocTable\\error.docx");

Table tab = doc.getFirstSection().getBody().getTables().get(0);
Cell lastCell = tab.getLastRow().getLastCell();
for(Run run : (Iterable<Run>) lastCell.getChildNodes(NodeType.RUN, true))
{
    run.getFont().clearFormatting();
    run.getFont().setSize(10);
    run.getFont().setName("Arial");
    run.getFont().setBold(false);
}

doc.save("D:\\DocTable\\awjava-18.10.docx");