@tahir.manzoor: I’ve implemented the same functionality in my application.
But I’m facing one issue in which if my document already contains the image in the header and if I use PageSetup.DifferentFirstPageHeaderFooter = true
then it is removing the existing image from first page header.
any solution for this issue?
To ensure a timely and accurate response, please attach the following resources here for testing:
- Your input Word document.
- Please attach the output Word file that shows the undesired behavior.
- Please attach the expected output Word file that shows the desired behavior.
- Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.
As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.
PS: To attach these resources, please zip and upload them.
Hello @tahir.manzoor:
I’ve attached zip file with input-output file. In which you can check that the header image is available in the input.docx and the same is removed from output.pdf while applying the background. input-output.zip (114.1 KB)
My expected result is it should keep both (Header if exist + Background).
Also find following code snippet which we are using for the implementation.
val builder = DocumentBuilder(document)
document.firstSection.pageSetup.differentFirstPageHeaderFooter = true
builder.moveToHeaderFooter(HEADER_FIRST)
val background = builder.insertImage("ImageData")
background.wrapType = NONE
background.behindText = true
background.relativeHorizontalPosition = RelativeHorizontalPosition.PAGE
background.relativeVerticalPosition = RelativeVerticalPosition.PAGE
background.left = (builder.pageSetup.pageWidth - background.width) / 2
background.top = (builder.pageSetup.pageHeight - background.height) / 2
Let me know if any other information required.
You are inserting image in Header First
of document and setting PageSetup.DifferentFirstPageHeaderFooter property to true. So, the image in the primary header is not visible on first page of document and newly inserted image is visible.
Please move the cursor to the end of document and insert page break as shown below. Open the output document and check the second page. You can see the original image on second page’s header.
builder.moveToDocumentEnd();
builder.insertBreak(BreakType.PAGE_BREAK);
But I want header image as it is in the first page with watermark image
is this possible @tahir.manzoor ?
Please do not use this property to get the desired output. Could you please share why are you inserting image into document’s header?
Please ZIP and attach your expected output document here for our reference. We will then provide you more information about your query.
files.zip (445.1 KB)
In document’s header the image is already there as you can see in input.docx.
I want to insert watermark (background) image in the document with the following condition
- The first page of the document should contain first.png as the watermark (background)
- The remaining pages of the document should contains remaining.png as the watermark (background)
You can check the unexpected-output.pdf in which watermark images are as it is as expected. but the header image is removed from the first page.
I want to keep the header image as it is in the first page want to also apply watermarks. check expected.pdf
If I’ll not use
document.firstSection.pageSetup.differentFirstPageHeaderFooter = true
then I’ll not be able to set different watermark (background) in first page and remaining pages. right?
Let me know if still any other information required
In your document, the required header image is in primary header. So, you need to copy this image into Header First
. Please use following code example to get the desired output.
Document doc = new Document(MyDir + "input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
doc.getFirstSection().getPageSetup().setDifferentFirstPageHeaderFooter(true);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_FIRST);
Shape background = builder.insertImage(MyDir + "first.png");
background.setWrapType(WrapType.NONE);
background.setBehindText(true);
background.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
background.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
background.setLeft((builder.getPageSetup().getPageWidth() - background.getWidth()) / 2);
background.setTop((builder.getPageSetup().getPageHeight() - background.getHeight()) / 2);
CopyHeaderFooter(doc);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
Shape remaining = builder.insertImage(MyDir + "remaining.png");
remaining.setWrapType(WrapType.NONE);
remaining.setBehindText(true);
remaining.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
remaining.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
remaining.setLeft((builder.getPageSetup().getPageWidth() - remaining.getWidth()) / 2);
remaining.setTop((builder.getPageSetup().getPageHeight() - remaining.getHeight()) / 2);
doc.save(MyDir + "21.5.pdf");
public static void CopyHeaderFooter(Document srcDoc)
{
for (Node node : (Iterable<Node>)srcDoc.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY).getChildNodes())
{
if(node != null)
{
Node dstNode = node.deepClone(true);
srcDoc.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_FIRST).appendChild(dstNode);
}
}
}
Thanks @tahir.manzoor
Now I’m able to see header with the watermark in the first page.
But, I’ve checked with other input documents (document with header + footer text)
In that, I’ve found that the footer is not visible in the first page of the output document.
Please check the attached files.zip (174.1 KB)
and let me know how can I enable footer also in the first page.
Please copy the nodes of footer primary in footer first using the same approach shared in my previous post.
public static void CopyHeaderFooter(Document srcDoc)
{
for (Node node : (Iterable<Node>)srcDoc.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY).getChildNodes())
{
if(node != null)
{
Node dstNode = node.deepClone(true);
srcDoc.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_FIRST).appendChild(dstNode);
}
}
for (Node node : (Iterable<Node>)srcDoc.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY).getChildNodes())
{
if(node != null)
{
Node dstNode = node.deepClone(true);
srcDoc.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_FIRST).appendChild(dstNode);
}
}
}
We suggest you pleas read the following article.
Thanks @tahir.manzoor,
The article you’ve provided helps me a lot to resolve many small issues after applying watermarks in the document in which the headers and footers are already there.
Still I’m facing one issue in which, the header of the second page is not with proper space (check top spacing) compared to first page. Please find attached files for the current and expected output. files.zip (377.7 KB)
To apply distance I’ve tried
builder.pageSetup.headerDistance = 20.0
before CopyHeaderFooter function call.
Please let me know if there are any solution to keep header space in sync in all the pages
Thanks again for the support you have provided till now.
Please do not use this property for this issue. You are facing this issue because watermark and image in primary header are child nodes of separate paragraphs. You need to add both images in same paragraph to avoid this issue.
Please call CopyHeaderFooter method after importing the document as shown below to get the desired output.
Document doc = new Document(MyDir + "input.docx");
CopyHeaderFooter(doc);
Thanks
moving CopyHeaderFooter(doc)
function immediate after document resolved the issue