Page 1 of 1

Creating watermark programatically

PostPosted: Tue Mar 25, 2014 9:59 pm
by sunny8875
Hi,

I am trying to generate a .docx document programatically, Can any one please let me know how to add watermark(say for example a draft watermark) to the document that is being created programatically.

Thanks in advance.

Re: Creating watermark programatically

PostPosted: Tue Mar 25, 2014 11:07 pm
by jason
As usual, create a sample docx and upload it to the webapp to generate code.

IIRC, the watermark is place in the header, so be sure to look at those.

Re: Creating watermark programatically

PostPosted: Wed Aug 13, 2014 10:50 pm
by nicky
Hi,

i used some sample code to add an image to the header... it worked :)

But i have one problem which i couldn't solve => the image is in the text flow, so everything is moving if the picture is too big. I want that the picture is behind the text... but i didn't find any solution to do this.

And where can i find this webapp to generate code?

Thank you for your help.

Best regards

Code: Select all
package play;

import org.docx4j.dml.CTGraphicalObjectFrameLocking;
import org.docx4j.dml.CTNonVisualGraphicFrameProperties;
import org.docx4j.dml.CTOfficeArtExtension;
import org.docx4j.dml.CTOfficeArtExtensionList;
import org.docx4j.dml.wordprocessingDrawing.CTEffectExtent;
import org.docx4j.dml.wordprocessingDrawing.Inline;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.Part;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.Hdr;
import org.docx4j.wml.HdrFtrRef;
import org.docx4j.wml.HeaderReference;
import org.docx4j.wml.ObjectFactory;
import org.docx4j.wml.SectPr;

public class Watermark {
   
   public WordprocessingMLPackage addWatermark(WordprocessingMLPackage wordMLPackage, byte[] image) throws Exception{
      Relationship relationship = createHeaderPart(wordMLPackage, image);

      createHeaderReference(wordMLPackage, relationship);
      
      return wordMLPackage;
   }
   
   private ObjectFactory objectFactory = new ObjectFactory();
   
   public Relationship createHeaderPart(WordprocessingMLPackage wordprocessingMLPackage, byte[] image) throws Exception {

      HeaderPart headerPart = new HeaderPart();
      headerPart.setPackage(wordprocessingMLPackage);
      headerPart.setJaxbElement(getHdr(wordprocessingMLPackage, headerPart, image));
      return wordprocessingMLPackage.getMainDocumentPart().addTargetPart(headerPart);

   }

   public Hdr getHdr(WordprocessingMLPackage wordprocessingMLPackage, Part sourcePart, byte[] image) throws Exception {

      Hdr hdr = objectFactory.createHdr();
      //.getEGBlockLevelElts()
      hdr.getContent().add(
            newImage(wordprocessingMLPackage, sourcePart, image, "filename", "alttext", 1, 2));
      return hdr;

   }

   public org.docx4j.wml.P newImage(WordprocessingMLPackage wordMLPackage, Part sourcePart, byte[] bytes,
         String filenameHint, String altText, int id1, int id2) throws Exception {

      BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, sourcePart, bytes);
      
      Inline inline = imagePart.createImageInline(filenameHint, altText, id1, id2, false);
            
      org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();
      
      org.docx4j.wml.P p = factory.createP();
      org.docx4j.wml.R run = factory.createR();
      p.getContent().add(run);

      org.docx4j.wml.Drawing drawing = factory.createDrawing();
      run.getContent().add(drawing);
      
      drawing.getAnchorOrInline().add(inline);
      
      return p;
   }

   public void createHeaderReference(WordprocessingMLPackage wordprocessingMLPackage, Relationship relationship)
         throws InvalidFormatException {

      SectPr sectPr = objectFactory.createSectPr();

      HeaderReference headerReference = objectFactory.createHeaderReference();
      headerReference.setId(relationship.getId());
      headerReference.setType(HdrFtrRef.DEFAULT);
      sectPr.getEGHdrFtrReferences().add(headerReference);// add header or
      // footer references

      wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr);

   }

}

Re: Creating watermark programatically

PostPosted: Thu Aug 14, 2014 12:55 am
by nicky
I also tried to work by the XmlUtils.unmarshalString - method... but there i receive an exception.

