Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Thursday, October 27, 2016 3:40:46 PM(UTC)

sandpa  
sandpa

Groups: Registered
Posts: 5


Hi,

Is it possible to add a watermark to all pages of a given PDF file using the Document SDK (.Net) ? The watermark would be a text watermark and I would like it to be displayed diagonally across the page (or any position needed) and have an opacity and color that can be controlled. Any code snippet, class/method that points me in the right direction will be appreciated.

Thanks
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

#2 Posted : Monday, October 31, 2016 10:26:22 AM(UTC)

Aaron  
Aaron

Groups: Registered, Tech Support, Administrators
Posts: 71

Was thanked: 4 time(s) in 3 post(s)

Hi,

You can use our AnnBatesStampComposer class to add watermarks to your pages

What you would want to do is have a PNG file which contains transparent text and no background. Then create a Bates Stamp from that and add it into your files. We actually ship a BatesStamp Composer demo with the SDK. You can find the demo along with the source code for the demo here:

<Install Directory>\LEADTOOLS 19\Examples\DotNet\CS\AnnotationsBatesStampComposerDemo

In the demo, you will want to navigate to Bates Stamp -> Bates Stamp Properties which will pop up the Bates Stamp Properties dialog. Here you will want to add a new Stamp and load in your transparent PNG file and click OK:

UserPostedImage

You should now have a watermark on your pages:

UserPostedImage

Edited by moderator Monday, November 16, 2020 4:58:05 PM(UTC)  | Reason: Not specified

Aaron Brasington
Developer Support Engineer
LEAD Technologies, Inc.

LEAD Logo
 
#3 Posted : Friday, November 4, 2016 1:02:54 PM(UTC)

sandpa  
sandpa

Groups: Registered
Posts: 5


Hi Aaron,

Thanks for your reply. Is there a way to use the AnnBatesStampComposer without using the BatesStampDialog ?

My use case is like this :
I have a RasterImage generated from a document that does not have any watermarks on it. Before displaying the image to the end user (using the LeadTools JS viewer) I want to introduce a watermark on the image.

Is this possible with the AnnBatesStampComposer ?

thanks,
 
#4 Posted : Friday, November 4, 2016 2:37:11 PM(UTC)

Aaron  
Aaron

Groups: Registered, Tech Support, Administrators
Posts: 71

Was thanked: 4 time(s) in 3 post(s)

Yes, you can certainly perform this functionality without having to use the BatesStampDialog. All the properties can be added programmatically. Here is a link to our documentation which contains sample code demonstrating how this would be done:

https://www.leadtools.co...nbatesstampcomposer.html

In short, you would just need to create your AnnBatesStamp object and set your desired properties. Then add your new AnnBatesStamp to the AnnBatesStampCollection on your AnnBatesStampComposer. This process will programmatically insert your AnnBatesStamp just like the BatesStampDialog would.

Code:
   //Create AnnBatesStamp and set its properties
   AnnBatesStamp batesStamp = new AnnBatesStamp();
   batesStamp.Font = new AnnFont("Arial", 12);
   batesStamp.Foreground = AnnSolidColorBrush.Create("Red");
   batesStamp.HorizontalAlignment = AnnHorizontalAlignment.Center;
   batesStamp.VerticalAlignment = AnnVerticalAlignment.Center;
   batesStamp.Logo.Angle = 45;
   batesStamp.Logo.Font = new AnnFont("Arial", 14);
   batesStamp.Logo.Opacity = 0.5;
   batesStamp.Logo.StretchLogo = true;
   batesStamp.Logo.Picture = new AnnPicture(Path.Combine(ImagesPath.Path, "cannon.jpg"));
   //Add some elements to Bates stamp elements list
   batesStamp.Elements.Add(new AnnBatesNumber());
   batesStamp.Elements.Add(AnnBatesText.Create(" "));
   batesStamp.Elements.Add(new AnnBatesDateTime());
   batesStamp.Elements.Add(AnnBatesText.Create(" This is text element"));

   //Create AnnBatesStampComposer instance and add the created Bates stamp to it
   AnnBatesStampComposer batesStampComposer = new AnnBatesStampComposer();
   //Set the rendering engine
   AnnBatesStampComposer.RenderingEngine = new AnnWinFormsRenderingEngine();
   batesStampComposer.Stamps.Add(batesStamp);

   //Use the main automation object for your application instead of _automation we used here
   AnnAutomation automation = _automation;
   AnnContainer mainContainer = automation.Container;

   //Create Bates stamp container, set its size and mapper and attach it to the composer
   AnnContainer batesStampContainer = new AnnContainer();
   batesStampContainer.Size = mainContainer.Size;
   batesStampContainer.Mapper = mainContainer.Mapper.Clone();

   //Apply Bates Stamp to our container
   batesStampComposer.TargetContainers.Add(batesStampContainer);

   //insert the Bates stamp container below all other containers in the automation 
   automation.Containers.Insert(0, batesStampContainer);

   //print the content of this Bates stamp
   Debug.WriteLine(batesStamp.AsString(batesStampContainer)); //  the output will be "000001 1/1/0001 2:00:00 AM This is text element"

   //Render the containers
   automation.Invalidate(LeadRectD.Empty);
