Free Support Forum - aspose.com

How to insert rtf string after paragraph content without any break (line break- paragraph break- page break)

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

Hi,


Thanks for your inquiry. I think, in this case you can modify the code as follows:

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.GetChildNodes(NodeType.Any, True)

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

If importedNode.NodeType.Equals(NodeType.Run) Then

builder.CurrentParagraph.Runs.Add(importedNode)

End If

Next

End Sub


I hope, this helps.

Best regards,

Thank you for your reply.
I’m used your code, but every run in some source paragraph (rtf) become one paragraph

Source Paragraph:
<!–[if !mso]>

v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);}

<![endif]–>

Tìm số electron, số proton và số nơtron có trong các hạt sau:

a) ion nitrat ...

b) cation Fe3+ ..

c) phân tử NH3 ..

<!–[if gte mso 10]> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:"Times New Roman"; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;}

<![endif]–>

Hi Awais!
I’m resolved my problem by this code:
For Each srcSection As Section In rtfDoc.Sections
’ Loop through all block level nodes (paragraphs and tables) in the body of the section.
For Each srcNode As Node In srcSection.Body.GetChildNodes(NodeType.Any, True)
If srcNode.NodeType.Equals(NodeType.Paragraph) Then
For Each oChild As Node In TryCast(srcNode, Paragraph).GetChildNodes(NodeType.Any, True)
Dim newNode As Node = importer.ImportNode(oChild, True)
If newNode.NodeType.Equals(NodeType.Run) Then
With TryCast(newNode, Run).Font
.Bold = False
.Italic = False
.Name = “Times New Roman”
.Size = 12
.Color = Color.Black
End With
End If
builder.CurrentParagraph.ChildNodes.Add(newNode)
Next
builder.InsertBreak(BreakType.LineBreak)
End If

Next srcNode
Next srcSection

Thank you!

Hi,


Thanks for the additional information. It’s great you were able to find what you were looking for. Please let us know any time you have any further queries. We’re always glad to help you.

Best regards,

Hi!
Using this up code help me insert rtf string with many paragraph, but it can’t solve problem when rtf string contain Table. Can you show me the way to solve this problem? (Pls get the attached file - TableRTF example I want to insert)
Thank you very much.

Hi,


Thanks for your inquiry. I have attached the output document, i.e. produced by using the code from here on my side, here for your reference. Could you please make a screenshot that highlights the problematic areas and attach it here for our reference? We will investigate as to how you want your final output to be generated like and provide you more information.

Best regards,