Search in sources :

Example 1 with SectionWrapper

use of org.docx4j.model.structure.SectionWrapper in project docx4j-template by vindell.

the class AddingPageNrToFooter method createFooterReference.

/**
 * This method fetches the document final section properties, and adds a newly
 * created footer reference to them.
 *
 * @param relationship
 */
public static void createFooterReference(Relationship relationship) {
    List<SectionWrapper> sections = wordMLPackage.getDocumentModel().getSections();
    SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
    // There is always a section wrapper, but it might not contain a sectPr
    if (sectPr == null) {
        sectPr = factory.createSectPr();
        wordMLPackage.getMainDocumentPart().addObject(sectPr);
        sections.get(sections.size() - 1).setSectPr(sectPr);
    }
    FooterReference footerReference = factory.createFooterReference();
    footerReference.setId(relationship.getId());
    footerReference.setType(HdrFtrRef.DEFAULT);
    sectPr.getEGHdrFtrReferences().add(footerReference);
}
Also used : SectionWrapper(org.docx4j.model.structure.SectionWrapper) FooterReference(org.docx4j.wml.FooterReference) SectPr(org.docx4j.wml.SectPr)

Example 2 with SectionWrapper

use of org.docx4j.model.structure.SectionWrapper in project docx4j-template by vindell.

the class AddingAFooter method createFooterReference.

/**
 *  First we retrieve the document sections from the package. As we want to add
 *  a footer, we get the last section and take the section properties from it.
 *  The section is always present, but it might not have properties, so we check
 *  if they exist to see if we should create them. If they need to be created,
 *  we do and add them to the main document part and the section.
 *  Then we create a reference to the footer, give it the id of the relationship,
 *  set the type to header/footer reference and add it to the collection of
 *  references to headers and footers in the section properties.
 *
 * @param relationship
 */
private static void createFooterReference(Relationship relationship) {
    List<SectionWrapper> sections = wordMLPackage.getDocumentModel().getSections();
    SectPr sectionProperties = sections.get(sections.size() - 1).getSectPr();
    // There is always a section wrapper, but it might not contain a sectPr
    if (sectionProperties == null) {
        sectionProperties = factory.createSectPr();
        wordMLPackage.getMainDocumentPart().addObject(sectionProperties);
        sections.get(sections.size() - 1).setSectPr(sectionProperties);
    }
    FooterReference footerReference = factory.createFooterReference();
    footerReference.setId(relationship.getId());
    footerReference.setType(HdrFtrRef.DEFAULT);
    sectionProperties.getEGHdrFtrReferences().add(footerReference);
}
Also used : SectionWrapper(org.docx4j.model.structure.SectionWrapper) FooterReference(org.docx4j.wml.FooterReference) SectPr(org.docx4j.wml.SectPr)

Example 3 with SectionWrapper

use of org.docx4j.model.structure.SectionWrapper in project flexmark-java by vsch.

the class CoreNodeDocxRenderer method newImage.

