Hello, I have a requirement to update an existing RTF document with new content in such a fashion that the old content of the document will be inside a box and the new content is at the beginning of the document. Is there a way to build this?
Example output (when viewing in rtf viewer):
New Content Goes here
— — — — — — — — — —
| |
| Old Content Goes here |
| |
— — — — — — — — — —
I’m able to insert the new content at the beginning but that content is going to the table cell instead of the existing content. How do I achieve what I intended?
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentStart();
builder.insertBreak(BreakType.PARAGRAPH_BREAK);
builder.insertCell();
builder.write("New Content goes here");
doc.save(updatedRtfFile);
@rohithkodakandla Could you please attach your input and expected output documents here for our reference? We will check the documents and provide you more information.
Hello @alexey.noskov, I’m unable to upload the rtf file. It lets me choose .rtf or .txt files. I have uploaded screenshot of the original rtf file.
@rohithkodakandla You can zip original and expected RTF documents and attach ZIP achieve here for our reference. Unfortunately, screenshots are not very convenient for analysis.
rtfedits.zip (4.1 KB)
Hello @alexey.noskov I have uploaded the zip file with the two rtf documents. Original and the expected file after update.
@rohithkodakandla You can use the following code to achieve what you need:
Document doc = new Document("C:\\Temp\\in.rtf");
// Create document builder and insert table with one cell.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln();
Table table = builder.startTable();
Cell cell = builder.insertCell();
builder.endRow();
builder.endTable();
// Move the content after the table into the created cell.
while (table.getNextSibling() != null)
cell.appendChild(table.getNextSibling());
// Move to document beginning and insert new content.
builder.moveToDocumentStart();
builder.write("This is new Content");
doc.save("C:\\Temp\\out.rtf");