Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JWord
>
Tutorial
> Create text watermark
The example shows you how to create watermark with text, not with image.
import com.independentsoft.office.vml.ConnectType; import com.independentsoft.office.vml.ExtensionHandlingBehavior; import com.independentsoft.office.vml.Formula; import com.independentsoft.office.vml.FormulaSet; import com.independentsoft.office.vml.HorizontalPosition; import com.independentsoft.office.vml.Lock; import com.independentsoft.office.vml.Position; import com.independentsoft.office.vml.RelativeHorizontalPosition; import com.independentsoft.office.vml.RelativeVerticalPosition; import com.independentsoft.office.vml.Shape; import com.independentsoft.office.vml.ShapePath; import com.independentsoft.office.vml.ShapeStyle; import com.independentsoft.office.vml.ShapeTemplate; import com.independentsoft.office.vml.TextPath; import com.independentsoft.office.vml.VerticalPosition; import com.independentsoft.office.word.Paragraph; import com.independentsoft.office.word.Run; import com.independentsoft.office.word.VmlObject; import com.independentsoft.office.word.WordDocument; import com.independentsoft.office.word.headerFooter.Header; import com.independentsoft.office.word.sections.PageSize; import com.independentsoft.office.word.sections.Section; public class Example { public static void main(String[] args) { try { WordDocument doc = new WordDocument(); ShapeTemplate shapeTemplate = new ShapeTemplate(); shapeTemplate.setCoordinateSpaceSize("21600,21600"); shapeTemplate.setEdgePath("m@7,l@8,m@5,21600l@6,21600e"); shapeTemplate.setAdjustmentParameters("10800"); Formula formula1 = new Formula(); formula1.setEquation("sum #0 0 10800"); Formula formula2 = new Formula(); formula2.setEquation("prod #0 2 1"); Formula formula3 = new Formula(); formula3.setEquation("sum 21600 0 @1"); Formula formula4 = new Formula(); formula4.setEquation("sum 0 0 @2"); Formula formula5 = new Formula(); formula5.setEquation("sum 21600 0 @3"); Formula formula6 = new Formula(); formula6.setEquation("if @0 @3 0"); Formula formula7 = new Formula(); formula7.setEquation("if @0 21600 @1"); Formula formula8 = new Formula(); formula8.setEquation("if @0 0 @2"); Formula formula9 = new Formula(); formula9.setEquation("if @0 @4 21600"); Formula formula10 = new Formula(); formula10.setEquation("mid @5 @6"); Formula formula11 = new Formula(); formula11.setEquation("mid @8 @5"); Formula formula12 = new Formula(); formula12.setEquation("mid @7 @8"); Formula formula13 = new Formula(); formula13.setEquation("mid @6 @7"); Formula formula14 = new Formula(); formula14.setEquation("sum @6 0 @5"); FormulaSet formulas = new FormulaSet(); formulas.getFormulas().add(formula1); formulas.getFormulas().add(formula2); formulas.getFormulas().add(formula3); formulas.getFormulas().add(formula4); formulas.getFormulas().add(formula5); formulas.getFormulas().add(formula6); formulas.getFormulas().add(formula7); formulas.getFormulas().add(formula8); formulas.getFormulas().add(formula9); formulas.getFormulas().add(formula10); formulas.getFormulas().add(formula11); formulas.getFormulas().add(formula12); formulas.getFormulas().add(formula13); formulas.getFormulas().add(formula14); shapeTemplate.getContent().add(formulas); ShapePath path = new ShapePath(); path.setEnableGradient(true); path.setConnectionPointType(ConnectType.CUSTOM); path.setConnectionPointConnectAngles("270,180,90,0"); path.setConnectionPoint("@9,0;@10,10800;@11,21600;@12,10800"); path.setDisplayTextPath(true); TextPath textPath = new TextPath(); textPath.setFitPath(true); textPath.setDisplay(true); Lock locking = new Lock(); locking.setAutoShapeType(true); locking.setText(true); locking.setExtensionHandlingBehavior(ExtensionHandlingBehavior.EDITABLE); shapeTemplate.getContent().add(path); shapeTemplate.getContent().add(textPath); shapeTemplate.getContent().add(locking); ShapeStyle style = new ShapeStyle(); style.setPosition(Position.ABSOLUTE); style.setLeftMargin("0"); style.setTopMargin("0"); style.setWidth("412pt"); style.setHeight("247pt"); style.setRotation("315"); style.setHorizontalPosition(HorizontalPosition.CENTER); style.setRelativeHorizontalPosition(RelativeHorizontalPosition.MARGIN); style.setVerticalPosition(VerticalPosition.CENTER); style.setRelativeVerticalPosition(RelativeVerticalPosition.MARGIN); Shape shape = new Shape(); shape.setID("Object123"); shape.setStyle(style); ShapeStyle textStyle = new ShapeStyle(); textStyle.setFontFamily("Calibri"); textStyle.setFontSize("1pt"); TextPath textPath2 = new TextPath(); textPath2.setText("DRAFT"); textPath2.setStyle(textStyle); shape.getContent().add(textPath2); VmlObject vmlObject = new VmlObject(); vmlObject.getContent().add(shapeTemplate); vmlObject.getContent().add(shape); Run headerRun = new Run(); headerRun.add(vmlObject); Paragraph headerParagraph = new Paragraph(); headerParagraph.add(headerRun); Header header = new Header(); header.add(headerParagraph); Section section = new Section(); section.setPageSize(new PageSize(12240, 15840)); //8.5 x 11 in section.setHeader(header); doc.getBody().setSection(section); Run run = new Run(); run.addText("Hello Word!"); Paragraph paragraph = new Paragraph(); paragraph.add(run); doc.getBody().add(paragraph); doc.save("c:\\test\\output.docx", true); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Need help? Ask our developers:
Name*
Email*
Message*