import org.apache.poi.xwpf.usermodel.XWPFHeaderFooter;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRelation;
-import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlOptions;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtrRef;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.HdrDocument;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr.Enum;
+
import schemasMicrosoftComOfficeOffice.CTLock;
import schemasMicrosoftComOfficeOffice.STConnectType;
import schemasMicrosoftComVml.CTFormulas;
* and creates any header and footer objects
* as required.
*/
- public XWPFHeaderFooterPolicy(XWPFDocument doc) throws IOException, XmlException {
+ public XWPFHeaderFooterPolicy(XWPFDocument doc) {
this(doc, doc.getDocument().getBody().getSectPr());
}
* and creates any header and footer objects
* as required.
*/
- public XWPFHeaderFooterPolicy(XWPFDocument doc, CTSectPr sectPr) throws IOException, XmlException {
+ public XWPFHeaderFooterPolicy(XWPFDocument doc, CTSectPr sectPr) {
// Grab what headers and footers have been defined
// For now, we don't care about different ranges, as it
// doesn't seem that .docx properly supports that
assignHeader(wrapper, type);
hdrDoc.save(outputStream, xmlOptions);
outputStream.close();
+
return wrapper;
}
}
} else {
CTP p = ftr.addNewP();
- byte[] rsidr = doc.getDocument().getBody().getPArray(0).getRsidR();
- byte[] rsidrdefault = doc.getDocument().getBody().getPArray(0).getRsidRDefault();
- p.setRsidP(rsidr);
- p.setRsidRDefault(rsidrdefault);
+ CTBody body = doc.getDocument().getBody();
+ if (body.sizeOfPArray() > 0) {
+ CTP p0 = body.getPArray(0);
+ if (p0.isSetRsidR()) {
+ byte[] rsidr = p0.getRsidR();
+ byte[] rsidrdefault = p0.getRsidRDefault();
+ p.setRsidP(rsidr);
+ p.setRsidRDefault(rsidrdefault);
+ }
+ }
CTPPr pPr = p.addNewPPr();
pPr.addNewPStyle().setVal(pStyle);
}
public XWPFHeaderFooterPolicy getHeaderFooterPolicy() {
return headerFooterPolicy;
}
+ public XWPFHeaderFooterPolicy createHeaderFooterPolicy() {
+ if (headerFooterPolicy == null) {
+ if (! ctDocument.getBody().isSetSectPr()) {
+ ctDocument.getBody().addNewSectPr();
+ }
+ headerFooterPolicy = new XWPFHeaderFooterPolicy(this);
+ }
+ return headerFooterPolicy;
+ }
/**
* Returns the styles object used
return null;
}
+ /**
+ * Adds a new paragraph at the end of the header or footer
+ */
+ public XWPFParagraph createParagraph() {
+ XWPFParagraph paragraph = new XWPFParagraph(headerFooter.addNewP(), this);
+ paragraphs.add(paragraph);
+ return paragraph;
+ }
+
/**
* add a new paragraph at position of the cursor
*
import java.io.IOException;
import junit.framework.TestCase;
+
import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
+import org.apache.poi.xwpf.usermodel.XWPFFooter;
+import org.apache.poi.xwpf.usermodel.XWPFHeader;
/**
* Tests for XWPF Header Footer Stuff
private XWPFDocument diffFirst;
protected void setUp() throws IOException {
-
noHeader = XWPFTestDataSamples.openSampleDocument("NoHeadFoot.docx");
header = XWPFTestDataSamples.openSampleDocument("ThreeColHead.docx");
headerFooter = XWPFTestDataSamples.openSampleDocument("SimpleHeadThreeColFoot.docx");
assertEquals(policy.getDefaultFooter(), policy.getFooter(2));
assertEquals(policy.getDefaultFooter(), policy.getFooter(3));
}
+
+ @SuppressWarnings("resource")
+ public void testCreate() throws Exception {
+ XWPFDocument doc = new XWPFDocument();
+ assertEquals(null, doc.getHeaderFooterPolicy());
+ assertEquals(0, doc.getHeaderList().size());
+ assertEquals(0, doc.getFooterList().size());
+
+ XWPFHeaderFooterPolicy policy = doc.createHeaderFooterPolicy();
+ assertNotNull(doc.getHeaderFooterPolicy());
+ assertEquals(0, doc.getHeaderList().size());
+ assertEquals(0, doc.getFooterList().size());
+
+ // Create a header and a footer
+ XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
+ XWPFFooter footer = policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
+ header.createParagraph().createRun().setText("Header Hello");
+ footer.createParagraph().createRun().setText("Footer Bye");
+
+
+ // Save, re-load, and check
+ doc = XWPFTestDataSamples.writeOutAndReadBack(doc);
+ assertNotNull(doc.getHeaderFooterPolicy());
+ assertEquals(1, doc.getHeaderList().size());
+ assertEquals(1, doc.getFooterList().size());
+
+ assertEquals("Header Hello\n", doc.getHeaderList().get(0).getText());
+ assertEquals("Footer Bye\n", doc.getFooterList().get(0).getText());
+ }
public void testContents() {
XWPFHeaderFooterPolicy policy;
import static org.junit.Assert.assertArrayEquals;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
import junit.framework.TestCase;
+
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.xssf.usermodel.XSSFRelation;
import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
public class TestXWPFPictureData extends TestCase {
XWPFDocument readBack = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
verifyOneHeaderPicture(readBack);
}
+
+ public void FIXMEtestCreateHeaderPicture() throws Exception { // TODO Fix
+ XWPFDocument doc = new XWPFDocument();
+
+ // Starts with no header
+ XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();
+ assertNull(policy);
+
+ // Add a default header
+ policy = doc.createHeaderFooterPolicy();
+
+ XWPFParagraph[] hparas = new XWPFParagraph[] {
+ new XWPFParagraph(CTP.Factory.newInstance(), doc)
+ };
+ hparas[0].createRun().setText("Header Hello World!");
+ XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, hparas);
+ assertEquals(0, header.getAllPictures().size());
+ assertEquals(1, header.getParagraphs().size());
+
+ // Add a picture to it
+ header.getParagraphs().get(0).getRuns().get(0).addPicture(
+ new ByteArrayInputStream(new byte[] {1,2,3,4}),
+ Document.PICTURE_TYPE_JPEG, "test.jpg", 2, 2);
+
+ // Check
+ verifyOneHeaderPicture(doc);
+
+ // Save, re-load, re-check
+ XWPFDocument readBack = XWPFTestDataSamples.writeOutAndReadBack(doc);
+ verifyOneHeaderPicture(readBack);
+ }
private void verifyOneHeaderPicture(XWPFDocument sampleDoc) {
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();