Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JWord
>
Tutorial
> Insert a watermark picture
The following example shows you insert a picture as a watermark.
import com.independentsoft.office.vml.ConnectType; import com.independentsoft.office.vml.HorizontalPosition; import com.independentsoft.office.vml.Image; 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.Stroke; import com.independentsoft.office.vml.StrokeJoinStyle; 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@4@5l@4@11@9@11@9@5xe"); shapeTemplate.setPreferRelativeResize(true); Stroke stroke = new Stroke(); stroke.setJoinStyle(StrokeJoinStyle.MITER); ShapePath path = new ShapePath(); path.setEnableGradient(true); path.setConnectionPointType(ConnectType.FOUR); Lock locking = new Lock(); locking.setAspectRatio(true); shapeTemplate.getContent().add(stroke); shapeTemplate.getContent().add(path); shapeTemplate.getContent().add(locking); ShapeStyle style = new ShapeStyle(); style.setPosition(Position.ABSOLUTE); style.setLeftMargin("0"); style.setTopMargin("0"); style.setWidth("467.5pt"); style.setHeight("374pt"); style.setHorizontalPosition(HorizontalPosition.CENTER); style.setRelativeHorizontalPosition(RelativeHorizontalPosition.MARGIN); style.setVerticalPosition(VerticalPosition.CENTER); style.setRelativeVerticalPosition(RelativeVerticalPosition.MARGIN); Shape shape = new Shape(); shape.setStyle(style); shape.setID("Shape1"); shape.setTypeReference("#ShapeTemplate1"); shape.setStroked(false); shape.setAllowInCell(false); Image image = new Image("c:\\test\\draft.png"); shape.getContent().add(image); 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*