Search in sources :

Example 6 with XWPFHeaderFooterPolicy

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());
                }
            }
        }
    }
}
Also used : XWPFHeaderFooterPolicy(org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy)

Example 7 with XWPFHeaderFooterPolicy

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XWPFHeaderFooterPolicy(org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy)

Example 8 with XWPFHeaderFooterPolicy

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();
}
Also used : XWPFHeaderFooterPolicy(org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy) Test(org.junit.Test)

Example 9 with XWPFHeaderFooterPolicy

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);
    }
}
Also used : XWPFHeaderFooterPolicy(org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy)

Example 10 with XWPFHeaderFooterPolicy

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);
    }
}
Also used : XWPFHyperlink(org.apache.poi.xwpf.usermodel.XWPFHyperlink) XWPFCommentsDecorator(org.apache.poi.xwpf.model.XWPFCommentsDecorator) XWPFHyperlinkRun(org.apache.poi.xwpf.usermodel.XWPFHyperlinkRun) IRunElement(org.apache.poi.xwpf.usermodel.IRunElement) XWPFHeaderFooterPolicy(org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy) CTSectPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr)

Aggregations

XWPFHeaderFooterPolicy (org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy)17 Test (org.junit.Test)5 XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)3 XWPFRun (org.apache.poi.xwpf.usermodel.XWPFRun)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 XWPFCommentsDecorator (org.apache.poi.xwpf.model.XWPFCommentsDecorator)2 IRunElement (org.apache.poi.xwpf.usermodel.IRunElement)2 XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)2 XWPFHyperlink (org.apache.poi.xwpf.usermodel.XWPFHyperlink)2 XWPFHyperlinkRun (org.apache.poi.xwpf.usermodel.XWPFHyperlinkRun)2 XmlCursor (org.apache.xmlbeans.XmlCursor)2 XmlObject (org.apache.xmlbeans.XmlObject)2 CTP (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)2 CTSectPr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr)2 CTText (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 QName (javax.xml.namespace.QName)1