Hi team,
I’m generating a pdf from a word template file using aspose java and appending the another file.
The template file is having the header and now the resultant file is having the header in all the pages.
I do not want the header file from the appended file.
Is it possible to remove the header from appended file?
NOTE: i’m appending multiple files and i want to remove the header from all those files. is it possible?
@Rakesh_M Could you please attach the main file along with at least one of the files that you are appending? This will help us in understanding the context and reproducing the issue effectively.
Sure.
In the out put pdf file, i do not want the header from appended files.(TestFile1, TestFile2,TestFile3)
Please find the attachment for the source code and template input and out put files.
removeheader.zip (79.0 KB)
1 Like
@Rakesh_M please use the following code to get the expected output:
Document doc = new Document("C:\\Temp\\input.docx");
var sectionsAdded = 0;
for (var i = 1; i < 4; i++)
{
var temp = new Document("C:\\Temp\\TestFile"+i+".docx");
temp.getFirstSection().getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
sectionsAdded += temp.getSections().getCount();
doc.appendDocument(temp, ImportFormatMode.KEEP_SOURCE_FORMATTING);
}
doc.updatePageLayout();
DocumentBuilder builder = new DocumentBuilder(doc);
var indexStart = doc.getSections().getCount() - 1;
var indexEnds = doc.getSections().getCount() - sectionsAdded;
for (var index = indexStart; index >= indexEnds; index--)
{
var sec = doc.getSections().get(index);
sec.getPageSetup().setDifferentFirstPageHeaderFooter(false);
sec.getPageSetup().setOddAndEvenPagesHeaderFooter(false);
HeaderFooter primary = sec.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
HeaderFooter first = sec.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_FIRST);
HeaderFooter even = sec.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_EVEN);
if (first != null)
{
first.removeAllChildren();
first.isLinkedToPrevious(false);
}
if (even != null)
{
even.removeAllChildren();
even.isLinkedToPrevious(false);
}
if (primary == null)
{
//If the primary HeaderFooter is null I need to create one to be able to set the parameters.
builder.moveToSection(index);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.insertParagraph();
primary = sec.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
}
primary.removeAllChildren();
primary.isLinkedToPrevious(false);
}
doc.save("C:\\Temp\\output.docx");
output.docx (15.4 KB)