We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Problem to insert document into other document

Hi,

I've got some issues adding a document within another document. I tried to use method InsertDocument and, in order to make it work, i created a new bookmark. I'd need to replace a specific tag with my document, unfortunately my document gets added on the paragraph above. Any ideas?

Ivan


This message was posted using Aspose.Live 2 Forum

Hi Ivan,

Thanks for your request. Actually, InsertDocument method inserts a document after the specified paragraph or table. That is why there is an additional paragraph break.

I think, you can try using code like provided here to insert a document in the same line: https://forum.aspose.com/t/114203

Hope this helps. Please let us know if you need more assistance, we will be glad to help you.

Best regards,

Thanks for the reply, I forget everytime to tell you that I’m writing in VB .net and now I’ve some issue to translate it. Is possible to see that source in vb .net code?

best regards

I read the documentation about Paragraph, it seem to be present in the aspose.pdf library. I’m using the aspose.word, I’m sorry, I had to tell you before

Hi Ivan,

Thanks for your request. Here is VB version of this method.

Private Sub InsertDocumentAtBookmark(ByVal bookmarkName As String, ByVal dstDoc As Document, ByVal srcDoc As Document)

Dim importer As New NodeImporter(srcDoc, dstDoc, ImportFormatMode.UseDestinationStyles)

' Create DocumentBuilder

Dim builder As New DocumentBuilder(dstDoc)

' Move cursor to bookmark and insert paragraph break

builder.MoveToBookmark(bookmarkName)

builder.Writeln()

' If current paragraph is a list item, we should clear its formating.

If (builder.CurrentParagraph.IsListItem) Then

builder.ListFormat.RemoveNumbers()

End If

' Content of srcdoc will be inserted after this node

Dim insertAfterNode As Node = builder.CurrentParagraph.PreviousSibling

'Content of first paragraph of srcDoc will be apended to this parafraph

Dim insertAfterParagraph As Paragraph = Nothing

If (insertAfterNode.NodeType = NodeType.Paragraph) Then

insertAfterParagraph = CType(insertAfterNode, Paragraph)

End If

' Content of last paragraph of srcDoc will be apended to this parafraph

Dim insertBeforeParagraph As Paragraph = builder.CurrentParagraph

' We will be inserting into the parent of the destination paragraph.

Dim dstStory As CompositeNode = insertAfterNode.ParentNode

' Remove empty paragraphs from the end of document

While (Not DirectCast(srcDoc.LastSection.Body.LastChild, CompositeNode).HasChildNodes)

srcDoc.LastSection.Body.LastParagraph.Remove()

If (srcDoc.LastSection.Body.LastChild Is Nothing) Then

Exit While

End If

End While

' Loop through all sections in the source document.

For Each srcSection As Section In srcDoc.Sections

'Loop through all block level nodes (paragraphs and tables) in the body of the section.

For nodeIdx As Integer = 0 To srcSection.Body.ChildNodes.Count - 1

Dim srcNode As Node = srcSection.Body.ChildNodes(nodeIdx)

' Do not insert node if it is a last empty paragarph in the section.

Dim para As Paragraph = Nothing

If (srcNode.NodeType = NodeType.Paragraph) Then

para = CType(srcNode, Paragraph)

End If

If ((Not para Is Nothing) AndAlso para.IsEndOfSection AndAlso (Not para.HasChildNodes)) Then

Exit For

End If

' If current paragraph is first paragraph of srcDoc

' then appent its content to insertAfterParagraph

Dim nodeInserted As Boolean = False

If ((Not para Is Nothing) AndAlso para.Equals(srcDoc.FirstSection.Body.FirstChild)) Then

nodeInserted = True ' set this flag to know that we already processed this node.

For i As Integer = 0 To para.ChildNodes.Count - 1

Dim node As Node = para.ChildNodes(i)

Dim dstNode As Node = importer.ImportNode(node, True)

insertAfterParagraph.AppendChild(dstNode)

Next

' If subdocument contains only one paragraph

' then copy content of insertBeforeParagraph to insertAfterParagraph

' and remove insertBeforeParagraph

