package docx.util; import java.util.List; import javax.xml.bind.JAXBException; import org.docx4j.XmlUtils; import org.docx4j.jaxb.Context; import org.docx4j.model.structure.SectionWrapper; import org.docx4j.openpackaging.exceptions.Docx4JException; import org.docx4j.openpackaging.exceptions.InvalidFormatException; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart; import org.docx4j.relationships.Relationship; import org.docx4j.wml.HdrFtrRef; import org.docx4j.wml.HeaderReference; import org.docx4j.wml.ObjectFactory; import org.docx4j.wml.P; import org.docx4j.wml.SectPr; /** * The Class WatermarkUtil. */ public class WatermarkUtil { private static ObjectFactory wmlObjectFactory = Context.getWmlObjectFactory(); public static void addWaterMarkOnDocument(final WordprocessingMLPackage wmlPackage, final String backgroundTxt) throws JAXBException, Docx4JException { Relationship relationship = createHeaderPart(wmlPackage, backgroundTxt); createHeaderReference(wmlPackage, relationship); } /** * Creates the header part. * * @param wordprocessingMLPackage the wordprocessing ml package * @param backgroundTxt the background txt * @return the relationship * @throws JAXBException the JAXB exception * @throws InvalidFormatException the invalid format exception */ private static Relationship createHeaderPart(final WordprocessingMLPackage wordprocessingMLPackage, final String backgroundTxt) throws JAXBException, InvalidFormatException { HeaderPart headerPart = wordprocessingMLPackage.getHeaderFooterPolicy().getDefaultHeader(); headerPart.getContent().add(getPForHdr(backgroundTxt)); return headerPart.getSourceRelationship(); } /** * Creates the header reference. * * @param wordprocessingMLPackage the wordprocessing ml package * @param relationship the relationship * @throws InvalidFormatException the invalid format exception */ private static void createHeaderReference(final WordprocessingMLPackage wordprocessingMLPackage, final Relationship relationship) throws InvalidFormatException { List sections = wordprocessingMLPackage.getDocumentModel().getSections(); SectPr sectPr = null; for (int i = 0; i < sections.size(); i++) { int sectionPos = i; sectPr = sections.get(sectionPos).getSectPr(); if (sectPr != null) { boolean isRemoved = removeFirstHeaderRefInSectionIfExists(sectPr); } } for (int i = 0; i < sections.size(); i++) { int sectionPos = i; sectPr = sections.get(sectionPos).getSectPr(); if (sectPr == null) { sectPr = wmlObjectFactory.createSectPr(); wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr); sections.get(sectionPos).setSectPr(sectPr); } HeaderReference headerReference = createHeaderReferenceTypeFirst(sectPr, relationship.getId()); sectPr.getEGHdrFtrReferences().add(headerReference); break; } } /** * Creates the header reference type first. * * @param sectPr the sect pr * @param relId the rel id * @return the header reference */ private static HeaderReference createHeaderReferenceTypeFirst(final SectPr sectPr, final String relId) { HeaderReference headerReference = wmlObjectFactory.createHeaderReference(); headerReference.setId(relId); headerReference.setType(HdrFtrRef.FIRST); headerReference.setParent(sectPr); return headerReference; } /** * Removes the first header ref in section if exists. * * @param sectPr the sect pr * @return true, if successful */ private static boolean removeFirstHeaderRefInSectionIfExists(final SectPr sectPr) { HeaderReference headRef = null; for (org.docx4j.wml.CTRel rel : sectPr.getEGHdrFtrReferences()) { if (rel instanceof HeaderReference) { headRef = (HeaderReference) rel; if (headRef.getType() == HdrFtrRef.FIRST) { break; } } } if (headRef != null) { return sectPr.getEGHdrFtrReferences().remove(headRef); } return false; } /** * Gets the p for hdr. * * @param backgroundTxt the background txt * @return the p for hdr * @throws JAXBException the JAXB exception */ private static P getPForHdr(final String backgroundTxt) throws JAXBException { String joinToken = " "; int quantifier = 5; String txtToken = ""; for (int i = 0; i < quantifier; i++) { txtToken += backgroundTxt + joinToken; } txtToken = txtToken.substring(0, txtToken.length() - joinToken.length()); String bounds = "margin-left:-3pt;margin-top:-20pt;width:518pt;height:750pt"; String txtColor = "#ddd8c2"; StringBuffer xmlBuf = new StringBuffer(); xmlBuf.append( ""); xmlBuf.append( ""); xmlBuf.append( ""); xmlBuf.append( ""); xmlBuf.append( ""); xmlBuf.append( ""); xmlBuf.append( ""); xmlBuf.append( ""); xmlBuf.append(""); return (P) XmlUtils.unmarshalString(xmlBuf.toString()); } }