Hello Aspose team, I trying to update the header content of an word documents using the aspose java api.
public class WaterMark {
public static void main(String[] args) throws Exception {
Document doc = new Document("E:\\Project\\Sprint 19\\Test_Aspose_Doc\\PageNumbering\\Dummy\\123.docx");
Section section1 = doc.getSections().get(0);
HeaderFooter header = section1.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
String strHeader = header.getText();
System.out.println("Header text is : \n"+header.getText());
String[] sList = strHeader.split("\\|");
StringBuilder sb = new StringBuilder();
for(String s : sList) {
sb.append(s.trim()+" | ");
}
DocumentBuilder builder = new DocumentBuilder(doc);
PageSetup pageSetup = section1.getPageSetup();
pageSetup.setDifferentFirstPageHeaderFooter(false);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
sb.append("Hello! This is updated header text");
builder.write(sb.toString());
doc.save("E:\\Project\\Sprint 19\\Test_Aspose_Doc\\PageNumbering\\ModifiedHeaderFooter.docx");
}
}
I am able to fetch the content of content of header, but while I am trying to update it, content goes somewhere else.
Please guide.
I have attached the sample input file.
123.docx (104.4 KB)
Input file.ModifiedHeaderFooter.docx (92.9 KB)
Output file.
@ketanpbh using your code, you are actually getting the expected result. Can you please attach an example of your expected output, you can build it using MS Word app or similar?
Hello @eduardo.canal
I missed my expected output. Here I am uploding the output file.
My output - When I try to update the content of vertical box text which is also the part of header. Instead of updating its existing vertical box content it goes on the top of document which is wrong.
What I want - I want to update the content of the vertical box (refer image for more clarity) which is the part of the header document.
Expected output should be like this:
Expected_Output.docx (104.1 KB)
1 Like
@ketanpbh you can use the following code to get the expected output:
Document doc = new Document("C:\\Temp\\input.docx");
Section section1 = doc.getFirstSection();
HeaderFooter header = section1.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
Shape shape = (Shape)header.getChildNodes(NodeType.SHAPE, 0, true);
if(shape != null) {
List<Node> runs = shape.getChildNodes(NodeType.RUN, true).stream()
.filter(r -> !((Run)r).getText().isEmpty())
.collect(Collectors.toList());
if(!runs.isEmpty()) {
Run run = (Run)runs.get(runs.size()-1);
run.setText(run.getText() + "| Hello! This is updated header text.");
for (Paragraph p : shape.getChildNodes(NodeType.PARAGRAPH, true).<Paragraph>toList()) {
for (Run r : p.getRuns()) {
r.getFont().setName("Arial");
r.getFont().setSize(10.0);
}
}
shape.getTextBox().setFitShapeToText(true);
shape.setLeft(shape.getLeft() - 10);
}
}
doc.save("C:\\Temp\\output.docx");
output.docx (97.0 KB)
1 Like
Hello @eduardo.canal. Thanks for your code.
But while I am using the above code provided by you is facing the compilation issue. I am using eclipse ide.
I am working with word file. Want to modify the text which is present inside vertical shape(Part of header). I want to update the text present inside the shape on all pages.
Please find the attached file for you reference.
SampleFile - V1.10.3 - Copy.docx (217.6 KB)
@ketanpbh Please try using the following code:
Document doc = new Document("C:\\Temp\\in.docx");
for (Section s : doc.getSections())
{
HeaderFooter hf = s.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
if (hf != null)
{
// get shape.
Shape shape = (Shape)hf.getChild(NodeType.SHAPE, 0, true);
if (shape != null)
{
// Reset old text
NodeCollection paragraphs = shape.getChildNodes(NodeType.PARAGRAPH, true);
while (paragraphs.getCount() > 1)
paragraphs.get(paragraphs.getCount() - 1).remove();
Paragraph p = (Paragraph)paragraphs.get(0);
for (Run r : p.getRuns())
{
r.setText("");
}
// Remove SDT from the shape.
for (StructuredDocumentTag sdt : (Iterable<StructuredDocumentTag>)shape.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true))
sdt.removeSelfOnly();
// Set new text
p.getRuns().get(0).setText("This is new text");
}
}
}
doc.save("C:\\Temp\\out.docx");