If (srcDoc.FirstSection.Body.FirstParagraph.Equals(srcDoc.LastSection.Body.LastChild)) Then

While (insertBeforeParagraph.HasChildNodes)

insertAfterParagraph.AppendChild(insertBeforeParagraph.FirstChild)

End While

insertBeforeParagraph.Remove()

End If

End If

' If current paragraph is last paragraph of srcDoc

' then appent its content to insertBeforeParagraph

If ((Not para Is Nothing) AndAlso para.Equals(srcDoc.LastSection.Body.LastChild)) Then

nodeInserted = True ' set this flag to know that we already processed this node.

Dim previouseNode As Node = Nothing

For i As Integer = 0 To para.ChildNodes.Count - 1

Dim node As Node = para.ChildNodes(i)

Dim dstNode As Node = importer.ImportNode(node, True)

If (previouseNode Is Nothing) Then

insertBeforeParagraph.InsertBefore(dstNode, insertBeforeParagraph.FirstChild)

Else

insertBeforeParagraph.InsertAfter(dstNode, previouseNode)

End If

previouseNode = dstNode

Next

End If

If (Not nodeInserted) Then

' This creates a clone of the node, suitable for insertion into the destination document.

Dim newNode As Node = importer.ImportNode(srcNode, True)

' Insert new node after the reference node.

dstStory.InsertAfter(newNode, insertAfterNode)

insertAfterNode = newNode

End If

Next

Next

End Sub

Hope this helps.

Best regards,

Great!!! It is what I was looking for!

Thank you very much Alexey.

Ps: I will explane another very hard issue in other discussion

Hi,

I found another issue while i was adding a document into another document. If the document I have to insert has got a table inside, the program throws InvalidCastException: "Impossible to make a cast from an ‘aspose.words.tables.table’ object to ‘aspose.words.paragraph’."

I attach the file I used to test the method.

Hi

Thanks for your request. I cannot reproduce the problem on my side. Could you please attach both source and destination documents and specify name of bookmark where the source document should be inserted? I will check the issue one more time and provide you more information.

Best regards,

Hi,

This is the destination document (I attached the source document in the previous post).
The bookmark’s name is “bookmark” (created in the IReplacingCallback_Replacing method), I think the problem is that source document begins with a table, is that possible?

I’ll wait your help.

Ivan


Hi Ivan,

Thank you for additional information. But I am afraid, I still cannot reproduce the problem on my side. Here the code I used for testing:

_

Public Sub Test001()

Dim dst As New Document("C:\Temp\TemplateWord.doc")

Dim src As New Document("C:\Temp\output.doc")

InsertDocumentAtBookmark("bookmark", dst, src)

dst.Save("C:\Temp\out.doc")

End Sub

Could you please create a simple application that will demonstrate the issue?

Best regards,

Hi,

this is my code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
prova.sostituisci(“salva”)
End Sub
______

inside prova there is

Public Function sostituisci(ByVal azione As String) As Boolean
Dim doc As New Document(“C:\TemplateWord.doc”)
'Dim doc As New Document(“C:\FI1230.doc”)
Dim builder As New DocumentBuilder(doc)

