createHeader/Footer with paragraphs and fixed GUMP break (case sensitive/insensitive filename problem)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@805820 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
9926c89006
commit
a7e9db045f
@ -30,6 +30,7 @@ import org.apache.poi.xwpf.usermodel.XWPFFactory;
|
|||||||
import org.apache.poi.xwpf.usermodel.XWPFFooter;
|
import org.apache.poi.xwpf.usermodel.XWPFFooter;
|
||||||
import org.apache.poi.xwpf.usermodel.XWPFHeader;
|
import org.apache.poi.xwpf.usermodel.XWPFHeader;
|
||||||
import org.apache.poi.xwpf.usermodel.XWPFHeaderFooter;
|
import org.apache.poi.xwpf.usermodel.XWPFHeaderFooter;
|
||||||
|
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
||||||
import org.apache.poi.xwpf.usermodel.XWPFRelation;
|
import org.apache.poi.xwpf.usermodel.XWPFRelation;
|
||||||
import org.apache.xmlbeans.XmlException;
|
import org.apache.xmlbeans.XmlException;
|
||||||
import org.apache.xmlbeans.XmlOptions;
|
import org.apache.xmlbeans.XmlOptions;
|
||||||
@ -127,15 +128,18 @@ public class XWPFHeaderFooterPolicy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public XWPFHeader createHeader(Enum type) throws IOException {
|
public XWPFHeader createHeader(Enum type) throws IOException {
|
||||||
|
return createHeader(type, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public XWPFHeader createHeader(Enum type, XWPFParagraph[] pars) throws IOException {
|
||||||
XWPFRelation relation = XWPFRelation.HEADER;
|
XWPFRelation relation = XWPFRelation.HEADER;
|
||||||
String pStyle = "Header";
|
String pStyle = "Header";
|
||||||
int i = getRelationIndex(relation);
|
int i = getRelationIndex(relation);
|
||||||
HdrDocument hdrDoc = HdrDocument.Factory.newInstance();
|
HdrDocument hdrDoc = HdrDocument.Factory.newInstance();
|
||||||
XWPFHeader wrapper = (XWPFHeader)doc.createRelationship(relation, XWPFFactory.getInstance(), i);
|
XWPFHeader wrapper = (XWPFHeader)doc.createRelationship(relation, XWPFFactory.getInstance(), i);
|
||||||
|
|
||||||
CTHdrFtr hdr = buildHdr(type, pStyle, wrapper);
|
CTHdrFtr hdr = buildHdr(type, pStyle, wrapper, pars);
|
||||||
|
|
||||||
OutputStream outputStream = wrapper.getPackagePart().getOutputStream();
|
OutputStream outputStream = wrapper.getPackagePart().getOutputStream();
|
||||||
hdrDoc.setHdr(hdr);
|
hdrDoc.setHdr(hdr);
|
||||||
@ -148,15 +152,18 @@ public class XWPFHeaderFooterPolicy {
|
|||||||
return wrapper;
|
return wrapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public XWPFFooter createFooter(Enum type) throws IOException {
|
public XWPFFooter createFooter(Enum type) throws IOException {
|
||||||
|
return createFooter(type, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public XWPFFooter createFooter(Enum type, XWPFParagraph[] pars) throws IOException {
|
||||||
XWPFRelation relation = XWPFRelation.FOOTER;
|
XWPFRelation relation = XWPFRelation.FOOTER;
|
||||||
String pStyle = "Footer";
|
String pStyle = "Footer";
|
||||||
int i = getRelationIndex(relation);
|
int i = getRelationIndex(relation);
|
||||||
FtrDocument ftrDoc = FtrDocument.Factory.newInstance();
|
FtrDocument ftrDoc = FtrDocument.Factory.newInstance();
|
||||||
XWPFFooter wrapper = (XWPFFooter)doc.createRelationship(relation, XWPFFactory.getInstance(), i);
|
XWPFFooter wrapper = (XWPFFooter)doc.createRelationship(relation, XWPFFactory.getInstance(), i);
|
||||||
|
|
||||||
CTHdrFtr ftr = buildFtr(type, pStyle, wrapper);
|
CTHdrFtr ftr = buildFtr(type, pStyle, wrapper, pars);
|
||||||
|
|
||||||
OutputStream outputStream = wrapper.getPackagePart().getOutputStream();
|
OutputStream outputStream = wrapper.getPackagePart().getOutputStream();
|
||||||
ftrDoc.setFtr(ftr);
|
ftrDoc.setFtr(ftr);
|
||||||
@ -183,29 +190,36 @@ public class XWPFHeaderFooterPolicy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private CTHdrFtr buildFtr(Enum type, String pStyle, XWPFHeaderFooter wrapper) {
|
private CTHdrFtr buildFtr(Enum type, String pStyle, XWPFHeaderFooter wrapper, XWPFParagraph[] pars) {
|
||||||
CTHdrFtr ftr = buildHdrFtr(pStyle);
|
CTHdrFtr ftr = buildHdrFtr(pStyle, pars);
|
||||||
setFooterReference(type, wrapper);
|
setFooterReference(type, wrapper);
|
||||||
return ftr;
|
return ftr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private CTHdrFtr buildHdr(Enum type, String pStyle, XWPFHeaderFooter wrapper) {
|
private CTHdrFtr buildHdr(Enum type, String pStyle, XWPFHeaderFooter wrapper, XWPFParagraph[] pars) {
|
||||||
CTHdrFtr hdr = buildHdrFtr(pStyle);
|
CTHdrFtr hdr = buildHdrFtr(pStyle, pars);
|
||||||
setHeaderReference(type, wrapper);
|
setHeaderReference(type, wrapper);
|
||||||
return hdr;
|
return hdr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private CTHdrFtr buildHdrFtr(String pStyle, XWPFParagraph[] paragraphs) {
|
||||||
private CTHdrFtr buildHdrFtr(String pStyle) {
|
|
||||||
CTHdrFtr ftr = CTHdrFtr.Factory.newInstance();
|
CTHdrFtr ftr = CTHdrFtr.Factory.newInstance();
|
||||||
CTP p = ftr.addNewP();
|
if (paragraphs != null) {
|
||||||
byte[] rsidr = doc.getDocument().getBody().getPArray()[0].getRsidR();
|
for (int i = 0 ; i < paragraphs.length ; i++) {
|
||||||
byte[] rsidrdefault = doc.getDocument().getBody().getPArray()[0].getRsidRDefault();
|
CTP p = ftr.addNewP();
|
||||||
p.setRsidP(rsidr);
|
ftr.setPArray(0, paragraphs[i].getCTP());
|
||||||
p.setRsidRDefault(rsidrdefault);
|
}
|
||||||
CTPPr pPr = p.addNewPPr();
|
}
|
||||||
pPr.addNewPStyle().setVal(pStyle);
|
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);
|
||||||
|
CTPPr pPr = p.addNewPPr();
|
||||||
|
pPr.addNewPStyle().setVal(pStyle);
|
||||||
|
}
|
||||||
return ftr;
|
return ftr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,6 +24,9 @@ import junit.framework.TestCase;
|
|||||||
import org.apache.poi.POIXMLDocument;
|
import org.apache.poi.POIXMLDocument;
|
||||||
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
|
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr;
|
||||||
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
|
||||||
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
|
||||||
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
|
||||||
|
|
||||||
public class TestXWPFHeader extends TestCase {
|
public class TestXWPFHeader extends TestCase {
|
||||||
|
|
||||||
@ -54,7 +57,7 @@ public class TestXWPFHeader extends TestCase {
|
|||||||
public void testSetHeader() throws IOException {
|
public void testSetHeader() throws IOException {
|
||||||
File sampleFile = new File(
|
File sampleFile = new File(
|
||||||
System.getProperty("HWPF.testdata.path") +
|
System.getProperty("HWPF.testdata.path") +
|
||||||
File.separator + "sampleDoc.docx"
|
File.separator + "SampleDoc.docx"
|
||||||
);
|
);
|
||||||
assertTrue(sampleFile.exists());
|
assertTrue(sampleFile.exists());
|
||||||
XWPFDocument sampleDoc;
|
XWPFDocument sampleDoc;
|
||||||
@ -64,10 +67,31 @@ public class TestXWPFHeader extends TestCase {
|
|||||||
// no header is set (yet)
|
// no header is set (yet)
|
||||||
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
|
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
|
||||||
assertNull(policy.getDefaultHeader());
|
assertNull(policy.getDefaultHeader());
|
||||||
|
assertNull(policy.getFirstPageHeader());
|
||||||
|
assertNull(policy.getDefaultFooter());
|
||||||
|
|
||||||
|
CTP ctP1 = CTP.Factory.newInstance();
|
||||||
|
CTR ctR1 = ctP1.addNewR();
|
||||||
|
CTText t = ctR1.addNewT();
|
||||||
|
t.set("Paragraph in header");
|
||||||
|
|
||||||
|
CTP ctP2 = CTP.Factory.newInstance();
|
||||||
|
CTR ctR2 = ctP2.addNewR();
|
||||||
|
CTText t2 = ctR2.addNewT();
|
||||||
|
t2.set("Second paragraph.. for footer");
|
||||||
|
|
||||||
|
XWPFParagraph p1 = new XWPFParagraph(ctP1);
|
||||||
|
XWPFParagraph[] pars = new XWPFParagraph[1];
|
||||||
|
pars[0] = p1;
|
||||||
|
|
||||||
|
XWPFParagraph p2 = new XWPFParagraph(ctP2);
|
||||||
|
XWPFParagraph[] pars2 = new XWPFParagraph[1];
|
||||||
|
pars2[0] = p2;
|
||||||
|
|
||||||
// set a default header and test it is not null
|
// set a default header and test it is not null
|
||||||
policy.createHeader(policy.DEFAULT);
|
policy.createHeader(policy.DEFAULT, pars);
|
||||||
policy.createHeader(policy.FIRST);
|
policy.createHeader(policy.FIRST);
|
||||||
policy.createFooter(policy.DEFAULT);
|
policy.createFooter(policy.DEFAULT, pars2);
|
||||||
|
|
||||||
assertNotNull(policy.getDefaultHeader());
|
assertNotNull(policy.getDefaultHeader());
|
||||||
assertNotNull(policy.getFirstPageHeader());
|
assertNotNull(policy.getFirstPageHeader());
|
||||||
|
Loading…
Reference in New Issue
Block a user