Here is the source:
Code: Select all
   public static void createParagraph() throws JAXBException {
      String openXML = "<w:p xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w10=\"urn:schemas-microsoft-com:office:word\">"
            + "<w:bookmarkStart w:name=\"_GoBack\" w:id=\"0\"/>"
            + "<w:r>"
            + "<w:rPr>"
            + "<w:noProof/>"
            + "<w:lang w:eastAsia=\"de-AT\"/>"
            + "</w:rPr>"
            + "<w:drawing>"
            + "<wp:anchor distT=\"0\" distB=\"0\" distL=\"114300\" distR=\"114300\" simplePos=\"false\" relativeHeight=\"251658240\" behindDoc=\"true\" locked=\"false\" layoutInCell=\"true\" allowOverlap=\"true\">"
            + "<wp:simplePos x=\"0\" y=\"0\"/>" + "<wp:positionH relativeFrom=\"column\">"
            + "<wp:posOffset>-4445</wp:posOffset>" + "</wp:positionH>"
            + "<wp:positionV relativeFrom=\"paragraph\">" + "<wp:posOffset>-1905</wp:posOffset>"
            + "</wp:positionV>" + "<wp:extent cx=\"5760720\" cy=\"8148697\"/>"
            + "<wp:effectExtent l=\"0\" t=\"0\" r=\"0\" b=\"5080\"/>" + "<wp:wrapNone/>"
            + "<wp:docPr id=\"1\" name=\"filename\" descr=\"alttext\"/>" + "<wp:cNvGraphicFramePr>"
            + "<a:graphicFrameLocks noChangeAspect=\"true\"/>" + "</wp:cNvGraphicFramePr>" + "<a:graphic>"
            + "<a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" + "<pic:pic>"
            + "<pic:nvPicPr>" + "<pic:cNvPr id=\"2\" name=\"filename\"/>" + "<pic:cNvPicPr/>" + "</pic:nvPicPr>"
            + "<pic:blipFill>" + "<a:blip cstate=\"print\" r:embed=\"rId1\">" + "<a:extLst>"
            + "<a:ext uri=\"{28A0092B-C50C-407E-A947-70E740481C1C}\">" + "<a14:useLocalDpi val=\"0\"/>"
            + "</a:ext>" + "</a:extLst>" + "</a:blip>" + "<a:stretch>" + "<a:fillRect/>" + "</a:stretch>"
            + "</pic:blipFill>" + "<pic:spPr>" + "<a:xfrm>" + "<a:off x=\"0\" y=\"0\"/>"
            + "<a:ext cx=\"5760720\" cy=\"8148697\"/>" + "</a:xfrm>" + "<a:prstGeom prst=\"rect\">" + "<a:avLst/>"
            + "</a:prstGeom>" + "</pic:spPr>" + "</pic:pic>" + "</a:graphicData>" + "</a:graphic>" + "</wp:anchor>"
            + "</w:drawing>" + "</w:r>" + "<w:bookmarkEnd w:id=\"0\"/>" + "</w:p>";
      P p = (P) XmlUtils.unmarshalString(openXML);
   }

Here the exception:
Code: Select all
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 510; Präfix "wp" für Element "wp:anchor" ist nicht gebunden.
   at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
   at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
   at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
   at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
   at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
   at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
   at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
   at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
   at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
   at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
   at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
   at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
   at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
   at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
   at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
   at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
   at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
   at org.docx4j.XmlUtils.unmarshalString(XmlUtils.java:381)
   at org.docx4j.XmlUtils.unmarshalString(XmlUtils.java:361)
   at play.Watermark.createParagraph(Watermark.java:113)
   at play.Playground.main(Playground.java:13)
Exception in thread "main" javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 510; Präfix "wp" für Element "wp:anchor" ist nicht gebunden.]
   at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
   at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
   at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
   at org.docx4j.XmlUtils.unmarshalString(XmlUtils.java:381)
   at org.docx4j.XmlUtils.unmarshalString(XmlUtils.java:361)
   at play.Watermark.createParagraph(Watermark.java:113)
   at play.Playground.main(Playground.java:13)
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 510; Präfix "wp" für Element "wp:anchor" ist nicht gebunden.
   at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
   at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
   ... 8 more

Re: Creating watermark programatically

PostPosted: Thu Aug 14, 2014 2:47 am
by nicky
And one more try... but also failed:

Code: Select all
public static Relationship nextTest(WordprocessingMLPackage wordMLPackage, byte[] image) throws Exception {

      HeaderPart headerPart = new HeaderPart();
      headerPart.setPackage(wordMLPackage);
      
      ObjectFactory objectFactory = new ObjectFactory();
      
      Hdr hdr = objectFactory.createHdr();
      
      BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, headerPart,
            image);

      Relationship rel = headerPart.addTargetPart(imagePart);
      
      java.util.HashMap<String, String> mappingsBG = new java.util.HashMap<String, String>();

      mappingsBG.put("rID", rel.getId());

      String openXML = "<w:p xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w10=\"urn:schemas-microsoft-com:office:word\">"
            + "<w:pPr>"
            + "<w:pStyle w:val=\"Kopfzeile\"/>"
            + "</w:pPr>"
            + "<w:r>"
            + "<w:rPr>"
            + "<w:noProof/>"
            + "<w:lang w:eastAsia=\"de-AT\"/>"
            + "</w:rPr>"
            + "<w:pict>"
            + "<v:shapetype stroked=\"f\" filled=\"f\" o:spt=\"75.0\" o:preferrelative=\"t\" path=\"m@4@5l@4@11@9@11@9@5xe\" coordsize=\"21600,21600\" id=\"_x0000_t75\">"
            + "<v:stroke joinstyle=\"miter\"/>"
            + "<v:formulas>"
            + "<v:f eqn=\"if lineDrawn pixelLineWidth 0\"/>"
            + "<v:f eqn=\"sum @0 1 0\"/>"
            + "<v:f eqn=\"sum 0 0 @1\"/>"
            + "<v:f eqn=\"prod @2 1 2\"/>"
            + "<v:f eqn=\"prod @3 21600 pixelWidth\"/>"
            + "<v:f eqn=\"prod @3 21600 pixelHeight\"/>"
            + "<v:f eqn=\"sum @0 0 1\"/>"
            + "<v:f eqn=\"prod @6 1 2\"/>"
            + "<v:f eqn=\"prod @7 21600 pixelWidth\"/>"
            + "<v:f eqn=\"sum @8 21600 0\"/>"
            + "<v:f eqn=\"prod @7 21600 pixelHeight\"/>"
            + "<v:f eqn=\"sum @10 21600 0\"/>"
            + "</v:formulas>"
            + "<v:path gradientshapeok=\"t\" o:connecttype=\"rect\" o:extrusionok=\"f\"/>"
            + "<o:lock aspectratio=\"t\" v:ext=\"edit\"/>"
            + "</v:shapetype>"
            + "<v:shape type=\"#_x0000_t75\" style=\"position:absolute;margin-left:0;margin-top:0;width:453.55pt;height:290.85pt;z-index:-251656192;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin\" id=\"WordPictureWatermark2001574948\" o:spid=\"_x0000_s2051\" o:allowincell=\"f\">"
            + "<v:imagedata o:title=\"cia\" r:id=\"${rID}\"/>" + "</v:shape>" + "</w:pict>" + "</w:r>" + "</w:p>";
      
      P p = (P) org.docx4j.XmlUtils.unmarshallFromTemplate(openXML, mappingsBG);
      //P p = (P) XmlUtils.unmarshalString(openXML);
      hdr.getContent().add(p);
      
      headerPart.setJaxbElement(hdr);
      return wordMLPackage.getMainDocumentPart().addTargetPart(headerPart);
   }


Exception:
Code: Select all
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1392; Präfix "r" für Attribut "r:id", das mit Elementtyp "v:imagedata" verknüpft ist, ist nicht gebunden.
   at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
   at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
   at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
   at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
   at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
   at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
   at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
   at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
   at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
   at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
   at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
   at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
   at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
   at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
   at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
   at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
   at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
   at org.docx4j.XmlUtils.unmarshalString(XmlUtils.java:381)
   at org.docx4j.XmlUtils.unmarshallFromTemplate(XmlUtils.java:430)
   at org.docx4j.XmlUtils.unmarshallFromTemplate(XmlUtils.java:423)
   at play.Watermark.nextTest(Watermark.java:255)
   at play.Playground.main(Playground.java:21)
