From af0f440a47a94f509717f74d76af741f8518190f Mon Sep 17 00:00:00 2001 From: Paolo Mottadelli Date: Thu, 20 Aug 2009 15:14:00 +0000 Subject: [PATCH] first createWatermark implementation git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@806209 13f79535-47bb-0310-9956-ffa450edef68 --- .../xwpf/model/XWPFHeaderFooterPolicy.java | 102 ++++++++++++++++++ .../poi/xwpf/usermodel/XWPFParagraph.java | 2 +- .../poi/xwpf/usermodel/TestXWPFHeader.java | 18 ++++ 3 files changed, 121 insertions(+), 1 deletion(-) diff --git a/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java b/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java index 0d8a82e7cf..b6bc5f15eb 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java +++ b/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java @@ -38,12 +38,28 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtrRef; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.FtrDocument; import org.openxmlformats.schemas.wordprocessingml.x2006.main.HdrDocument; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr.Enum; +import schemasMicrosoftComOfficeOffice.CTLock; +import schemasMicrosoftComOfficeOffice.STConnectType; +import schemasMicrosoftComVml.CTFormulas; +import schemasMicrosoftComVml.CTGroup; +import schemasMicrosoftComVml.CTH; +import schemasMicrosoftComVml.CTHandles; +import schemasMicrosoftComVml.CTPath; +import schemasMicrosoftComVml.CTShape; +import schemasMicrosoftComVml.CTShapetype; +import schemasMicrosoftComVml.CTTextPath; +import schemasMicrosoftComVml.STExt; +import schemasMicrosoftComVml.STTrueFalse; + /** * A .docx file can have no headers/footers, the same header/footer * on each page, odd/even page footers, and optionally also @@ -315,4 +331,90 @@ public class XWPFHeaderFooterPolicy { } return defaultFooter; } + + + public void createWatermark(String text) { + XWPFParagraph[] pars = new XWPFParagraph[1]; + try { + pars[0] = getWatermarkParagraph(text, 1); + createHeader(DEFAULT, pars); + pars[0] = getWatermarkParagraph(text, 2); + createHeader(FIRST, pars); + pars[0] = getWatermarkParagraph(text, 3); + createHeader(EVEN, pars); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + + /* + * This is the default Watermark paragraph; the only variable is the text message + * TODO: manage all the other variables + */ + private XWPFParagraph getWatermarkParagraph(String text, int idx) { + CTP p = CTP.Factory.newInstance(); + byte[] rsidr = doc.getDocument().getBody().getPArray()[0].getRsidR(); + byte[] rsidrdefault = doc.getDocument().getBody().getPArray()[0].getRsidRDefault(); + p.setRsidP(rsidr); + p.setRsidRDefault(rsidrdefault); + CTPPr pPr = p.addNewPPr(); + pPr.addNewPStyle().setVal("Header"); + // start watermark paragraph + CTR r = p.addNewR(); + CTRPr rPr = r.addNewRPr(); + rPr.addNewNoProof(); + CTPicture pict = r.addNewPict(); + CTGroup group = CTGroup.Factory.newInstance(); + CTShapetype shapetype = group.addNewShapetype(); + shapetype.setId("_x0000_t136"); + shapetype.setCoordsize("1600,21600"); + shapetype.setSpt(136); + shapetype.setAdj("10800"); + shapetype.setPath2("m@7,0l@8,0m@5,21600l@6,21600e"); + CTFormulas formulas = shapetype.addNewFormulas(); + formulas.addNewF().setEqn("sum #0 0 10800"); + formulas.addNewF().setEqn("prod #0 2 1"); + formulas.addNewF().setEqn("sum 21600 0 @1"); + formulas.addNewF().setEqn("sum 0 0 @2"); + formulas.addNewF().setEqn("sum 21600 0 @3"); + formulas.addNewF().setEqn("if @0 @3 0"); + formulas.addNewF().setEqn("if @0 21600 @1"); + formulas.addNewF().setEqn("if @0 0 @2"); + formulas.addNewF().setEqn("if @0 @4 21600"); + formulas.addNewF().setEqn("mid @5 @6"); + formulas.addNewF().setEqn("mid @8 @5"); + formulas.addNewF().setEqn("mid @7 @8"); + formulas.addNewF().setEqn("mid @6 @7"); + formulas.addNewF().setEqn("sum @6 0 @5"); + CTPath path = shapetype.addNewPath(); + path.setTextpathok(STTrueFalse.T); + path.setConnecttype(STConnectType.CUSTOM); + path.setConnectlocs("@9,0;@10,10800;@11,21600;@12,10800"); + path.setConnectangles("270,180,90,0"); + CTTextPath shapeTypeTextPath = shapetype.addNewTextpath(); + shapeTypeTextPath.setOn(STTrueFalse.T); + shapeTypeTextPath.setFitshape(STTrueFalse.T); + CTHandles handles = shapetype.addNewHandles(); + CTH h = handles.addNewH(); + h.setPosition("#0,bottomRight"); + h.setXrange("6629,14971"); + CTLock lock = shapetype.addNewLock(); + lock.setExt(STExt.EDIT); + CTShape shape = group.addNewShape(); + shape.setId("PowerPlusWaterMarkObject" + idx); + shape.setSpid("_x0000_s102" + (4+idx)); + shape.setType("#_x0000_t136"); + shape.setStyle("position:absolute;margin-left:0;margin-top:0;width:415pt;height:207.5pt;z-index:-251654144;mso-wrap-edited:f;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin"); + shape.setWrapcoords("616 5068 390 16297 39 16921 -39 17155 7265 17545 7186 17467 -39 17467 18904 17467 10507 17467 8710 17545 18904 17077 18787 16843 18358 16297 18279 12554 19178 12476 20701 11774 20779 11228 21131 10059 21248 8811 21248 7563 20975 6316 20935 5380 19490 5146 14022 5068 2616 5068"); + shape.setFillcolor("black"); + shape.setStroked(STTrueFalse.FALSE); + CTTextPath shapeTextPath = shape.addNewTextpath(); + shapeTextPath.setStyle("font-family:"Cambria";font-size:1pt"); + shapeTextPath.setString(text); + pict.set(group); + // end watermark paragraph + return new XWPFParagraph(p); + } } diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java index 91e1c13481..58d10db401 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java @@ -43,7 +43,7 @@ public class XWPFParagraph { private StringBuffer footnoteText = new StringBuffer(); - protected XWPFParagraph(CTP prgrph) { + public XWPFParagraph(CTP prgrph) { this(prgrph, null); } diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java index a893ab1183..478f622b2d 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java @@ -17,7 +17,10 @@ package org.apache.poi.xwpf.usermodel; import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import junit.framework.TestCase; @@ -82,5 +85,20 @@ public class TestXWPFHeader extends TestCase { assertNotNull(policy.getFirstPageHeader()); assertNotNull(policy.getDefaultFooter()); } + + public void testSetWatermark() throws IOException { + XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx"); + // no header is set (yet) + XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy(); + assertNull(policy.getDefaultHeader()); + assertNull(policy.getFirstPageHeader()); + assertNull(policy.getDefaultFooter()); + + policy.createWatermark("DRAFT"); + + assertNotNull(policy.getDefaultHeader()); + assertNotNull(policy.getFirstPageHeader()); + assertNotNull(policy.getEvenPageHeader()); + } } -- 2.39.5