Problem adding image in the header of word document - java

I'm adding a picture in the header of a word document. It shows a frame for the image and says "the image cannot currently be display". If I add text to the header it show the text, and if I add the image in the document body, it also shows the image. So is getting the image and it show text on the header, but no the image.
I'm running out of checkings, can anyone advise with this please?
Thank you!
public static void createHeaderAndFotter(XWPFDocument document) throws IOException, BadElementException, InvalidFormatException {
XWPFHeaderFooterPolicy headerFooterPolicy = document.getHeaderFooterPolicy();
if (headerFooterPolicy == null) headerFooterPolicy = document.createHeaderFooterPolicy();
File image = new ClassPathResource("/static/images/NIAB_Header.bmp").getFile();
BufferedImage bimg1 = ImageIO.read(image);
int width = bimg1.getWidth();
int height = bimg1.getHeight();
String imageName= image.getName();
XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
XWPFParagraph paragraph = header.createParagraph();
// XWPFParagraph paragraph = document.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run = paragraph.createRun();
run.addPicture(new FileInputStream(image), XWPFDocument.PICTURE_TYPE_PNG, imageName, Units.toEMU(width), Units.toEMU(height));
run.setText("HEADER");
}
If I remove the commment on this line and comment the one before, then it adds the image
XWPFParagraph paragraph = document.createParagraph();

I believe whether this works or not highly depends on apache poiversion used. There was multiple issues with pictures in header/footer in former apache poi versions.
The following is the most minimal working code using apache poi 4.0.1. It is recommend always using the latest stable version.:
Code:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.apache.poi.util.Units;
public class CreateWordHeaderWithImage {
public static void main(String[] args) throws Exception {
XWPFDocument doc = new XWPFDocument();
// the body content
XWPFParagraph paragraph = doc.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("The Body...");
// create header
XWPFHeader header = doc.createHeader(HeaderFooterType.DEFAULT);
// header's first paragraph
paragraph = header.getParagraphArray(0);
if (paragraph == null) paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
run = paragraph.createRun();
FileInputStream in = new FileInputStream("samplePict.jpeg");
run.addPicture(in, Document.PICTURE_TYPE_JPEG, "samplePict.jpeg", Units.toEMU(100), Units.toEMU(50));
in.close();
run.setText("HEADER");
FileOutputStream out = new FileOutputStream("CreateWordHeaderWithImage.docx");
doc.write(out);
doc.close();
out.close();
}
}
Result:

Related

How to add title page image in apache poi with header image in it. Currently, whenever I add image in header, title page image disappears

Here I am trying to insert image in the title page in apache poi but with the header image, it is not displaying the image on the title page. If I remove the addPicture from the header part, then, title page correctly displays the proper image. I am not sure what's the issue. I have added the code here.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.apache.poi.util.Units;
import org.apache.poi.wp.usermodel.HeaderFooterType;
public class Check {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
sectPr.addNewTitlePg();
File image = new File("D:\\Img.png");
FileInputStream imageData = new FileInputStream(image);
run = paragraph.createRun();
int imageType = XWPFDocument.PICTURE_TYPE_PNG;
String imageFileName = image.getName();
int width = 612;
int height = 550;
run.addPicture(imageData, imageType, imageFileName, Units.toEMU(width), Units.toEMU(height));
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("The Body... first page");
paragraph = document.createParagraph();
run = paragraph.createRun();
run.addBreak(BreakType.PAGE);
run.setText("The Body... second page");
XWPFHeader header = document.createHeader(HeaderFooterType.FIRST);
paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
run = paragraph.createRun();
run.setText("First page footer...");
// adding header here
header = document.createHeader(HeaderFooterType.DEFAULT);
paragraph = header.getParagraphArray(0);
if (paragraph == null)
paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.RIGHT);
image = new File("D:\\Picture.png");
imageData = new FileInputStream(image);
run = paragraph.createRun();
imageType = XWPFDocument.PICTURE_TYPE_PNG;
imageFileName = image.getName();
width = 100;
height = 50;
// adding image here.
run.addPicture(imageData, imageType, imageFileName, Units.toEMU(width), Units.toEMU(height));
// header image part completed
FileOutputStream out = new FileOutputStream("D:\\CreateWordFooters.docx");
document.write(out);
out.close();
document.close();
}
}