Exception in thread "main" javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1392; Präfix "r" für Attribut "r:id", das mit Elementtyp "v:imagedata" verknüpft ist, ist nicht gebunden.]
   at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
   at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
   at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
   at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
   at org.docx4j.XmlUtils.unmarshalString(XmlUtils.java:381)
   at org.docx4j.XmlUtils.unmarshallFromTemplate(XmlUtils.java:430)
   at org.docx4j.XmlUtils.unmarshallFromTemplate(XmlUtils.java:423)
   at play.Watermark.nextTest(Watermark.java:255)
   at play.Playground.main(Playground.java:21)
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1392; Präfix "r" für Attribut "r:id", das mit Elementtyp "v:imagedata" verknüpft ist, ist nicht gebunden.
   at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
   at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
   ... 9 more

Re: Creating watermark programatically

PostPosted: Thu Aug 14, 2014 2:56 am
by nicky
But this one worked... at least i thought it works... no exception but the worddocument is without image... may be i have added the image the wrong way...

Code: Select all
public static Relationship nextTest(WordprocessingMLPackage wordMLPackage, byte[] image) throws Exception {

      HeaderPart headerPart = new HeaderPart();
      headerPart.setPackage(wordMLPackage);
      
      ObjectFactory objectFactory = new ObjectFactory();
      
      Hdr hdr = objectFactory.createHdr();
      
      BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, headerPart,
            image);

      Relationship rel = headerPart.addTargetPart(imagePart);
      
      java.util.HashMap<String, String> mappingsBG = new java.util.HashMap<String, String>();

      mappingsBG.put("rID", rel.getId());

      String openXML = "<w:p xmlns:wpc=\"http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas\" xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:wp14=\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:w10=\"urn:schemas-microsoft-com:office:word\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\" xmlns:wpg=\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\" xmlns:wpi=\"http://schemas.microsoft.com/office/word/2010/wordprocessingInk\" xmlns:wne=\"http://schemas.microsoft.com/office/word/2006/wordml\" xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\" mc:Ignorable=\"w14 wp14\">"
            + "<w:pPr>"
            + "<w:pStyle w:val=\"Kopfzeile\"/>"
            + "</w:pPr>"
            + "<w:r>"
            + "<w:rPr>"
            + "<w:noProof/>"
            + "<w:lang w:eastAsia=\"de-AT\"/>"
            + "</w:rPr>"
            + "<w:pict>"
            + "<v:shapetype stroked=\"f\" filled=\"f\" o:spt=\"75.0\" o:preferrelative=\"t\" path=\"m@4@5l@4@11@9@11@9@5xe\" coordsize=\"21600,21600\" id=\"_x0000_t75\">"
            + "<v:stroke joinstyle=\"miter\"/>"
            + "<v:formulas>"
            + "<v:f eqn=\"if lineDrawn pixelLineWidth 0\"/>"
            + "<v:f eqn=\"sum @0 1 0\"/>"
            + "<v:f eqn=\"sum 0 0 @1\"/>"
            + "<v:f eqn=\"prod @2 1 2\"/>"
            + "<v:f eqn=\"prod @3 21600 pixelWidth\"/>"
            + "<v:f eqn=\"prod @3 21600 pixelHeight\"/>"
            + "<v:f eqn=\"sum @0 0 1\"/>"
            + "<v:f eqn=\"prod @6 1 2\"/>"
            + "<v:f eqn=\"prod @7 21600 pixelWidth\"/>"
            + "<v:f eqn=\"sum @8 21600 0\"/>"
            + "<v:f eqn=\"prod @7 21600 pixelHeight\"/>"
            + "<v:f eqn=\"sum @10 21600 0\"/>"
            + "</v:formulas>"
            + "<v:path gradientshapeok=\"t\" o:connecttype=\"rect\" o:extrusionok=\"f\"/>"
            + "<o:lock aspectratio=\"t\" v:ext=\"edit\"/>"
            + "</v:shapetype>"
            + "<v:shape type=\"#_x0000_t75\" style=\"position:absolute;margin-left:0;margin-top:0;width:453.55pt;height:290.85pt;z-index:-251656192;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin\" id=\"WordPictureWatermark2001574948\" o:spid=\"_x0000_s2051\" o:allowincell=\"f\">"
            + "<v:imagedata o:title=\"cia\" r:id=\"${rID}\"/>" + "</v:shape>" + "</w:pict>" + "</w:r>" + "</w:p>";
      
      P p = (P) org.docx4j.XmlUtils.unmarshallFromTemplate(openXML, mappingsBG);
      //P p = (P) XmlUtils.unmarshalString(openXML);
      hdr.getContent().add(p);
      
      headerPart.setJaxbElement(hdr);
      return wordMLPackage.getMainDocumentPart().addTargetPart(headerPart);
   }

