Hi,
I am facing one issue and need help in understanding if I am doing something wrong.
Requirement : Add a logo/image to the footer of each page of the document. (A document can have multiple section breaks)
Problem/Retro Steps :
-
Upload a document which has multiple section breaks and user-defined footers on each page.
-
My code will parse the document and add image/logo on each page of the document.
-
Unfortunately, the code is somehow deleting the user-defined footers from some of the pages of the document.
Questions :
What is wrong with the current code?
My code is adding logo/image perfectly fine on each page of the document. But somehow, it is removing user-defined footers from some pages of the document (when document has multiple section breaks).
An Example Document for reference : Document With Multiple Section breaks
Code :
//wordBytes --> bytes of the word document uploaded.
ByteArrayInputStream wordDocInputStream = new ByteArrayInputStream(wordBytes);
Document wordDoc = new Document(wordDocInputStream); //Converting the word bytes to com.aspose.word.Document
for (Section section : wordDoc.getSections())
{
HeaderFooterCollection headerfooters = section.getHeadersFooters();
if (headerfooters.getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY) == null)
{
headerfooters.add(new HeaderFooter(wordDoc, HeaderFooterType.FOOTER_PRIMARY));
}
PageSetup pageSetup = section.getPageSetup();
if (pageSetup != null)
{
if (pageSetup.getDifferentFirstPageHeaderFooter()
&& headerfooters.getByHeaderFooterType(HeaderFooterType.FOOTER_FIRST) == null)
{
headerfooters.add(new HeaderFooter(wordDoc, HeaderFooterType.FOOTER_FIRST));
}
if (pageSetup.getOddAndEvenPagesHeaderFooter()
&& headerfooters.getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN) == null)
{
headerfooters.add(new HeaderFooter(wordDoc, HeaderFooterType.FOOTER_EVEN));
}
}
for (HeaderFooter headerFooter : headerfooters)
{
int type = headerFooter.getHeaderFooterType();
if (type == HeaderFooterType.FOOTER_PRIMARY
|| type == HeaderFooterType.FOOTER_FIRST
|| type == HeaderFooterType.FOOTER_EVEN)
{
Shape image = new Shape(wordDoc, ShapeType.IMAGE);
image.setWrapType(WrapType.SQUARE);
image.setHorizontalAlignment(HorizontalAlignment.CENTER);
image.getImageData().setImage(new BufferedInputStream(new FileInputStream(pathToImage)));
// Append a new center-aligned paragraph to each footer present ...
Paragraph paragraph = new Paragraph(wordDoc);
paragraph.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
paragraph.appendChild(image);
headerFooter.appendChild(paragraph);
}
}
}