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

Free Support Forum - aspose.com

JPEG Watermark appears twice

Hello,

I’ve added an image (jpeg) watermark with Aspose.Pdf. The problem is that the watermark appears TWICE. One of them is placed in the right position.

I have attached the watermark and the result.

Next is the code:

public class PDFcreator
{
private Pdf pdf;
private Section sec;

/------------------------------------------------------------------------/
public PDFcreator()
{
// the Pdf
pdf = new Pdf();

// the one and only Section
sec = pdf.Sections.Add();

setSectionSetup();

setPageSetup();
}
/------------------------------------------------------------------------/
private void setSectionSetup()
{
}
/------------------------------------------------------------------------/
private void setPageSetup()
{
pdf.PageSetup.PageWidth = PageSize.A4Width;
pdf.PageSetup.PageHeight = PageSize.A4Height;

MarginInfo marginInfo = new MarginInfo();

marginInfo.Top = 56 * PDFRender.MMs_to_POINTs;
marginInfo.Bottom = 16 * PDFRender.MMs_to_POINTs;
marginInfo.Left = 24 * PDFRender.MMs_to_POINTs;
marginInfo.Right = 20 * PDFRender.MMs_to_POINTs;

sec.PageInfo.Margin = marginInfo;


pdf.PageTransitionType = PageTransitionType.Split;

}
/------------------------------------------------------------------------/
public void addText(string textContent, int leftX, int topY)
{
float leftXpoints = leftX * PDFRender.MMs_to_POINTs;
float topYpoints = topY * PDFRender.MMs_to_POINTs;

// Font info
TextInfo textInfo = new TextInfo();

textInfo.FontSize = 12;
textInfo.Alignment = AlignmentType.Justify;

// Paragraph of type = Text
Text textParagraph = new Text(textContent);

textParagraph.TextInfo = textInfo;

// Add paragraph to single section
sec.Paragraphs.Add(textParagraph);

}
/------------------------------------------------------------------------/
public void saveToFile(string fileName)
{
pdf.Save(fileName);
}
/------------------------------------------------------------------------/
public void addImage(int leftX, int topY, float zoomfactor, MemoryStream imageStream)
{
//Create an image object
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(sec);

image1.PositioningType = PositioningType.PageRelative;

image1.ImageInfo.ImageFileType = ImageFileType.Jpeg;

image1.ImageInfo.ImageStream = imageStream;


image1.Left = leftX * PDFRender.MMs_to_POINTs;
image1.Top = topY * PDFRender.MMs_to_POINTs;


//Add the image into paragraphs collection of the section
sec.Paragraphs.Add(image1);


//Set desired the image scale
image1.ImageScale = zoomfactor;
}
/------------------------------------------------------------------------/
public void addImageWaterMark(int leftX, int topY, float zoomfactor, MemoryStream imageStream, int maxWidth, int maxHeight)
{

Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
image1.ImageInfo.ImageFileType = ImageFileType.Jpeg;
image1.ImageInfo.ImageStream = imageStream;

//Add the image into paragraphs collection of the section
sec.Paragraphs.Add(image1);

//Set desired the image scale
image1.ImageScale = zoomfactor;

FloatingBox watermark1 = new FloatingBox(maxWidth * PDFRender.MMs_to_POINTs,
maxHeight * PDFRender.MMs_to_POINTs);


watermark1.PositioningType = PositioningType.PageRelative;

watermark1.Left = leftX * PDFRender.MMs_to_POINTs - 24 * PDFRender.MMs_to_POINTs; ;
watermark1.Top = topY * PDFRender.MMs_to_POINTs;

watermark1.Paragraphs.Add(image1);

pdf.Watermarks.Add(watermark1);
}
/------------------------------------------------------------------------/
}


// Then I make use of that class:

PDFcreator pdfCreator = new PDFcreator();

// Add IMAGE

string imageFilePath = testFilePath + “KroockedGrind.jpg”;

FileStream imageStream = File.Open(imageFilePath, FileMode.Open);

float zoomFactor = 0.2f;

// mm, later converted to points

pdfCreator.addImageWaterMark(176, 9, zoomFactor, PDFRender.StreamToMemoryStream(imageStream), 33,40);

pdfCreator.saveToFile(“PdfCreator_____2.pdf”);

imageStream.Flush();
imageStream.Close();

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

I have tested the issue and I am able to notice the reason why the image is being displayed twice over the Pdf. You have first added the image to Paragraphs collection of current Pdf section and then added the same image to Watermarks collection of the Pdf document. Please comment the following code line in addImageWaterMark() method to resolve the issue.

//Add the image into paragraphs collection of the section
sec.Paragraphs.Add(image1);

Please take a look, and in case of any further issue, feel free to contact.

It worked!

Thanks a lot.