apache poi disable default footer for the first page

I am trying to create a word document in which I will have no footer only in the first page and a footer for the rest of the pages. I wrote the following code (I also tried to change -reverse- the order of creation of footer and footerFirst objects) but that did not help. I still have the default footer on all pages.
How should I disable the footer from the first page? Thanks in advance.
private XWPFDocument initDocument(String FILE) throws Exception{
XWPFDocument document = new XWPFDocument();
XWPFHeaderFooterPolicy headerFooterPolicy = document.getHeaderFooterPolicy();
if (headerFooterPolicy == null) headerFooterPolicy = document.createHeaderFooterPolicy();
// create header start
XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
//XWPFParagraph paragraph = footer.createParagraph();
XWPFParagraph paragraph = footer.getParagraphArray(0);
if (paragraph == null)
paragraph = footer.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run = paragraph.createRun();
run.setFontSize(11);
run.setFontFamily("Times New Roman");
run.setText("Some company info in the footer");
XWPFFooter footerFirst = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.FIRST);
paragraph = footerFirst.getParagraphArray(0);
if (paragraph == null)
paragraph = footerFirst.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
run = paragraph.createRun();
run.setText(" ");
return document;
}
That there is a different header set for first page only, means not that this header will also be shown. In Word GUI there is a checkbox [x] Different First Page in Header & Footer Tools to achieve that.
And according Office Open XML Part 4 - Markup Language Reference there must a boolean XML element titlePg be set to determine that there is a title page present.
In old apache poi versions using XWPFHeaderFooterPolicy this XML element titlePg can only be set using underlying low level objects using document.getDocument().getBody().getSectPr().addNewTitlePg();.
But using current apache poi versions (since 3.16) there is no need using XWPFHeaderFooterPolicy directly. Now there is XWPFDocument.createHeader and XWPFDocument.createFooter using a HeaderFooterType. This sets titlePg flag in XML when HeaderFooterType.FIRST is used.
Complete example which sets and uses HeaderFooterType.FIRST and HeaderFooterType.DEFAULT:
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.wp.usermodel.HeaderFooterType;
public class CreateWordFooters {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
// the body content
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("The Body... first page");
paragraph = document.createParagraph();
run=paragraph.createRun();
run.addBreak(BreakType.PAGE);
run.setText("The Body... second page");
// create first page footer
XWPFFooter footer = document.createFooter(HeaderFooterType.FIRST);
paragraph = footer.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
run = paragraph.createRun();
run.setText("First page footer...");
// create default footer
footer = document.createFooter(HeaderFooterType.DEFAULT);
paragraph = footer.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
run = paragraph.createRun();
run.setText("Default footer...");
FileOutputStream out = new FileOutputStream("CreateWordFooters.docx");
document.write(out);
out.close();
document.close();
}
}

Apache-poi : cannot add image in docx header

