We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Wriring a Text in the center of a Word Document

Hi,



I want to write a Sentence: “For Training Use Only” in the center of the word document as a watermark in a 45 degree angle.

How do i do it?



Thanks,

Linu Joseph

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I already answered this question in the following thread you created earlier.

Have you tried using the code provided there?

Best regards.

Yes…i have tried but it seems the code is not in JAVA…it is throwing a syntax error…



Is there any other way?



Thanks,

Linu

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Yes, code is in C#. Here is code in java.

//open document and create DocumentBuilder

Document doc = new Document("C:\\Temp\\in.doc");

DocumentBuilder builder = new DocumentBuilder(doc);

//Create whatermark shape

Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);

watermark.getTextPath().setText("PRODUCTION_USE FOR ONLY TRAINING");

watermark.getTextPath().setFontFamily("Times New Roman");

watermark.setWidth(600);

watermark.setHeight(100);

watermark.setRotation(-45);

//uncomment the following line if you need solid black text

watermark.getFill().setColor(java.awt.Color.BLACK);

//Set position of watermark

watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);

watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

watermark.setWrapType(WrapType.NONE);

watermark.setVerticalAlignment(VerticalAlignment.CENTER);

watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);

//Create new paragraph and append watermark to this paragraph

Paragraph watermarkPar = new Paragraph(doc);

watermarkPar.appendChild(watermark);

int[] hfTypes = {HeaderFooterType.HEADER_PRIMARY,

HeaderFooterType.HEADER_FIRST,

HeaderFooterType.HEADER_EVEN};

//Now we should insert watermark into the header of each section of document

for (int sectIdx = 0; sectIdx < doc.getSections().getCount(); sectIdx++)

{

Section sect = doc.getSections().get(sectIdx);

for (int hftIdx = 0; hftIdx < hfTypes.length; hftIdx++)

{

int hftype = hfTypes[hftIdx];

if (sect.getHeadersFooters().getByHeaderFooterType(hftype) == null)

{

//Create new header

if (hftype == HeaderFooterType.HEADER_PRIMARY ||

hftype == HeaderFooterType.HEADER_FIRST && sect.getPageSetup().getDifferentFirstPageHeaderFooter() ||

hftype == HeaderFooterType.HEADER_EVEN && sect.getPageSetup().getOddAndEvenPagesHeaderFooter())

{

HeaderFooter hf = new HeaderFooter(doc, hftype);

//insert clone of watermarkPar into the header

hf.appendChild(watermarkPar.deepClone(true));

sect.getHeadersFooters().add(hf);

}

}

else

{

sect.getHeadersFooters().getByHeaderFooterType(hftype).appendChild(watermarkPar.deepClone(true));

}

}

}

doc.getViewOptions().setViewType(ViewType.PAGE_LAYOUT);

doc.save("C:\\Temp\\out.doc");

Hope this helps.

Best regards.