Hi, I am new with Aspose.Words and I am looking for sample code to remove text and “backspace” following text . Here’s what I’m trying to accomplished…
Question1
Answer1
Answer2
Answer3
I want to remove “Answer1” and move “Answer2”, “Answer3” up with the same line:
Question 1 Answer2 Answer3
Thank you!
Hi,
Thanks for your inquiry. I have attached sample input/output word documents for your reference. You can view the DOM structure of these documents by opening them with DocumentExplorer. Please see the following screen shot:
``
There are four Paragraph nodes with a single Run inside each of them and you can easily write code to append the Runs (‘Answer 2’ and ‘Answer 3’ text nodes) to the first Paragraph as seen in the out.docx. For more information, I would suggest you please read the following API pages.
https://reference.aspose.com/words/net/aspose.words/paragraph/
https://reference.aspose.com/words/net/aspose.words/run/
Please let me know if I can be of any further assistance.
Best Regards,
Hi. I have used “Run” and “Paragraph” to retrieve my purpose. But there are some thing wrong. I attached my source file I want to use Aspose.Words to modify, output file when using my code, and expected result I want
My code:
Dim doc As New Document("E:\in.doc")
' doc.Range.Replace(New Regex(ControlChar.ParagraphBreak), New LineBreakReplacer(), False)
Dim builder As New DocumentBuilder(doc)
Dim desiredtextchanged As String = "This is changed Text"
'Get run of keyword2
Dim keyword2 As Run = GetKeywordPosition(doc, "Content")
Dim blnDelete As [Boolean] = False
Dim Runs As Node() = doc.GetChildNodes(NodeType.Run, True).ToArray()
For Each run As Run In Runs
If run Is keyword2 Then
blnDelete = False
Dim dstStory As CompositeNode = run.ParentNode
Dim prevPara As Paragraph = DirectCast(dstStory.PreviousSibling, Aspose.Words.Paragraph)
Dim prevPrevPara As Paragraph = DirectCast(dstStory.NextSibling, Aspose.Words.Paragraph)
If (prevPara Is Nothing) OrElse (prevPrevPara Is Nothing) Then
Return
End If
For Each node As Node In prevPrevPara.ChildNodes
prevPara.AppendChild(node)
builder.MoveTo(node)
Next
'For i As Integer = 0 To prevPrevPara.ChildNodes.Count
' If prevPrevPara.ChildNodes.Item(i) IsNot Nothing Then
' prevPara.AppendChild(prevPrevPara.ChildNodes.Item(i))
' End If
'Next
'prevPrevPara.Remove()
run.Remove()
keyword2 = GetKeywordPosition(doc, "Content")
End If
Next
doc.Save("E:\Out.doc")
Private Function GetKeywordPosition(ByVal doc As Document, ByVal keyword As String) As Run
Dim Runs As Node() = doc.GetChildNodes(NodeType.Run, True).ToArray()
For Each run As Run In Runs
If run.Text.Trim().IndexOf(keyword) > -1 Then
Return run
End If
Next
Return Nothing
End Function
Please help me debug this code.
Thank you very much
Hi,
Thanks for your inquiry and sorry for the delayed response. I think, you can achieve this by using the following code snippet:
Dim doc As New Document("c:\test\in.doc")
Dim tab As Table = doc.FirstSection.Body.Tables(1)
For Each row As Row In tab.Rows
For Each cell As Cell In row.Cells
cell.FirstParagraph.NextSibling.Remove()
Dim runs As Node() = DirectCast(cell.FirstParagraph.NextSibling, Paragraph).GetChildNodes(NodeType.Run, True).ToArray()
For Each run As Run In runs
cell.FirstParagraph.AppendChild(run)
Next
cell.FirstParagraph.NextSibling.Remove()
Next
Next
doc.Save("c:\test\output.doc")
If we can help you with anything else, please feel free to ask.
Best Regards,
Thanks a lot. Your code snippet is very useful for me.