I want to create a header in an existing docx document with Apache POI (I have tried versions 3.14 and 4.0.1).
But when I open the docx, in the header I am getting this ("we cannot display this image"):
I am doing this :
document = new XWPFDocument(OPCPackage.open("C:\\users\\thomas\\withoutHeader.docx"));
CTSectPr sectPr1 = document.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr1);
//Header content
CTP ctpHeader = CTP.Factory.newInstance();
CTR ctrHeader = ctpHeader.addNewR();
CTText ctHeader = ctrHeader.addNewT();
String headerText = "This is header";
ctHeader.setStringValue(headerText);
XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);
XWPFParagraph[] parsHeader = new XWPFParagraph[1];
parsHeader[0] = headerParagraph;
policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);
//Header image
policy = new XWPFHeaderFooterPolicy(document);
XWPFHeader header = policy.getDefaultHeader();
System.out.println(header.getText());
XWPFParagraph paragraph = header.getParagraphArray(0);
if (paragraph == null) paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun r = paragraph.createRun();
FileInputStream in = new FileInputStream("C:\\Users\\thomas\\dev\\logo.png");
r.addPicture(in, Document.PICTURE_TYPE_JPEG, "C:\\Users\\thomas\\dev\\logo.png", Units.toEMU(100), Units.toEMU(50));
in.close();
FileOutputStream out = new FileOutputStream("C:\\users\\thomas\\withHeader.docx");
document.write(out);
document.close();
out.close();
What am I missing?
Following complete example works for me using current apache poi 4.1.1.
The example opens a *.docx template which should not have headers already. Then it adds a default header having text and a logo.png.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.apache.poi.util.Units;
public class CreateWordHeaderWithImage {
public static void main(String[] args) throws Exception {
XWPFDocument doc = new XWPFDocument(new FileInputStream("./Template.docx"));
XWPFParagraph paragraph;
XWPFRun run;
// create header
XWPFHeader header = doc.createHeader(HeaderFooterType.DEFAULT);
// header's first paragraph
paragraph = header.getParagraphArray(0);
if (paragraph == null) paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
run = paragraph.createRun();
run.setText("This is header ");
FileInputStream in = new FileInputStream("./logo.png");
run.addPicture(in, Document.PICTURE_TYPE_PNG, "logo.png", Units.toEMU(100), Units.toEMU(50));
in.close();
FileOutputStream out = new FileOutputStream("./CreateWordHeaderWithImage.docx");
doc.write(out);
doc.close();
out.close();
}
}
Same code works using apache poi 3.17 too.

how to add shapes in word document using apache poi library [duplicate]