Re: Creating watermark programatically

PostPosted: Thu Aug 14, 2014 11:33 am
by jason
Präfix "wp" für Element "wp:anchor" ist nicht gebunden.


Declare the namespace in your string

public static Relationship nextTest(WordprocessingMLPackage wordMLPackage, byte[] image) throws Exception {

HeaderPart headerPart = new HeaderPart();


You still need to add headerPart to wordMLPackage; see the relevant sample (ie in src/samples on GitHub).

Re: Creating watermark programatically

PostPosted: Thu Sep 17, 2015 10:09 am
by AndreasN
Hi,

I tested the watermark example with a docx template which has already a headerpart.

https://github.com/plutext/docx4j/blob/master/src/samples/docx4j/org/docx4j/samples/Watermark.java


As there was no text visible of the watermark in the processed template file, I played around with some settings, especially the type of the created headerReference object and got it nearly:

Code: Select all
HeaderReference headerReference = factory.createHeaderReference();
headerReference.setId(relationship.getId());
headerReference.setType(HdrFtrRef.FIRST);


The problem now is that the document headers margin / position shifts down for 20-30 px.

Maybe the already existing SectionWrapper properties (SectPr) and within the existing headerreferences (sectPr.getEGHdrFtrReferences()) needs to be changed in some way?


Second item of interest: How to bring the watermark in front of all body content?

I changed the XML a bit and the background text is now partially shown in front of existing table objects in the processed template - but unfortunately not in front of filled table cells in that tables.

Code: Select all
<v:shape fillcolor=\"red\" id=\"PowerPlusWaterMarkObject357476642\" o:allowincell=\"f\" o:spid=\"_x0000_s2049\" stroked=\"f\" style=\"position:absolute;margin-left:0;margin-top:0;width:527.85pt;height:131.95pt;rotation:315;z-index:0;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:top;mso-position-vertical-relative:margin\" type=\"#_x0000_t136\">" + "<v:fill opacity=\".6\"/>



Kind regards,
Andreas

Re: Creating watermark programatically

PostPosted: Thu Sep 17, 2015 6:22 pm
by jason
One way to proceed is to open your generated docx in Word and edit it to suit using Word.

Then you examine the resulting XML to determine what you need to set in your docx4j code.

If you can't tell what you need to change or how to change it, feel free to post your generated docx and the desired-state docx.

Re: Creating watermark programatically

PostPosted: Fri Sep 18, 2015 1:20 am
by AndreasN
Hi,

I updated the example and add an attachment to illustrate how it works with an already given header.

The tiny offset of the headline after adding the watermark is ok to me, there will be a break added in the content area, don't know why.

The overlay of the body content, especially with shaded table cells, is probably the nature of the different areas / layers and can't be manipulated (so far I read the specs and reviewed the existing properties) - http://officeopenxml.com/WPtableCellPro ... hading.php

@Jason: I used your great analysis tool "Docx4j Helper for Word" - thx again for contributing.

Kind regards,
Andreas

Re: Creating watermark programatically

PostPosted: Tue Oct 27, 2015 7:59 pm
by AndreasN
Hi,

I did some minor changes to my contributed example class:
- the watermark is now rendered on all document pages
- there is a quantifier to define the number of repeats on each page

For the non-word conform watermark hack in front of all, there is an approach to put the text / image into a table in the first row and make this as head row; in principle it works but I failed when the document has more than one page as I guess the page breaks needs to be considered.

Jason, thank you for support.

Kind regards,
Andreas

Re: Creating watermark programatically

PostPosted: Fri Dec 18, 2015 9:51 pm
by AndreasN
Hi,

I updated the watermark example to get rid of existing header references of type FIRST, before I create a new header reference... I noticed in one use case I have that headers got doubled when documents are merged; maybe if first document header differs might probably have an impact too.

It's experimental code, maybe someone find it useful.

Kind regards,
Andreas

Re: Creating watermark programatically

PostPosted: Mon Feb 15, 2016 7:58 pm
by AndreasN
Hi,

Finally I implement a solution which works as expected. The only requirement on the document is that a header part exists.

Cheers,
Andreas