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);
}
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);
}
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;
}
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);
}
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);
}
Aggregations