I'm trying to add some shapes and a logo-file into the header of my word docx document. Adding a picture works for me, but i didn't find any solution how to add a shape. can anyone help me?
String imgFile="logo.png";
XWPFDocument document = new XWPFDocument(new FileInputStream("myfile.docx"));
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr);
XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
XWPFParagraph paragraph = header.getParagraphArray(0);
paragraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run = paragraph.createRun();
XWPFPicture picture = run.addPicture(new FileInputStream(imgFile), XWPFDocument.PICTURE_TYPE_PNG, imgFile, Units.toEMU(195), Units.toEMU(22));
String blipID = "";
for(XWPFPictureData picturedata : header.getAllPackagePictures()) {
blipID = header.getRelationId(picturedata);
}
picture.getCTPicture().getBlipFill().getBlip().setEmbed(blipID); //now they have a blipID too
At the end the header should look like this
thanks
Since the apache poi XWPF is really in beta state until now, such things are only possible if one knows exactly how Word will store it's contents into the XML. Then one can work around the inadequacies of apache poi XWPF. You have already used such a workaround which corrects the missed blipID when pictures are added to runs in header or footer.
To discover how Word will store it's contents into the XML is not rocket science. A *.docx file is simply a ZIP file. If one unzip this file using a Zip software, one can simply have a look into the XML files.
As far as I know adding shapes (in this case text boxes) in Word documents is not supported by apache poi directly. For this using the low level underlying objects (in this case CTGroupand CTShape) is needed.
Example: (code should be self explanatory)
import java.io.*;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTxbxContent;
import com.microsoft.schemas.vml.CTGroup;
import com.microsoft.schemas.vml.CTShape;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTabStop;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTabJc;
import org.w3c.dom.Node;
import java.math.BigInteger;
public class CreateWordHeaderFooterTextBoxPicture {
public static void main(String[] args) throws Exception {
XWPFDocument doc= new XWPFDocument();
// the body content
XWPFParagraph paragraph = doc.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("The Body:");
paragraph = doc.createParagraph();
run=paragraph.createRun();
run.setText("Lorem ipsum....");
// create header start
XWPFHeaderFooterPolicy headerFooterPolicy = doc.createHeaderFooterPolicy();
XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
// header's first paragraph
paragraph = header.getParagraphArray(0);
if (paragraph == null) paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
// create tab stops
CTTabStop tabStop = paragraph.getCTP().getPPr().addNewTabs().addNewTab();
tabStop.setVal(STTabJc.CENTER);
int twipsPerInch = 1440;
tabStop.setPos(BigInteger.valueOf(3 * twipsPerInch));
tabStop = paragraph.getCTP().getPPr().getTabs().addNewTab();
tabStop.setVal(STTabJc.RIGHT);
twipsPerInch = 1440;
tabStop.setPos(BigInteger.valueOf(6 * twipsPerInch));
// first run in header's first paragraph, to be for first text box
run = paragraph.createRun();
// create inline text box in run
CTGroup ctGroup = CTGroup.Factory.newInstance();
CTShape ctShape = ctGroup.addNewShape();
ctShape.setStyle("width:80pt;height:24pt");
CTTxbxContent ctTxbxContent = ctShape.addNewTextbox().addNewTxbxContent();
XWPFParagraph textboxparagraph = new XWPFParagraph(ctTxbxContent.addNewP(), (IBody)header);
XWPFRun textboxrun = textboxparagraph.createRun();
textboxrun.setText("The TextBox 1...");
textboxrun.setFontSize(10);
Node ctGroupNode = ctGroup.getDomNode();
CTPicture ctPicture = CTPicture.Factory.parse(ctGroupNode);
CTR cTR = run.getCTR();
cTR.addNewPict();
cTR.setPictArray(0, ctPicture);
// add tab to run
run.addTab();
// second run in header's first paragraph, to be for logo picture
run = paragraph.createRun();
// add the picture in the headers run
String imgFile="Logo.png";
XWPFPicture picture = run.addPicture(new FileInputStream(imgFile), XWPFDocument.PICTURE_TYPE_PNG, imgFile, Units.toEMU(195), Units.toEMU(22));
String blipID = "";
for(XWPFPictureData picturedata : header.getAllPackagePictures()) {
blipID = header.getRelationId(picturedata);
}
picture.getCTPicture().getBlipFill().getBlip().setEmbed(blipID);
// add tab to run
run.addTab();
// third run in header's first paragraph, to be for second text box
run = paragraph.createRun();
// create inline text box in run
ctGroup = CTGroup.Factory.newInstance();
ctShape = ctGroup.addNewShape();
ctShape.setStyle("width:80pt;height:24pt");
ctTxbxContent = ctShape.addNewTextbox().addNewTxbxContent();
textboxparagraph = new XWPFParagraph(ctTxbxContent.addNewP(), (IBody)header);
textboxrun = textboxparagraph.createRun();
textboxrun.setText("The TextBox 2...");
textboxrun.setFontSize(10);
ctGroupNode = ctGroup.getDomNode();
ctPicture = CTPicture.Factory.parse(ctGroupNode);
cTR = run.getCTR();
cTR.addNewPict();
cTR.setPictArray(0, ctPicture);
// create header end
// create footer start
XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
paragraph = footer.getParagraphArray(0);
if (paragraph == null) paragraph = footer.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
run = paragraph.createRun();
run.setText("The Footer:");
doc.write(new FileOutputStream("test.docx"));
}
}

Apache POI XWPF adding shapes to header

