use of org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy in project poi by apache.
the class TestXWPFPictureData method testBug51770.
public void testBug51770() throws InvalidFormatException, IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug51170.docx");
XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();
XWPFHeader header = policy.getDefaultHeader();
for (XWPFParagraph paragraph : header.getParagraphs()) {
for (XWPFRun run : paragraph.getRuns()) {
for (XWPFPicture picture : run.getEmbeddedPictures()) {
if (paragraph.getDocument() != null) {
//System.out.println(picture.getCTPicture());
XWPFPictureData data = picture.getPictureData();
if (data != null)
System.out.println(data.getFileName());
}
}
}
}
}
use of org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy in project poi by apache.
the class TestXWPFPictureData method testCreateHeaderPicture.
public void testCreateHeaderPicture() throws Exception {
XWPFDocument doc = new XWPFDocument();
// Starts with no header
XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();
assertNull(policy);
// Add a default header
policy = doc.createHeaderFooterPolicy();
XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
header.createParagraph().createRun().setText("Hello, Header World!");
header.createParagraph().createRun().setText("Paragraph 2");
assertEquals(0, header.getAllPictures().size());
assertEquals(2, header.getParagraphs().size());
// Add a picture to the first paragraph
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);
}
use of org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy in project poi by apache.
the class TestXWPFRun method testPictureInHeader.
@Test
public void testPictureInHeader() throws IOException {
XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx");
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
XWPFHeader header = policy.getDefaultHeader();
int count = 0;
for (XWPFParagraph p : header.getParagraphs()) {
for (XWPFRun r : p.getRuns()) {
List<XWPFPicture> pictures = r.getEmbeddedPictures();
for (XWPFPicture pic : pictures) {
assertNotNull(pic.getPictureData());
assertEquals("DOZOR", pic.getDescription());
}
count += pictures.size();
}
}
assertEquals(1, count);
sampleDoc.close();
}
use of org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy in project tika by apache.
the class XWPFWordExtractorDecorator method buildXHTML.
/**
* @see org.apache.poi.xwpf.extractor.XWPFWordExtractor#getText()
*/
@Override
protected void buildXHTML(XHTMLContentHandler xhtml) throws SAXException, XmlException, IOException {
XWPFHeaderFooterPolicy hfPolicy = document.getHeaderFooterPolicy();
XWPFListManager listManager = new XWPFListManager(document.getNumbering());
// headers
if (hfPolicy != null) {
extractHeaders(xhtml, hfPolicy, listManager);
}
// process text in the order that it occurs in
extractIBodyText(document, listManager, xhtml);
// then all document tables
if (hfPolicy != null) {
extractFooters(xhtml, hfPolicy, listManager);
}
}
use of org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy in project poi by apache.
the class XWPFWordExtractor method appendParagraphText.
public void appendParagraphText(StringBuffer text, XWPFParagraph paragraph) {
CTSectPr ctSectPr = null;
if (paragraph.getCTP().getPPr() != null) {
ctSectPr = paragraph.getCTP().getPPr().getSectPr();
}
XWPFHeaderFooterPolicy headerFooterPolicy = null;
if (ctSectPr != null) {
headerFooterPolicy = new XWPFHeaderFooterPolicy(document, ctSectPr);
extractHeaders(text, headerFooterPolicy);
}
for (IRunElement run : paragraph.getRuns()) {
text.append(run);
if (run instanceof XWPFHyperlinkRun && fetchHyperlinks) {
XWPFHyperlink link = ((XWPFHyperlinkRun) run).getHyperlink(document);
if (link != null)
text.append(" <").append(link.getURL()).append(">");
}
}
// Add comments
XWPFCommentsDecorator decorator = new XWPFCommentsDecorator(paragraph, null);
String commentText = decorator.getCommentText();
if (commentText.length() > 0) {
text.append(commentText).append('\n');
}
// Do endnotes and footnotes
String footnameText = paragraph.getFootnoteText();
if (footnameText != null && footnameText.length() > 0) {
text.append(footnameText).append('\n');
}
if (ctSectPr != null) {
extractFooters(text, headerFooterPolicy);
}
}
Aggregations