Aaron Brasington
Developer Support Engineer
LEAD Technologies, Inc.

LEAD Logo
 
#5 Posted : Friday, November 4, 2016 3:42:47 PM(UTC)

sandpa  
sandpa

Groups: Registered
Posts: 5


I did come across this code but I could not really use it in my ASP.Ner Web API code. What would I specify for the _automation variable ? The comment says to use the main automation object for your application. What would that be in a Web API application ?
 
#6 Posted : Monday, November 7, 2016 2:02:15 PM(UTC)

Aaron  
Aaron

Groups: Registered, Tech Support, Administrators
Posts: 71

Was thanked: 4 time(s) in 3 post(s)

The AnnAutomation that the sample code is referring to can be removed since you are working within a web server and you are not rendering the annotations to the screen. You should just need to add your watermark annotation to the AnnContainer then render the watermark to the image. In general, you would just need the following things in your use case:

- AnnBatesStampComposer
- AnnWinformsRenderingEngine (Needed to render the annotations on the RasterImage)
- AnnContainer
- AnnBatesStamp

I have updated the sample code to demonstrate exactly how you would add the watermark to your image in your web service:

Code:
using (RasterCodecs codecs = new RasterCodecs())
{
   using (RasterImage image = codecs.Load(@"C:\Users\Public\Documents\LEADTOOLS Images\Leadtools.pdf"))
   {
      // Create AnnBatesStampComposer instance and add the created Bates stamp to it
      using (AnnBatesStampComposer batesStampComposer = new AnnBatesStampComposer())
      {
         //Set the rendering engine
         AnnBatesStampComposer.RenderingEngine = new AnnWinFormsRenderingEngine();
         //Create Bates stamp container
         AnnContainer batesStampContainer = new AnnContainer();
         // Map the resolutions of the AnnContainer to match the image
         batesStampContainer.Mapper.MapResolutions(image.XResolution, image.YResolution, image.XResolution, image.YResolution);
         // Set the size of the AnnContainer based on the size of the image
         batesStampContainer.Size = batesStampContainer.Mapper.SizeToContainerCoordinates(LeadSizeD.Create(image.Width, image.Height));

         // Create AnnBatesStamp and set its properties
         using (AnnBatesStamp batesStamp = new AnnBatesStamp())
         {
            batesStamp.HorizontalAlignment = AnnHorizontalAlignment.Left;
            batesStamp.VerticalAlignment = AnnVerticalAlignment.Top;
            batesStamp.Logo.Opacity = 0.5;
            batesStamp.Logo.Angle = -45;
            batesStamp.Logo.StretchLogo = true;
            batesStamp.Logo.Picture = new AnnPicture(@"watermark.png");
            // Add the AnnBatesStamp to the collection
            batesStampComposer.Stamps.Add(batesStamp);
            // Add the AnnContainer to the collection
            batesStampComposer.TargetContainers.Add(batesStampContainer);

            // Render the AnnContainer on the image
            AnnBatesStampComposer.RenderingEngine.RenderOnImage(batesStampContainer, image);

            // Save the resulting RasterImage which contains the newly created watermark
            codecs.Save(image, @"Watermarked_PDF.pdf", RasterImageFormat.RasPdf, 0);
         }
      }
   }
}


I have also attached the watermark.png that I am using in my tests so that you can test the code yourself.

watermark.png
Aaron Brasington
Developer Support Engineer
LEAD Technologies, Inc.

LEAD Logo
 
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2024, Yet Another Forum.NET
This page was generated in 0.098 seconds.