Hello,
I have been trying different ways to achieve the following using Aspose Words Cloud (Node.js backend):
I have a “base” .docx file which contains one page with header and footer , and a “content” file with user-provided content, which also has its own header and footer.
I need to generate a single document that combines these files so that the resulting document keeps the headers and footers from the “base” file while incorporating the content from the “content” file .
I have tried the functions provided in the SDK but I cannot achieve the expected result. I hope you can help me with this.
Here is my current function. It uploads both files to storage, deletes the headers and footers from the content file, and appends the content to the base. However, the resulting document shows the first page with header and footer but no content , and the content only starts on page 2:
public async mergeDocsWithAspose (
baseDocBuffer: Buffer,
contentDocBuffer: Buffer,
): Promise {
const {
WordsApi,
DocumentEntry,
DocumentEntryList,
AppendDocumentRequest,
UploadFileRequest,
DeleteHeadersFootersRequest,
UpdateSectionPageSetupRequest,
UpdateFieldsRequest,
DownloadFileRequest,
FileReference,
PageSetup
} = this.loadAspose();
const wordsApi = new WordsApi(myApiKeys);
const timestamp = Date.now();
const baseFileName = `Temp/base_${timestamp}.docx`;
const contentFileName = `Temp/content_${timestamp}.docx`;
await wordsApi.uploadFile(
new UploadFileRequest({
fileContent: baseDocBuffer,
path: baseFileName,
})
);
await wordsApi.uploadFile(
new UploadFileRequest({
fileContent: contentDocBuffer,
path: contentFileName,
})
);
for (let i = 0; i < 10; i++) {
try {
await wordsApi.deleteParagraph({
name: baseFileName,
sectionIndex: 0,
paragraphIndex: 0,
folder: "Temp"
});
} catch {
break;
}
}
await wordsApi.deleteHeadersFooters(
new DeleteHeadersFootersRequest({
name: `content_${timestamp}.docx`,
sectionPath: "",
folder: "Temp"
})
);
const contentFileRef = FileReference.fromRemoteFilePath(contentFileName);
const entryList = new DocumentEntryList({
documentEntries: [
new DocumentEntry({
fileReference: contentFileRef,
importFormatMode: "KeepSourceFormatting"
})
],
});
await wordsApi.appendDocument(
new AppendDocumentRequest({
name: `base_${timestamp}.docx`,
documentList: entryList,
folder: "Temp",
})
);
await wordsApi.deleteSection({
name: baseFileName,
sectionIndex: 0,
folder: "Temp"
});
const pageSetup = new PageSetup({
restartPageNumbering: true,
pageStartingNumber: 1,
sectionStart: "Continuous"
});
await wordsApi.updateSectionPageSetup(
new UpdateSectionPageSetupRequest({
name: `base_${timestamp}.docx`,
sectionIndex: 0,
folder: "Temp",
pageSetup: pageSetup
})
);
await wordsApi.updateFields(
new UpdateFieldsRequest({
name: `base_${timestamp}.docx`,
folder: "Temp"
})
);
const finalDoc = await wordsApi.downloadFile(
new DownloadFileRequest({
path: baseFileName
})
);
return finalDoc.body;
}
I would greatly appreciate any guidance on how to properly achieve this using Aspose Words Cloud with Node.js.
Thank you very much for your support.