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

Free Support Forum - aspose.com

Merging multiple documents into one blank document and inserting new bookmarks

Hi,

I am merging multiple documents into one empty one by first opening a new blank document and then appending existing documents into it.

This merge process works fine.

However, I am also setting bookmarks unique for each appended document, at the begining and end of each document, so that I can break off the merge documents into their original parts down-stream in the process. The problem is that I do not see the Bookmarks withing the merged document after I open it in MS Word and go to Insert | Bookmark tab, that is there is absolutely no bookmarks saved.

My code sample is as follows:

’ Create a blank document.
Dim doc As Aspose.Words.Document = New Aspose.Words.Document()
'Remove empty section in the beginning of every blank document.
doc.FirstSection.Remove()

Dim builder As DocumentBuilder = New DocumentBuilder(doc)
builder.MoveToDocumentEnd()
builder.Document.LastSection.Remove()

’ Save document as the merge (Doc) file.
doc.Save(MergeDirectory & “” & fNameMergeDoc, SaveFormat.Doc)

builder.StartBookmark("\StartOfDoc")

Do While anotherfileexists

docbuilder.StartBookmark(getbName)
Dim nextDoc As Aspose.Words.Document = New Aspose.Words.Document(reportPath) // Get a new doc
AppendDoc(doc, nextDoc)
docbuilder.EndBookmark(getbName)

Loop

builder.EndBookmark("\EndOfDoc")

doc.Save(MergeDirectory & “” & fNameMergeDoc, SaveFormat.Doc)

Function getbname()
…Get a uniques Bookmark
End function

Public Sub AppendDoc(ByVal dstDoc As Aspose.Words.Document, ByVal srcDoc As Aspose.Words.Document)
’ Loop through all sections in the source document.
’ Section nodes are immediate children of the Document node so we can just enumerate the Document.

For Each srcSection As Aspose.Words.Section In srcDoc
’ Because we are copying a section from one document to another,
’ it is required to import the Section node into the destination document.
’ This adjusts any document-specific references to styles, lists, etc.

’ Importing a node creates a copy of the original node, but the copy
’ is ready to be inserted into the destination document.
Dim dstSection As Node = dstDoc.ImportNode(srcSection, True, ImportFormatMode.KeepSourceFormatting)

’ Now the new section node can be appended to the destination document.

dstDoc.AppendChild(dstSection)

Next srcSection

End Sub

Hi

Thanks for your inquiry. Please try using the following code:

Sub Main()

Dim lic As License = New License()

lic.SetLicense(“Aspose.Words.lic”)

'Define list of sub documents

Dim subDocs As String() = {“C:\Temp\in1.doc”, “C:\Temp\in2.doc”, “C:\Temp\in3.doc”}

’ Create a blank document.

Dim doc As Aspose.Words.Document = New Aspose.Words.Document()

Dim builder As DocumentBuilder = New DocumentBuilder(doc)

'Remove empty section in the beginning of every blank document.

doc.FirstSection.Remove()

Dim isFirst As Boolean = True

For Each reportPath As String In subDocs

'Get unique name of bookmark

Dim name As String = getbname()

'Append Document

Dim nextDoc As Aspose.Words.Document = New Aspose.Words.Document(reportPath) ’ Get a new doc

AppendDoc(doc, nextDoc)

If (isFirst) Then

builder.MoveToDocumentStart()

builder.StartBookmark(“StartOfDoc”)

builder.EndBookmark(“StartOfDoc”)

isFirst = False

End If

'insert bookamrk start

builder.StartBookmark(name)

'move to document end and insert bookmark end

builder.MoveToDocumentEnd()

builder.EndBookmark(name)

Next

'insert bookmark end

builder.StartBookmark(“EndOfDoc”)

builder.EndBookmark(“EndOfDoc”)

doc.Save(“C:\Temp\out.doc”)

End Sub

Function getbname() As String

'…Get a uniques Bookmark

Dim name As Guid = Guid.NewGuid()

Return name.ToString()

End Function

I think this should help you.

Best regards.

Thank you Alexey for your response.

It is now working much better and I am getting the Bookmarks in the merged document. However, they are not quite in the right place. Another problem that I have is that I am still getting a blank page at the beginning of the merged document (none of the individual documents have any blank page in front of them). It seems that the code to remove the first Section does not work properly.

So I thought that may be the next step is to remove this blank page first and then concentrate on putting the “[” sign for the bookmark at the Title of the next document (Shown in Red [ ). Right now it sits before the page break at the end of last document, like the following:

abcdefgh … this is the last line of the previous document … ijklmnop…

[

------------------------------------ Page Break --------------------------------

HEADER AND FOOTER SECTION

**[**Title of the Next Document

abcd gfjfhjf i jkm;nmgo hhd hjjd jhjhjh

Hi

Thanks for your inquiry. Could you please check the following lines in your code.

’ Create a blank document.

Dim doc As Aspose.Words.Document = New Aspose.Words.Document()

Dim builder As DocumentBuilder = New DocumentBuilder(doc)

'Remove empty section in the beginning of every blank document.

doc.FirstSection.Remove()

Please make sure you have the same in your code.

Best regards.

Thanks for your reply.

Actually I had it a little bit in different order like this:

’ Create a blank document.

Dim doc As Aspose.Words.Document = New Aspose.Words.Document()

'Remove empty section in the beginning of every blank document.

doc.FirstSection.Remove()

Dim builder As DocumentBuilder = New DocumentBuilder(doc)

However, even with the order of lines as you provided, still I get a blank page at the beginning of the merged file. Is there any other criteria that need be set?

Hi

Thanks for your inquiry. This code works fine on my side. Could you please attach your output document?

Best regards.

I did the same and created a Sub in my program with your exact code and ran it with my input files and still got the blank page. Then could we say that our environments are different as the same code produce different results. Could you please email me your input files and I’ll run it on my machine and send you back the output file if still getting a blank page. Thanks.

Hi

Thanks for your inquiry. The documents are pretty simple. Please see the attached files. Also try set SectionStart.Continuous as shown in the following code:

'Define list of sub documents

Dim subDocs As String() = {“C:\Temp\in1.doc”, “C:\Temp\in2.doc”, “C:\Temp\in3.doc”}

’ Create a blank document.

Dim doc As Aspose.Words.Document = New Aspose.Words.Document()

Dim builder As DocumentBuilder = New DocumentBuilder(doc)

'Remove empty section in the beginning of every blank document.

doc.FirstSection.Remove()

Dim isFirst As Boolean = True

For Each reportPath As String In subDocs

'Get unique name of bookmark

Dim name As String = getbname()

'Append Document

Dim nextDoc As Aspose.Words.Document = New Aspose.Words.Document(reportPath) ’ Get a new doc

If (isFirst) Then

nextDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous

End If

AppendDoc(doc, nextDoc)

If (isFirst) Then

builder.MoveToDocumentStart()

builder.StartBookmark(“StartOfDoc”)

builder.EndBookmark(“StartOfDoc”)

isFirst = False

End If

'insert bookamrk start

builder.StartBookmark(name)

'move to document end and insert bookmark end

builder.MoveToDocumentEnd()

builder.EndBookmark(name)

Next

'insert bookmark end

builder.StartBookmark(“EndOfDoc”)

builder.EndBookmark(“EndOfDoc”)

doc.Save(“C:\Temp\out.doc”)

Hope this helps.

Also please attach your documents for testing.

Best regards.