Hi Aspose Teams!
I’m using Aspose.Word to insert rtf string after paragraph like this
Expected Result:
Q1(1 m)This is RTF Content
Q2(1 m)This is RTF Content
Current Result:
Q1(1 m)
This is RTF Content
Q2(1 m)
This is RTF Content
My code:
-------------------------------------------------------------------------------------------------------------
Dim docContent As New Document()
Dim builder As New DocumentBuilder(docContent)
Dim sQuestionContentRtf As String = "{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}}
{*\generator Msftedit 5.41.21.2509;}\viewkind4\uc1\pard\f0\fs24 This is RTF Content\par
}"
For i As Integer = 0 To 10
Select Case i Mod 2 = 0
Case 1
builder.InsertParagraph()
builder.Write("Q1(1 m)")
InsertRtfDescription(builder, sQuestionContentRtf, False, False)
builder.InsertBreak(BreakType.ParagraphBreak)
builder.MoveToDocumentEnd()
Case 0
builder.InsertParagraph()
builder.Write("Q2(1 m)")
InsertRtfDescription(builder, drQuestion.QuestionContentRtf.Trim, False, False)
builder.MoveToDocumentEnd()
'Write table
Dim borders As BorderCollection = builder.CellFormat.Borders
borders.LineStyle = LineStyle.Single
borders.LineWidth = 1
borders.Color = System.Drawing.Color.White
Dim table As Table = builder.StartTable()
For j As Integer = 1 To 4
builder.InsertCell()
builder.Write(j.ToString + ".")
InsertRtfDescription(builder, sQuestionContentRtf , False, False)
Next
builder.EndRow()
’ We have finished inserting all the data from the DataTable, we can end the table.
builder.EndTable()
builder.MoveToDocumentEnd()
End Select
Next
Public Sub InsertRtfDescription(ByVal builder As DocumentBuilder, ByVal rtfString As String, ByVal isBold As Boolean, ByVal isItalic As Boolean)
Dim rtfBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(rtfString)
'Create memorystream
Dim rtfStream As New MemoryStream(rtfBytes)
'Create document from stream
Dim rtfDoc As New Document(rtfStream)
Dim currentNode As Node = builder.CurrentParagraph
For Each srcNode As Node In rtfDoc.FirstSection.Body
Dim importedNode As Node = builder.Document.ImportNode(srcNode, True, ImportFormatMode.KeepSourceFormatting)
If importedNode.IsComposite Then
'Get collection of runs from current node
Dim runs As NodeCollection = TryCast(importedNode, CompositeNode).GetChildNodes(NodeType.Run, True)
'Loop through all runs in the current node and change font
For Each run As Run In runs
run.Font.Bold = isBold
run.Font.Italic = isItalic
Next
End If
If currentNode.GetText.Contains(ControlChar.PageBreak) Then
'I want to move PageBreak before insert importedNode
End If
currentNode.ParentNode.InsertAfter(importedNode, currentNode)
Next
End Sub
------------------------------------------------------------------------------------------------------
Expect your recommends.
Best Regard!
tqnga