If (azione = “conta”) Then
MsgBox(“La parola cercata compare " & doc.Range.Replace(New Regex(Me.PTag1), Me.PTag1) & " volte”)
Exit Function
End If
If Me.PTag1 <> “” Then
doc.Range.Replace(New Regex("#" & Me.PTag1 & “#”), Me.PTag1New)
End If
If Me.PTag2 <> “” Then
doc.Range.Replace(New Regex("#" & Me.PTag2 & “#”), New ReplaceEvaluatorFindAndInsertObject(Me.PTag2New, Me.PUrl, “link”), False)
End If
If Me.PTag3 <> “” Then
If (Me.POggetto.Substring(Me.POggetto.Length() - 3, 3) = “jpg”) Then
doc.Range.Replace(New Regex("#" & Me.PTag3 & “#”), New ReplaceEvaluatorFindAndInsertObject(Me.POggetto, “image”), False)
ElseIf (Me.POggetto.Substring(Me.POggetto.Length() - 3, 3) = “doc”) Then
Dim srcDoc As New Document(“C:\output.doc”)
doc.Range.Replace(New Regex("#" & Me.PTag3 & “#”), New ReplaceEvaluatorFindAndInsertObject(doc, srcDoc, “document”), False)
End If
End If
If (azione = “salva”) Then
salva(doc, “C:”)
ElseIf (azione = “converti”) Then
converti(doc, “C:”)
End If
End Function
_______________________________

Private Function IReplacingCallback_Replacing(ByVal e As ReplacingArgs) As ReplaceAction Implements IReplacingCallback.Replacing

'This is a Run node that contains either the beginning or the complete match.
Dim currentNode As Node = e.MatchNode

'The first (and may be the only) run can contain text before the match,
'in this case it is necessary to split the run.
If (e.MatchOffset > 0) Then
currentNode = SplitRun(CType(currentNode, Run), e.MatchOffset)
End If

'This array is used to store all nodes of the match for further removing.
Dim runs As New ArrayList()

'Find all runs that contain parts of the match string.
Dim remainingLength As Integer = e.Match.Value.Length
While ((remainingLength > 0) AndAlso (currentNode IsNot Nothing) AndAlso (currentNode.GetText().Length <= remainingLength))
runs.Add(currentNode)
remainingLength = remainingLength - currentNode.GetText().Length

'Select the next Run node.
'Have to loop because there could be other nodes such as BookmarkStart etc.
Do While ((currentNode IsNot Nothing) AndAlso (currentNode.NodeType <> NodeType.Run))
currentNode = currentNode.NextSibling
Loop

End While
'Split the last run that contains the match if there is any text left.

If ((currentNode IsNot Nothing) AndAlso (remainingLength > 0)) Then
SplitRun(CType(currentNode, Run), remainingLength)
runs.Add(currentNode)
End If

'Create Document Buidler aond insert the object
Dim builder As New DocumentBuilder(e.MatchNode.Document)
builder.MoveTo(CType(runs(runs.Count - 1), Run))
If mType = “link” Then 'if the object is an Hyperlink
'builder.MoveTo(CType(runs(runs.Count - 1), Run))
builder.Font.StyleIdentifier = StyleIdentifier.Hyperlink
If (mUrl.Substring(0, 4) <> “http”) Then
builder.InsertHyperlink(mText, “http://” & mUrl, False)
Else
builder.InsertHyperlink(mText, mUrl, False)
End If
ElseIf mType = “image” Then 'if the object is an image
builder.InsertImage(mPath)
ElseIf mType = “document” Then 'if the object is a document to append
'Dim bookmark As Bookmark = mDoc.Range.Bookmarks(“bookmark”) '“insertionPlace”
builder.StartBookmark(“bookmark”)
builder.EndBookmark(“bookmark”)
InsertDocumentAtBookamrk(“bookmark”, mDoc, mSrcDoc)
End If

'Now remove all runs in the sequence.
For Each run As Run In runs
run.Remove()
Next
'Signal to the replace engine to do nothing because we have already done all what we wanted.
Return ReplaceAction.Skip

End Function
---------------
Private Shared Function SplitRun(ByVal run As Run, ByVal position As Integer) As Run
Dim afterRun As Run = CType(run.Clone(True), Run)
afterRun.Text = run.Text.Substring(position)
run.Text = run.Text.Substring(0, position)
run.ParentNode.InsertAfter(afterRun, run)
Return afterRun

End Function
-------------
Sub InsertDocumentAtBookamrk(ByVal bookmarkName As String, ByVal dstDoc As Document, ByVal srcDoc As Document)
'Create DocumentBuilder
Dim builder As DocumentBuilder = New DocumentBuilder(dstDoc)
'Move cursor to bookmark and insert paragraph break
builder.MoveToBookmark(bookmarkName)
builder.Writeln()

'Content of srcdoc will be inserted after this node
Dim insertAfterNode As Node = builder.CurrentParagraph.PreviousSibling

'Content of first paragraph of srcDoc will be apended to this parafraph
Dim insertAfterParagraph As Aspose.Words.Paragraph = CType(insertAfterNode, Aspose.Words.Paragraph)

'Content of last paragraph of srcDoc will be apended to this parafraph
Dim insertBeforeParagraph As Aspose.Words.Paragraph = builder.CurrentParagraph
'We will be inserting into the parent of the destination paragraph.
Dim dstStory As CompositeNode = insertAfterNode.ParentNode

'Remove empty paragraphs from the end of document
While (Not srcDoc.LastSection.Body.LastParagraph.HasChildNodes)
srcDoc.LastSection.Body.LastParagraph.Remove()
End While
'Loop through all sections in the source document.
Dim srcSection As Aspose.Words.Section
For Each srcSection In srcDoc.Sections
'Loop through all block level nodes (paragraphs and tables) in the body of the section.
Dim srcNode As Node
For Each srcNode In srcSection.Body
'Do not insert node if it is a last empty paragarph in the section.
Try
Dim para As Aspose.Words.Paragraph = CType(srcNode, Aspose.Words.Paragraph)
If (Not para Is Nothing) AndAlso para.IsEndOfSection AndAlso (Not para.HasChildNodes) Then
Exit For
End If
'If current paragraph is first paragraph of srcDoc
'then appent its content to insertAfterParagraph
If (para.Equals(srcDoc.FirstSection.Body.FirstParagraph)) Then
Dim node As Node
For Each node In para.ChildNodes
Dim dstNode As Node = dstDoc.ImportNode(node, True, ImportFormatMode.KeepSourceFormatting)
insertAfterParagraph.AppendChild(dstNode)
Next
'If subdocument contains only one paragraph
'then copy content of insertBeforeParagraph to insertAfterParagraph
'and remove insertBeforeParagraph
If (srcDoc.FirstSection.Body.FirstParagraph.Equals(srcDoc.LastSection.Body.LastParagraph)) Then
While (insertBeforeParagraph.HasChildNodes)
insertAfterParagraph.AppendChild(insertBeforeParagraph.FirstChild)
End While
insertBeforeParagraph.Remove()
End If
'If current paragraph is last paragraph of srcDoc
'then appent its content to insertBeforeParagraph
ElseIf (para.Equals(srcDoc.LastSection.Body.LastParagraph)) Then
Dim previouseNode As Node
Dim node As Node
For Each node In para.ChildNodes
Dim dstNode As Node = dstDoc.ImportNode(node, True, ImportFormatMode.KeepSourceFormatting)
If (previouseNode Is Nothing) Then
insertBeforeParagraph.InsertBefore(dstNode, insertBeforeParagraph.FirstChild)
Else
insertBeforeParagraph.InsertAfter(dstNode, previouseNode)
End If
previouseNode = dstNode
Next
Else
'This creates a clone of the node, suitable for insertion into the destination document.
Dim newNode As Node = dstDoc.ImportNode(srcNode, True, ImportFormatMode.KeepSourceFormatting)
'Insert new node after the reference node.
dstStory.InsertAfter(newNode, insertAfterNode)
insertAfterNode = newNode
End If
Catch e As Exception
MsgBox(e.Message)
End Try
Next
Next

End Sub



If i forgot anything you need I’ll add here


Hi

Thank you for additional information. Please try using this modified version of InsertDocumentAtBookmark method:

Private Sub InsertDocumentAtBookmark(ByVal bookmarkName As String, ByVal dstDoc As Document, ByVal srcDoc As Document)

Dim importer As New NodeImporter(srcDoc, dstDoc, ImportFormatMode.UseDestinationStyles)

' Create DocumentBuilder

Dim builder As New DocumentBuilder(dstDoc)

' Move cursor to bookmark and insert paragraph break

builder.MoveToBookmark(bookmarkName)

builder.Writeln()

' If current paragraph is a list item, we should clear its formating.

If (builder.CurrentParagraph.IsListItem) Then

builder.ListFormat.RemoveNumbers()

End If

' Content of srcdoc will be inserted after this node

Dim insertAfterNode As Node = builder.CurrentParagraph.PreviousSibling

'Content of first paragraph of srcDoc will be apended to this parafraph

Dim insertAfterParagraph As Paragraph = Nothing

If (insertAfterNode.NodeType = NodeType.Paragraph) Then

insertAfterParagraph = CType(insertAfterNode, Paragraph)

End If

' Content of last paragraph of srcDoc will be apended to this parafraph

Dim insertBeforeParagraph As Paragraph = builder.CurrentParagraph

' We will be inserting into the parent of the destination paragraph.

Dim dstStory As CompositeNode = insertAfterNode.ParentNode

' Remove empty paragraphs from the end of document

While (Not DirectCast(srcDoc.LastSection.Body.LastChild, CompositeNode).HasChildNodes)

srcDoc.LastSection.Body.LastParagraph.Remove()

If (srcDoc.LastSection.Body.LastChild Is Nothing) Then

Exit While

End If

End While

' Loop through all sections in the source document.

For Each srcSection As Section In srcDoc.Sections

'Loop through all block level nodes (paragraphs and tables) in the body of the section.

For nodeIdx As Integer = 0 To srcSection.Body.ChildNodes.Count - 1

Dim srcNode As Node = srcSection.Body.ChildNodes(nodeIdx)

' Do not insert node if it is a last empty paragarph in the section.

Dim para As Paragraph = Nothing

If (srcNode.NodeType = NodeType.Paragraph) Then

para = CType(srcNode, Paragraph)

End If

If ((Not para Is Nothing) AndAlso para.IsEndOfSection AndAlso (Not para.HasChildNodes)) Then

Exit For

End If

' If current paragraph is first paragraph of srcDoc

' then appent its content to insertAfterParagraph

Dim nodeInserted As Boolean = False

If ((Not para Is Nothing) AndAlso para.Equals(srcDoc.FirstSection.Body.FirstChild)) Then

nodeInserted = True ' set this flag to know that we already processed this node.

For i As Integer = 0 To para.ChildNodes.Count - 1

Dim node As Node = para.ChildNodes(i)

Dim dstNode As Node = importer.ImportNode(node, True)

insertAfterParagraph.AppendChild(dstNode)

Next

' If subdocument contains only one paragraph

' then copy content of insertBeforeParagraph to insertAfterParagraph

' and remove insertBeforeParagraph

If (srcDoc.FirstSection.Body.FirstParagraph.Equals(srcDoc.LastSection.Body.LastChild)) Then

While (insertBeforeParagraph.HasChildNodes)

insertAfterParagraph.AppendChild(insertBeforeParagraph.FirstChild)

End While

insertBeforeParagraph.Remove()

End If

End If

' If current paragraph is last paragraph of srcDoc

' then appent its content to insertBeforeParagraph

If ((Not para Is Nothing) AndAlso para.Equals(srcDoc.LastSection.Body.LastChild)) Then

nodeInserted = True ' set this flag to know that we already processed this node.

Dim previouseNode As Node = Nothing

For i As Integer = 0 To para.ChildNodes.Count - 1

Dim node As Node = para.ChildNodes(i)

Dim dstNode As Node = importer.ImportNode(node, True)

If (previouseNode Is Nothing) Then

insertBeforeParagraph.InsertBefore(dstNode, insertBeforeParagraph.FirstChild)

Else

insertBeforeParagraph.InsertAfter(dstNode, previouseNode)

End If

previouseNode = dstNode

Next

End If

If (Not nodeInserted) Then

' This creates a clone of the node, suitable for insertion into the destination document.

Dim newNode As Node = importer.ImportNode(srcNode, True)

' Insert new node after the reference node.

dstStory.InsertAfter(newNode, insertAfterNode)

insertAfterNode = newNode

End If

Next

Next

End Sub

Please let me know if it works for you.

Best regards,

Hi Alexis,

it works. What was the problem?

Ivan

Hi Ivan,

Thanks for your request. You were right, the problem was caused by the table. I used this modified version of the method for testing and that is why could not reproduce the problem. I apologize for inconvenience.

Best regards,

The issues you have found earlier (filed as WORDSNET-5251) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(19)