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:
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.
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");