zwei
August 2, 2023, 8:00am
1
For example
DocumentProperty prop = doc.getCustomDocumentProperties().get("ABTEILUNG_NAME");
Now I need to change the background color of this “prop”, only this prop’s background color should be changed, all others content of the Word file stay the same back groundcolor.
How can I make it?
@zwei It looks like it is required to highlight DOCPROPERTY field in the document. You can achieve this using the following code:
Document doc = new Document("C:\\Temp\\in.docx");
// highlight ABTEILUNG_NAME doc property fields.
for (Field f : doc.getRange().getFields())
{
if (f.getType() == FieldType.FIELD_DOC_PROPERTY)
{
FieldDocProperty prop = (FieldDocProperty)f;
if (prop.getFieldCode().contains("ABTEILUNG_NAME"))
{
Inline currentNode = prop.getStart();
while (currentNode != prop.getEnd())
{
currentNode.getFont().setHighlightColor(Color.YELLOW);
currentNode = (Inline)currentNode.getNextSibling();
}
}
}
}
doc.save("C:\\Temp\\out.docx");
1 Like
zwei
August 2, 2023, 8:49am
3
Thank you very much!
What if we need to change the color of ALL CustomDocumentProperties? Here we only change the back ground color of ALL CustomDocumentProperties, NO BuiltInDocumentProperties.
1 Like
@zwei , could you please attach a sample document? It will help us understand your requirements.
zwei
August 7, 2023, 8:18am
5
Here it is an exmaple of our test file.
We need to change “Nachname”, “Vorname”, “Abteilung” three fields, which are all Custom Document Properties.
ForcontDocPropertiesTest.docx (23,9 KB)
Here you may find the definition of Custom Document Properties, in this test, we only change these three fields.
image.png (14,5 KB)
Fettgedruckter Text
@zwei You could use the following code:
Document doc = new Document("C:\\Temp\\in.docx");
for (Field f : doc.getRange().getFields())
{
if (f.getType() == FieldType.FIELD_DOC_PROPERTY)
{
FieldDocProperty prop = (FieldDocProperty)f;
if (prop.getFieldCode().contains("ABTEILUNG_NAME")
|| prop.getFieldCode().contains("PERSON_VORNAME")
|| prop.getFieldCode().contains("PERSON_NACHNAME"))
{
Node currentNode = prop.getStart();
while (currentNode != prop.getEnd())
{
if(currentNode instanceof Inline)
((Inline)currentNode).getFont().setHighlightColor(Color.YELLOW);
currentNode = currentNode.getNextSibling();
}
}
}
}
doc.save("C:\\Temp\\out.docx");
1 Like