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

Free Support Forum - aspose.com

Getting existing Watermarks does not work

Hi I am using ApsosePDF for .Net version 17.3.0.0. I have already successfully added watermarks to pdf files using Aspose and everything works fine but I am not able to get the existing watermark object from those pdf files. Below is my code:


var pdfDocument = new Aspose.Pdf.Document(inputStream);
foreach (Page page in pdfDocument.Pages)
{
var hasWatermark = false;
foreach (Artifact artifact in page.Artifacts)
{
hasWatermark = artifact.Subtype == Artifact.ArtifactSubtype.Watermark;
}
if (!hasWatermark) return false;
}
return true;
page.Artifacts is always returned with count zero even when there is watermark in the page.

Hello Unnie,


Thanks for contacting support.

It would really be appreciated if you please share a sample document with which you are facing the issue, along with a sample code snippet which you are trying to add watermark inside PDF document. This will help us in testing the scenario in our environment and address it accordingly. Although please check following code snippet to add and get watermark inside PDF.


Document pdfDocument = new Document();<o:p></o:p>

System.Drawing.Image img = new Bitmap(dataDir + "aspose.jpg");

Watermark wm = new Watermark(img, new Rectangle(50, 100, 100, 200));

pdfDocument.Pages.Add().Watermark = wm;

pdfDocument.Save(dataDir + "output.pdf");

pdfDocument = new Document(dataDir + "output.pdf");

foreach (Artifact artifact in pdfDocument.Pages[1].Artifacts)

{

Console.WriteLine(artifact.Subtype + " " + artifact.Text + " " + artifact.Rectangle);

}



Best Regards,

Hi I am adding image stamp to all pages in the pdf as below:


var pdfFileStamp = new PdfFileStamp(inputStream);
var stamp = new Aspose.Pdf.Facades.Stamp();
stamp.BindImage(imagePath);
stamp.SetOrigin(Constants.imageXIndent, Constants.imageYIndent);
stamp.IsBackground = false;
stamp.Opacity = Constants.imageOpacity;
stamp.SetImageSize(Constants.imageWidth, Constants.imageHeight);
pdfFileStamp.AddStamp(stamp);

Does it have to do with that I am not using Watermark class. Is stamp and watermark the same in Aspose?

Hi Unnie,


Thanks for writing back.

Please note that adding image stamps with opacity, so that they can look like watermark, is different than that of adding watermark into PDF document. Though you can use image stamps instead of using watermark class because with image stamps, you can control many things (i.e alignment, opacity, rotation angle, etc.) but they do not add in Page.Artifacts Collection.

However in order to get image stamps, which are used as watermark, please check following code snippet.

var doc = new Document(dataDir + “input.pdf”);<o:p></o:p>

foreach (XImage image in doc.Pages[1].Resources.Images)

{

Console.WriteLine(image.Height.ToString());

}


In case of any further assistance, please feel free to contact us.


Best Regards,

Thanks for your reply. I am trying to add a watermark artifact like shown in the attachment (There will be an image and to the right there will be a text and a date below the text.
). Which class should i be using? Watermark class does not give ability to add text. Should i be using Artifact class?





Hello Unnie,


Thanks for your inquiry.

In order to add both image and text at custom positions, as watermark, you can use image and text stamps. Please check following sample code snippet where I have added image and text (at the right side of image), by setting their positions.

Document pdfDocument = new Document();<o:p></o:p>

ImageStamp imageStamp = new ImageStamp(dataDir + "aspose.jpg");

imageStamp.XIndent = 50;

imageStamp.VerticalAlignment = VerticalAlignment.Center;

imageStamp.Opacity = 0.5;

string str = "This is First Line Text";

Facades.FormattedText text = new Facades.FormattedText(str);

text.AddNewLineText(DateTime.Now.ToString());

TextStamp textStamp = new TextStamp(text);

//set text properties

textStamp.TextState.Font = FontRepository.FindFont("Arial");

textStamp.TextState.FontSize = Convert.ToSingle(12);

textStamp.TextState.FontStyle = FontStyles.Bold;

textStamp.TextState.FontStyle = FontStyles.Italic;

textStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);

textStamp.Opacity = 0.5;

textStamp.XIndent = 50 + imageStamp.Width;

textStamp.VerticalAlignment = VerticalAlignment.Center;

pdfDocument.Pages.Add();

pdfDocument.Pages[1].AddStamp(imageStamp);

pdfDocument.Pages[1].AddStamp(textStamp);

pdfDocument.Save(dataDir + "ParallelWatermark_out.pdf");



For your reference, I have attached generated output by above code as well. You may check in the attached document that date has been added into second line of the text. In case of any further assistance, please feel free to contact us.


Best Regards,

Why is there no option to set opacity in Watermark object? Currently when I add watermark image the content in that position gets hidden behind the watermark image. Is thee anyway to acheive this?

Hi Unnie,


Thanks for writing back.

As shared above, to achieve opacity, you can use ImageStamp in the PDF and as far as opacity in Watermark Class is concerned, we have generated an enhancement request as PDFNET-42691 in our issue tracking system. I have linked this thread with the request ID as well, so that you will be notified once the feature is added. We will keep you updated on the status of feature request. Please be patient and spare us little time.

We are sorry for the inconvenience.


Best Regards,