Hello there,
I’m trying to accomplish the following in the Document Builder:
- Open first file and save it into a variable
- Open second file and save it into a variable
- Open the main template file which contains some text and table of contents (first two pages of final document). It is a normal docx file - just called a template
- Clear out the file
- Insert the original template contents
- Go to the end of the opened file and insert contents of first file
- After that, insert the contents of second file
- Update the table of contents so it contains newly inserted sections
- Do some text replacement
- Add a paragraph at the end
- Save as .pdf
Here is the script I use for now:
builder.SetTmpFolder("DocBuilderTemp")
builder.OpenFile("firstfile.docx")
const oDocument = Api.GetDocument()
const json = oDocument.ToJSON(true, true)
GlobalVariable["firstfile"] = json
builder.CloseFile()
builder.OpenFile("secondfile.docx")
const oDocument = Api.GetDocument()
const json = oDocument.ToJSON(true, true)
GlobalVariable["secondfile"] = json
builder.CloseFile()
builder.OpenFile("template.docx")
const ofirstfile = Api.FromJSON(GlobalVariable["firstfile"])
const osecondfile = Api.FromJSON(GlobalVariable["secondfile"])
const oDocument = Api.GetDocument()
const oDocument2 = oDocument.GetContent()
oDocument.RemoveAllElements()
oDocument.InsertContent(oDocument2)
oDocument.InsertContent(ofirstfile)
oDocument.InsertContent(osecondfile)
oDocument.UpdateAllTOC()
builder.SaveFile("pdf", "result.pdf")
builder.CloseFile()
I’m struggling a bit with steps 4-8. The resulting file behaves strangely. It only inserts the one file’s contents and nothing else.
If I don’t do steps 4-5 and just insert the first file into template, it gets inserted at the beginning instead of at the end. I can’t insert any more files after that.
Should I be using some different workflow? Maybe other methods?