Free Support Forum - aspose.com

How to copy cell formats

Using Aspose Words for Java, how can I copy a CellFormat that I grab from one document (used as some kind of template) to aother one using its current DocumentBuilder?

For example, here’s what I need to do for the shading part, but there are all the other properties:

    void cloneCellFormat(DocumentBuilder builder, CellFormat cellFormat) {
        Shading shading = cellFormat.getShading();
    builder.getCellFormat().getShading().setBackgroundPatternColor(shading.getBackgroundPatternColor());
    builder.getCellFormat().getShading().setForegroundPatternColor(shading.getForegroundPatternColor());
    builder.getCellFormat().getShading().setTexture(shading.getTexture());
}

Thanks for your help
Yves

@yvesb,

Thanks for your inquiry. Please ZIP and attach your sample input Word document and expected Word document here for testing. Please create your expected Word document by using MS Word. We will then provide you code to achieve the same by using Aspose.Words for Java.

Hello Awais,

I would do that if there was a particular situation. This is a generic question, I wouldn’t even know what to zip to you.

Yves

@yvesb,

Please see these sample input/output Word documents (Copy-CellFormat-Info.zip (10.2 KB)) and try running the following code. Hope, this helps.

// Source document you want to copy CellFormat from
Document doc = new Document(MyDir + @"input.docx");
// Empty document to copy CellFormat info to
Document dstDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(dstDoc);
// Create a One Row One Cell Table from scratch
Table dstTable = builder.StartTable();
Cell targetCell = builder.InsertCell();
builder.Writeln("I'm a wonderful formatted cell.");
builder.EndRow();
builder.EndTable();
// Get Cell you want to copy CellFormat of
Cell srcCell = doc.FirstSection.Body.Tables[0].LastRow.LastCell;
// Import the Cell in destination document
Cell dstCell = (Cell) dstDoc.ImportNode(srcCell, false);
dstCell.EnsureMinimum();
builder.MoveTo(dstCell.FirstParagraph);
builder.Writeln("I'm a wonderful formatted cell.");
// Insert the cell at Row 1 Cell 2 position
targetCell.ParentNode.InsertAfter(dstCell, targetCell);
// Save document
dstDoc.Save(MyDir + @"18.4.docx");