Hello ,
I got issue while inserting a rtf into a word document.
Indeed, an extra paragraph break is created after my rtf but I don’t want to.
«_TEST&disp=value» is my rtf content
-Here is the word content :
«_TEST&disp=value» «_Number&disp=value»
-Here is the expected result :
testvalue numbervalue
-Here the current result :
testvalue
numbervalue
-here is the code used :
DocumentBuilder builder = new DocumentBuilder(e.Document);
builder.MoveToField(e.Field, true);
string rtfString = StringUtil.GetRtfFromRichText(e.FieldValue as string);
byte[] rtfBytes = Encoding.UTF8.GetBytes(rtfString);
//Create memorystream
using (MemoryStream rtfStream = new MemoryStream(rtfBytes))
{
//Create document from stream
Document rtfDoc = new Document(rtfStream, new LoadOptions() { LoadFormat = LoadFormat.Rtf });
//Insert rtf document into destination document
builder.InsertDocument(rtfDoc, ImportFormatMode.UseDestinationStyles);
if (builder.CurrentParagraph.Runs?.Count < 1)
{
builder.CurrentParagraph.Remove();
}
}
Thank you
@andyt.lisam
It seems that you are encountering an issue with an extra paragraph break being inserted after your RTF content when you insert it into a Word document. This can happen due to how paragraphs are handled in Aspose.Words.
To resolve this issue, you can check if the current paragraph after inserting the RTF document contains any runs (text elements). If it does not, you can remove the paragraph to avoid the extra space. Your current implementation already includes a check for this, but it may not be functioning as expected.
Here’s a refined version of your code that ensures the extra paragraph break is removed correctly:
DocumentBuilder builder = new DocumentBuilder(e.Document);
builder.MoveToField(e.Field, true);
string rtfString = StringUtil.GetRtfFromRichText(e.FieldValue as string);
byte[] rtfBytes = Encoding.UTF8.GetBytes(rtfString);
// Create a MemoryStream for the RTF content
using (MemoryStream rtfStream = new MemoryStream(rtfBytes))
{
// Create a document from the RTF stream
Document rtfDoc = new Document(rtfStream, new LoadOptions() { LoadFormat = LoadFormat.Rtf });
// Insert the RTF document into the destination document
builder.InsertDocument(rtfDoc, ImportFormatMode.UseDestinationStyles);
// Check if the current paragraph is empty and remove it if necessary
if (builder.CurrentParagraph.Runs.Count == 0)
{
builder.CurrentParagraph.Remove();
}
}
Make sure that the RTF content you are inserting does not inherently contain any paragraph breaks. If the RTF content itself has paragraph breaks, you may need to preprocess the RTF string to remove any unwanted breaks before inserting it.
This approach should help you achieve the expected result without the extra paragraph break. If you continue to experience issues, consider checking the RTF content for any hidden formatting that might be causing the extra space.
If you have any further questions or need additional assistance, feel free to ask!
@andyt.lisam Please try using DocumentBuilder.InsertDocumentInline instead of DocumentBuilder.InsertDocument
method. As a difference from InsertDocument
this method moves the content of the paragraph of the destination document, before which the source document is inserted, into the last paragraph of the inserted source document. Actually, this means that paragraph break of the last inserted paragraph is removed.