Free Support Forum - aspose.com

Add watermark to pdf during the Doc to PDF conversion process

i want to add watermark to the PDF during the Doc to PDF conversion process,if i only want to use Aspose.Words.jdk16.jar, how should i do?

thanks!

Hello

Thanks for your inquiry. Please follow the link to learn how to insert Watermark in the document using Aspose.Words:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/howto-add-a-watermark-to-a-document.html


Best regards,

thanks!
i already read that article, but is is not meet my requirement.
i want to add watermark to pdf during the Doc to PDF conversion process, do not want to add watermark in doc first , than convert to PDF.

Hi

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

Thanks for your request. Please try using the following code:

// Open the document.

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

// Work with document.

// Insert Watermark before saving to PDF.

insertWatermarkImage(doc, "C:\\Temp\\Watermark.jpg");

doc.saveToPdf("C:\\Temp\\out.pdf");

private static void insertWatermarkImage(Document doc, String watermarkImagePath) throws Exception

{

// Create a watermark shape. This will be a shape.

// You are free to try other shape types as watermarks.

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

// Set up the the watermark.

watermark.getImageData().setImage(watermarkImagePath);

watermark.setWidth(500);

watermark.setHeight(100);

// Place the watermark in the page center.

watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);

watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

watermark.setWrapType(WrapType.NONE);

watermark.setVerticalAlignment(VerticalAlignment.CENTER);

watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);

// Create a new paragraph and append the watermark to this paragraph.

Paragraph watermarkPara = new Paragraph(doc);

watermarkPara.appendChild(watermark);

// Insert the watermark into all headers of each document section.

for (Section sect : doc.getSections())

{

// There could be up to three different headers in each section, since we want

// the watermark to appear on all pages, insert into all headers.

insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);

insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);

insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);

}

}

private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) throws Exception

{

HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);

if (header == null)

{

// There is no header of the specified type in the current section, create it.

header = new HeaderFooter(sect.getDocument(), headerType);

sect.getHeadersFooters().add(header);

}

// Insert a clone of the watermark into the header.

header.appendChild(watermarkPara.deepClone(true));

}

Best regards,

it works well, but i use below to add text watermark and can not get the correct result. please advice.

import com.aspose.words.;
import com.aspose.words.Shape;

import java.awt.
;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

public class Word2PdfByAspose2 {
public static void main(String[] args) throws Exception {
// Sample infrastructure.
Document doc = new Document(“C:\test.doc”);
// Work with document.
// Insert Watermark before saving to PDF.
// insertWatermarkImage(doc, “C:\test.jpg”);
insertWatermarkText(doc, “test”);
doc.saveToPdf(“C:\out.pdf”);
System.out.println("##########");
}

private static void insertWatermarkImage(Document doc,
String watermarkImagePath) throws Exception {

// Create a watermark shape. This will be a shape.
// You are free to try other shape types as watermarks.
Shape watermark = new Shape(doc, ShapeType.IMAGE);
// Set up the the watermark.
watermark.getImageData().setImage(watermarkImagePath);
watermark.setWidth(500);
watermark.setHeight(100);
// Place the watermark in the page center.
watermark
.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
watermark.setWrapType(WrapType.NONE);
watermark.setVerticalAlignment(VerticalAlignment.CENTER);
watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
// Create a new paragraph and append the watermark to this paragraph.
Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.appendChild(watermark);
// Insert the watermark into all headers of each document section.
for (Section sect : doc.getSections()) {
// There could be up to three different headers in each section,
// since we want
// the watermark to appear on all pages, insert into all headers.
insertWatermarkIntoHeader(watermarkPara, sect,
HeaderFooterType.HEADER_PRIMARY);
insertWatermarkIntoHeader(watermarkPara, sect,
HeaderFooterType.HEADER_FIRST);
insertWatermarkIntoHeader(watermarkPara, sect,
HeaderFooterType.HEADER_EVEN);
}
}

private static void insertWatermarkIntoHeader(Paragraph watermarkPara,
Section sect, int headerType) throws Exception {

HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(
headerType);
if (header == null){
// There is no header of the specified type in the current section,
// create it.
header = new HeaderFooter(sect.getDocument(), headerType);
sect.getHeadersFooters().add(header);
}
// Insert a clone of the watermark into the header.
header.appendChild(watermarkPara.deepClone(true));

}
//copy from examples
private static void insertWatermarkText(Document doc, String watermarkText)
throws Exception {
// Create a watermark shape. This will be a WordArt shape.
// You are free to try other shape types as watermarks.
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);

// Set up the text of the watermark.
watermark.getTextPath().setText(watermarkText);
watermark.getTextPath().setFontFamily(“Arial”);
watermark.setWidth(500);
watermark.setHeight(100);
// Text will be directed from the bottom-left to the top-right corner.
watermark.setRotation(-45);
// Remove the following two lines if you need a solid black text.
watermark.getFill().setColor(Color.GRAY); // Try LightGray to get more
// Word-style watermark
watermark.setStrokeColor(Color.GRAY); // Try LightGray to get more
// Word-style watermark

// Place the watermark in the page center.
watermark
.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
watermark.setWrapType(WrapType.NONE);
watermark.setVerticalAlignment(VerticalAlignment.CENTER);
watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);

// Create a new paragraph and append the watermark to this paragraph.
Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.appendChild(watermark);

// Insert the watermark into all headers of each document section.
for (Section sect : doc.getSections()) {
// There could be up to three different headers in each section,
// since we want
// the watermark to appear on all pages, insert into all headers.
insertWatermarkIntoHeader(watermarkPara, sect,
HeaderFooterType.HEADER_PRIMARY);
insertWatermarkIntoHeader(watermarkPara, sect,
HeaderFooterType.HEADER_FIRST);
insertWatermarkIntoHeader(watermarkPara, sect,
HeaderFooterType.HEADER_EVEN);
}
}



}

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

Thanks for your inquiry. The problem occurs because Aspose.Words for Java does not support WordArt during converting to PDF (this feature is in beta by the way). WordArt is already supported in .NET version of Aspose.Words and will be supported in Java version once conversion to PDF is out of beta.

As a temporary workaround, you can try inserted an image as a watermark (as in my previous post).

Best regards,

Hi Andrey,

Any indication by when the Java conversion to PDF will be out of BETA? Is the BETA something I can test and show to our client in the meantime? As the watermark we need to insert is based on certain properties of the document we really need to be able to insert watermark “text”.

Kind regards

Hi

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

Thanks for your inquiry. We are still working on synchronizing .NET and Java versions of Apsose.Words. You can learn more about progress here:

https://blog.aspose.com/2010/10/19/only-214-files-remain-to-autoport

In meantime, if you need to insert text watermark, you can generate image with text on the fly. For instance, see the following code:

int width = 500;

int height = 500;

GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();

// Create buffered image that supports transparency

BufferedImage bufimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

// Create Graphics2D.

Graphics2D g2d = environment.createGraphics(bufimage);

g2d.setFont(new java.awt.Font("TimesRoman", java.awt.Font.BOLD | java.awt.Font.ITALIC, 100));

g2d.setColor(Color.gray);

g2d.rotate(Math.PI / 4, 40, 50);

g2d.drawString("Watermark", 40, 50);

// For testing insert the generated image into the document.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.insertImage(bufimage);

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

Hope this helps.

Best regards,

We are happy to inform you that the first auto-ported version of Aspose.Words for Java is ready. This version supports converting documents to PDF. You can get it from here.


Best regards,
Aspose.Words team


(4)

The issues you have found earlier (filed as WORDSJAVA-35) have been fixed in this .NET update and in this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.