Help with combining documents while keeping base headers/footers in Words Cloud (Node.js)

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.

Sure, I will check the code you shared.

Even if my current code is not the best way to achieve the goal, I have also tried other methods from the Document API:

and from the HeaderFooter API:

but I have not been able to get a 100% correct result.

One workaround I found to avoid the blank first page was to split the resulting document after the append operation, and then append all pages except the first one. However, this causes incorrect page numbering (the first page ends up starting at page number 2).

I would really appreciate your help with this.

May I ask you to share sample documents for testing purposes?

Of course, I’m sharing these sample documents with you. The “content” document only contains text and images for testing purposes. This file is uploaded by users, so I don’t have control over its formatting or internal structure (content). I only have control over the files that I use as the “base.” Thanks.
content.docx (301,8 KB)

base.docx (190,6 KB)

Thanks for sharing, I will reach you after analysis.

1 Like

Hi, any updates? Thanks!

I’m trying to find the most efficient way to achieve what you want. I’ll share my results tomorrow.

1 Like

I really appreciate your help.

Hi, I wanted to check in and see if you’ve had a chance to review this. Thank you.

I’m afraid I couldn’t achieve it yet with the public API that is available right now, I’m testing your case inside our codebase to check if can achieve it in general and provide some new option to SDK to do what you expect

@Fortekna
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSCLOUD-3293

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Hi, regarding the paid support, if I were to purchase it, would it ensure that the functionality I require is available?

Paid support is a priority basis for your analysis of the issue. Your issue already is in analysis, once it is finished I could share more details and ETA if it is possible