public R newImage(final DocxRendererContext docx, byte[] bytes, String filenameHint, Attributes attributes, int id1, int id2) {
    try {
        BinaryPartAbstractImage imagePart = null;
        imagePart = BinaryPartAbstractImage.createImagePart(docx.getPackage(), docx.getContainerPart(), bytes);
        Inline inline = null;
        String altText = attributes.contains("alt") ? attributes.getValue("alt") : "";
        List<SectionWrapper> sections = docx.getPackage().getDocumentModel().getSections();
        PageDimensions page = sections.get(sections.size() - 1).getPageDimensions();
        double writableWidthTwips = page.getWritableWidthTwips();
        long cx = getSizeInfo(attributes, "width", page.getWritableWidthTwips());
        long cy = cx > 0 ? getSizeInfo(attributes, "height", page.getWritableHeightTwips()) : -1;
        // kludge: normally there is no max-width attribute but we can fake it
        long longMaxWidth = getSizeInfo(attributes, "max-width", page.getWritableWidthTwips());
        int maxWidth = longMaxWidth > 0 && longMaxWidth <= Integer.MAX_VALUE ? (int) longMaxWidth : -1;
        if (cx > 0 && cy > 0) {
            // here we need cx & cy in emu which needs conversion from twips
            cx = UnitsOfMeasurement.twipToEMU(cx);
            cy = UnitsOfMeasurement.twipToEMU(cy);
            inline = imagePart.createImageInline(filenameHint, altText, id1, id2, cx, cy, false);
        } else {
            if (cx > 0) {
                inline = imagePart.createImageInline(filenameHint, altText, id1, id2, cx, false);
            } else {
                if (maxWidth > 0) {
                    inline = imagePart.createImageInline(filenameHint, altText, id1, id2, false, maxWidth);
                } else {
                    inline = imagePart.createImageInline(filenameHint, altText, id1, id2, false);
                }
            }
        }
        // Now add the inline in w:p/w:r/w:drawing
        org.docx4j.wml.R run = docx.createR();
        org.docx4j.wml.Drawing drawing = docx.getFactory().createDrawing();
        run.getContent().add(drawing);
        drawing.getAnchorOrInline().add(inline);
        return run;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SectionWrapper(org.docx4j.model.structure.SectionWrapper) Inline(org.docx4j.dml.wordprocessingDrawing.Inline) URISyntaxException(java.net.URISyntaxException) TocException(org.docx4j.toc.TocException) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException) PageDimensions(org.docx4j.model.structure.PageDimensions) org.docx4j.wml(org.docx4j.wml) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)

Example 4 with SectionWrapper

use of org.docx4j.model.structure.SectionWrapper in project docx4j-template by vindell.

the class Docx4J_例子2 method createFooterReference.

public void createFooterReference(WordprocessingMLPackage wordprocessingMLPackage, MainDocumentPart t, ObjectFactory factory, Relationship relationship) throws InvalidFormatException {
    List<SectionWrapper> sections = wordprocessingMLPackage.getDocumentModel().getSections();
    SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
    // There is always a section wrapper, but it might not contain a sectPr
    if (sectPr == null) {
        sectPr = factory.createSectPr();
        t.addObject(sectPr);
        sections.get(sections.size() - 1).setSectPr(sectPr);
    }
    FooterReference footerReference = factory.createFooterReference();
    footerReference.setId(relationship.getId());
    footerReference.setType(HdrFtrRef.DEFAULT);
    sectPr.getEGHdrFtrReferences().add(footerReference);
}
Also used : SectionWrapper(org.docx4j.model.structure.SectionWrapper) FooterReference(org.docx4j.wml.FooterReference) SectPr(org.docx4j.wml.SectPr)

Example 5 with SectionWrapper

use of org.docx4j.model.structure.SectionWrapper in project docx4j-template by vindell.

the class Docx4J_例子2 method createHeaderReference.

public void createHeaderReference(WordprocessingMLPackage wordprocessingMLPackage, MainDocumentPart t, ObjectFactory factory, Relationship relationship) throws InvalidFormatException {
    List<SectionWrapper> sections = wordprocessingMLPackage.getDocumentModel().getSections();
    SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
    // There is always a section wrapper, but it might not contain a sectPr
    if (sectPr == null) {
        sectPr = factory.createSectPr();
        t.addObject(sectPr);
        sections.get(sections.size() - 1).setSectPr(sectPr);
    }
    HeaderReference headerReference = factory.createHeaderReference();
    headerReference.setId(relationship.getId());
    headerReference.setType(HdrFtrRef.DEFAULT);
    sectPr.getEGHdrFtrReferences().add(headerReference);
}
Also used : SectionWrapper(org.docx4j.model.structure.SectionWrapper) HeaderReference(org.docx4j.wml.HeaderReference) SectPr(org.docx4j.wml.SectPr)

Aggregations

SectionWrapper (org.docx4j.model.structure.SectionWrapper)9 SectPr (org.docx4j.wml.SectPr)8 FooterReference (org.docx4j.wml.FooterReference)5 HeaderReference (org.docx4j.wml.HeaderReference)3 URISyntaxException (java.net.URISyntaxException)1 Inline (org.docx4j.dml.wordprocessingDrawing.Inline)1 PageDimensions (org.docx4j.model.structure.PageDimensions)1 Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)1 BinaryPartAbstractImage (org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)1 TocException (org.docx4j.toc.TocException)1 org.docx4j.wml (org.docx4j.wml)1