We’re using Aspose Words for Android. If I already have a fully configured Font object, how can I apply it to a call to write(). While I can find a getFont() method, I can’t find a setFont() method. The Font is captured from reading in a document, so I’ve no idea what’s in it and can’t rebuild it from scratch.
Document doc = new Document("copy font formatting.docx");
Run source = doc.getFirstSection().getBody().getFirstParagraph().getRuns().get(0);
Run destination = doc.getFirstSection().getBody().getLastParagraph().getRuns().get(0);
copyFormatting(source.getFont(), destination.getFont());
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentEnd();
builder.writeln();
builder.writeln("Another plain text");
copyFormatting(source.getFont(), builder.getFont());
builder.writeln("Should be formatted");
doc.save("awjava-20.11.docx");
public static void copyFormatting(Object source, Object dest) throws Exception {
if (source.getClass() != dest.getClass())
throw new Exception("All objects must be of the same type");
Method methodlist[] = source.getClass().getDeclaredMethods();
// Iterate through each property in the source object.
for (Method prop : methodlist) {
// Continue processing only if the method starts with ‘get’.
if (!prop.getName().startsWith("get"))
continue;
// Skip indexed access items. Skip setting the internals of a style as these should not be changed.
if (prop.getName() == "getItem" || prop.getName() == "getStyle" ||
prop.getName() == "getStyleName" || prop.getName() == "getStyleIdentifier")
continue;
Object value;
// Wrap this call as it can throw an exception. Skip if thrown
try {
// Get value by invoking getter method.
value = prop.invoke(source);
} catch (Exception e) {
continue;
}
// Get the corresponding setter method.
Method setter = null;
try {
setter = source.getClass().getDeclaredMethod(prop.getName().replace("get", "set"), prop.getReturnType());
} catch (Exception e) {
// do nothing if throws.
}
// Skip if value can not be retrieved.
if (value != null) {
// If this property returns a class which belongs to the
if (prop.getReturnType().getPackage() != null && prop.getReturnType().getPackage().getName().equals("com.aspose.words")) {
// Recurse into this class.
copyFormatting(prop.invoke(source), prop.invoke(dest));
} else if (setter != null) {
// If we can write to this property then copy the value across.
setter.invoke(dest, prop.invoke(source));
}
}
}
}
The copyFormatting is a standalone utility method and is not a part of Aspose.Words for Android via Java API. However, you will need to include the following package:
Please check the following code of copyFormatting method:
public static void copyFormatting(Object source, Object dest) throws Exception {
if (source.getClass() != dest.getClass())
throw new Exception("All objects must be of the same type");
Method methodlist[] = source.getClass().getDeclaredMethods();
// Iterate through each property in the source object.
for (Method prop : methodlist) {
// Continue processing only if the method starts with ‘get’.
if (!prop.getName().startsWith("get"))
continue;
// Skip indexed access items. Skip setting the internals of a style as these should not be changed.
if (prop.getName() == "getItem" || prop.getName() == "getStyle" ||
prop.getName() == "getStyleName" || prop.getName() == "getStyleIdentifier")
continue;
Object value;
// Wrap this call as it can throw an exception. Skip if thrown
try {
// Get value by invoking getter method.
value = prop.invoke(source);
} catch (Exception e) {
continue;
}
// Get the corresponding setter method.
Method setter = null;
try {
setter = source.getClass().getDeclaredMethod(prop.getName().replace("get", "set"), prop.getReturnType());
} catch (Exception e) {
// do nothing if throws.
}
// Skip if value can not be retrieved.
if (value != null) {
// If this property returns a class which belongs to the
if (prop.getReturnType().getPackage() != null && prop.getReturnType().getPackage().getName().equals("com.aspose.words")) {
// Recurse into this class.
copyFormatting(prop.invoke(source), prop.invoke(dest));
} else if (setter != null) {
// If we can write to this property then copy the value across.
setter.invoke(dest, prop.invoke(source));
}
}
}
}