I'm trying to add some shapes and a logo-file into the header of my word docx document. Adding a picture works for me, but i didn't find any solution how to add a shape. can anyone help me?
String imgFile="logo.png";
XWPFDocument document = new XWPFDocument(new FileInputStream("myfile.docx"));
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr);
XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
XWPFParagraph paragraph = header.getParagraphArray(0);
paragraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run = paragraph.createRun();
XWPFPicture picture = run.addPicture(new FileInputStream(imgFile), XWPFDocument.PICTURE_TYPE_PNG, imgFile, Units.toEMU(195), Units.toEMU(22));
String blipID = "";
for(XWPFPictureData picturedata : header.getAllPackagePictures()) {
blipID = header.getRelationId(picturedata);
}
picture.getCTPicture().getBlipFill().getBlip().setEmbed(blipID); //now they have a blipID too
At the end the header should look like this
thanks
Since the apache poi XWPF is really in beta state until now, such things are only possible if one knows exactly how Word will store it's contents into the XML. Then one can work around the inadequacies of apache poi XWPF. You have already used such a workaround which corrects the missed blipID when pictures are added to runs in header or footer.
To discover how Word will store it's contents into the XML is not rocket science. A *.docx file is simply a ZIP file. If one unzip this file using a Zip software, one can simply have a look into the XML files.
As far as I know adding shapes (in this case text boxes) in Word documents is not supported by apache poi directly. For this using the low level underlying objects (in this case CTGroupand CTShape) is needed.
Example: (code should be self explanatory)
import java.io.*;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTxbxContent;
import com.microsoft.schemas.vml.CTGroup;
import com.microsoft.schemas.vml.CTShape;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTabStop;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTabJc;
import org.w3c.dom.Node;
import java.math.BigInteger;
public class CreateWordHeaderFooterTextBoxPicture {
public static void main(String[] args) throws Exception {
XWPFDocument doc= new XWPFDocument();
// the body content
XWPFParagraph paragraph = doc.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("The Body:");
paragraph = doc.createParagraph();
run=paragraph.createRun();
run.setText("Lorem ipsum....");
// create header start
XWPFHeaderFooterPolicy headerFooterPolicy = doc.createHeaderFooterPolicy();
XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
// header's first paragraph
paragraph = header.getParagraphArray(0);
if (paragraph == null) paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
// create tab stops
CTTabStop tabStop = paragraph.getCTP().getPPr().addNewTabs().addNewTab();
tabStop.setVal(STTabJc.CENTER);
int twipsPerInch = 1440;
tabStop.setPos(BigInteger.valueOf(3 * twipsPerInch));
tabStop = paragraph.getCTP().getPPr().getTabs().addNewTab();
tabStop.setVal(STTabJc.RIGHT);
twipsPerInch = 1440;
tabStop.setPos(BigInteger.valueOf(6 * twipsPerInch));
// first run in header's first paragraph, to be for first text box
run = paragraph.createRun();
// create inline text box in run
CTGroup ctGroup = CTGroup.Factory.newInstance();
CTShape ctShape = ctGroup.addNewShape();
ctShape.setStyle("width:80pt;height:24pt");
CTTxbxContent ctTxbxContent = ctShape.addNewTextbox().addNewTxbxContent();
XWPFParagraph textboxparagraph = new XWPFParagraph(ctTxbxContent.addNewP(), (IBody)header);
XWPFRun textboxrun = textboxparagraph.createRun();
textboxrun.setText("The TextBox 1...");
textboxrun.setFontSize(10);
Node ctGroupNode = ctGroup.getDomNode();
CTPicture ctPicture = CTPicture.Factory.parse(ctGroupNode);
CTR cTR = run.getCTR();
cTR.addNewPict();
cTR.setPictArray(0, ctPicture);
// add tab to run
run.addTab();
// second run in header's first paragraph, to be for logo picture
run = paragraph.createRun();
// add the picture in the headers run
String imgFile="Logo.png";
XWPFPicture picture = run.addPicture(new FileInputStream(imgFile), XWPFDocument.PICTURE_TYPE_PNG, imgFile, Units.toEMU(195), Units.toEMU(22));
String blipID = "";
for(XWPFPictureData picturedata : header.getAllPackagePictures()) {
blipID = header.getRelationId(picturedata);
}
picture.getCTPicture().getBlipFill().getBlip().setEmbed(blipID);
// add tab to run
run.addTab();
// third run in header's first paragraph, to be for second text box
run = paragraph.createRun();
// create inline text box in run
ctGroup = CTGroup.Factory.newInstance();
ctShape = ctGroup.addNewShape();
ctShape.setStyle("width:80pt;height:24pt");
ctTxbxContent = ctShape.addNewTextbox().addNewTxbxContent();
textboxparagraph = new XWPFParagraph(ctTxbxContent.addNewP(), (IBody)header);
textboxrun = textboxparagraph.createRun();
textboxrun.setText("The TextBox 2...");
textboxrun.setFontSize(10);
ctGroupNode = ctGroup.getDomNode();
ctPicture = CTPicture.Factory.parse(ctGroupNode);
cTR = run.getCTR();
cTR.addNewPict();
cTR.setPictArray(0, ctPicture);
// create header end
// create footer start
XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
paragraph = footer.getParagraphArray(0);
if (paragraph == null) paragraph = footer.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
run = paragraph.createRun();
run.setText("The Footer:");
doc.write(new FileOutputStream("test.docx"));
}
}

Categories

Resources