Not able to see watermark in a WMF file

Hi
Was trying the code attached to watermark a WMF file,it is not watermarking the file.

Attaching the code and the sample WMF file used.

wmf.zip (59.4 KB)

Thanks
Sabarish

Hi, @sabkan
We appreciate your interest in Aspose.Imaging.
We are going to check this issue today.

Could you provide your output file?
I have tested your code with your file and got the correct output file with the watermark text.
изображение.png (15,6 КБ)

@evgeniy.sidenko
Thanks. I am using the trial version of the Aspose, is that the reason?

Attaching the output file

wmf.zip (34.0 KB)

Thanks

@sabkan.
Yes, I have tried with the trial version and our copyright badge overrides your text.

ok thanks for the confirmation.

1 Like

It is possible to draw out your watermark text in the bottom or center of the image, in this case you text will be visible

ok was trying changing position to 30 from 10 like this:

	graphics.drawString(watermarkText, font, com.aspose.imaging.Color.getBlack(), 30, 30);

Wanted the watermark to appear diagonally in the center.Can you help with that?

Thanks
Sabarish

Hi, @sabkan
Please, look at the following example.

import com.aspose.imaging.*;
import com.aspose.imaging.fileformats.wmf.WmfImage;
import com.aspose.imaging.fileformats.wmf.graphics.WmfRecorderGraphics2D;
import com.aspose.imaging.imageoptions.*;

private static SizeF calculateTextSize(Image image, Font font)
{
	Graphics gr = new Graphics(image);
	return gr.measureString("Watermark Text", font, new SizeF(), StringFormat.getGenericTypographic());
}

private static void convertSizeAccordingToAngle(SizeF textSize, float angle)
{
	// Convert size from 96dpi to 72dpi
	float width = textSize.getWidth() * 72f / 96f;
	float height = textSize.getHeight() * 72f / 96f;
	Matrix mx = new Matrix();
	mx.rotate(angle);
	PointF[] points = new PointF[1];
	points[0] = new PointF(width / 2, height);
	mx.transformPoints(points);
	textSize.setWidth(points[0].getX());
	textSize.setHeight(points[0].getY());
}

public void drawWatermarkText()
{
	com.aspose.imaging.Image image = com.aspose.imaging.Image.load("Sample1.wmf");
	try
	{
		// create and initialize an instance of WmfRecorderGraphics2D class

		WmfRecorderGraphics2D graphics = WmfRecorderGraphics2D.fromWmfImage((WmfImage) image);

		// create an instance of Font. Initialize it with Font Face, Size and Style
		com.aspose.imaging.Font font = new Font("Times New Roman", 40, com.aspose.imaging.FontStyle.Bold);

		float angle = 45;

		SizeF ret = calculateTextSize(image, font);

		convertSizeAccordingToAngle(ret, angle);

		// Place the text in the center
		int offsX = (int) (image.getWidth() / 2.0 - ret.getWidth());
		int offsY = (int) (image.getHeight() / 2.0 + ret.getHeight());
		graphics.drawString("Watermark Text", font, com.aspose.imaging.Color.getBlue(), offsX, offsY, angle);

		WmfImage image2 = graphics.endRecording();
		try
		{
			String watermarkedFilePath = "Sample1-with-watermark.wmf";
			// save output to disk
			image2.save(watermarkedFilePath);
		}
		finally
		{
			image2.close();
		}
	}
	finally
	{
		image.close();
	}
}

Thanks for the detailed code.
It works fine as per my expectation.

1 Like