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



Last Go to last post Unread Go to first unread post
#1 Posted : Friday, April 28, 2017 1:27:43 PM(UTC)


Groups: Manager, Tech Support, Administrators
Posts: 218

Was thanked: 12 time(s) in 12 post(s)

Attached is a VS2017 project written in JS and C# that allows the user to watermark a document prior to loading it by using the Bates Stamp Annotation:

The project is using a modified version of the Document Viewer Demo's DocumentService as well as a modified version of the Tutorial1 project that is included with the Document Viewer Demo.

To run this project, please download and extract it to this folder from the installation:
C:\LEADTOOLS 19\Examples\JS\Documents

Here is a screenshot of the result:

Here is the relevant code:

In the FactoryController.cs file of the DocumentsService, I added a new POST method that will accept a URI. This method will take the URI and create a watermarked image out of it and save it to the Document viewer cache and then return to the client the DocumentCache ID which will then call the Document Factory's LoadFromCache method.

Service controller code:

public string ApplyWatermark(Uri uri)
   string documentId = "";

   using (RasterCodecs codecs = new RasterCodecs())
   using (WebClient client = new WebClient())
      var bytes = client.DownloadData(uri.ToString());
      using (MemoryStream ms = new MemoryStream(bytes))
         ms.Position = 0;
         codecs.Options.Load.AllPages = true;
         using (RasterImage image = codecs.Load(ms))
         using (RasterImage watermarkImage = AddWatermark(image))
            using (MemoryStream outputStream = new MemoryStream())
               codecs.Save(watermarkImage, outputStream, RasterImageFormat.RasPdfJpeg411, 24);
               var loadOptions = new LoadDocumentOptions()
                  Cache = ServiceHelper.Cache,
                  CachePolicy = ServiceHelper.CreatePolicy(),
                  MimeType = "application/pdf"
               using (var document = DocumentFactory.LoadFromStream(outputStream, loadOptions))
                  document.AutoDeleteFromCache = false;
                  documentId = document.DocumentId;

   return documentId;

Here is the relevant code to apply the watermark to each page of the RasterImage using Bates Stamping:

private RasterImage AddWatermark(RasterImage image)
   using (AnnBatesStampComposer batesStampComposer = new AnnBatesStampComposer())
      AnnBatesStampComposer.RenderingEngine = new AnnWinFormsRenderingEngine();
      AnnContainer batesStampContainer = new AnnContainer();
      batesStampContainer.Mapper.MapResolutions(image.XResolution, image.YResolution, image.XResolution, image.YResolution);
      batesStampContainer.Size = batesStampContainer.Mapper.SizeToContainerCoordinates(LeadSizeD.Create(image.Width, image.Height));

      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;

         string imageFile = HostingEnvironment.ApplicationPhysicalPath + "Resources\\Images\\watermark.png";
         batesStamp.Logo.Picture = new AnnPicture(imageFile);

         RasterImage watermarkedImage = null;
         for (int i = 1; i <= image.PageCount; i++)
            image.Page = i;
            if (watermarkedImage == null)
               watermarkedImage = AnnBatesStampComposer.RenderingEngine.RenderOnImage(batesStampContainer, image).Clone();
               watermarkedImage.AddPage(AnnBatesStampComposer.RenderingEngine.RenderOnImage(batesStampContainer, image).Clone());
         return watermarkedImage;

Here is the Client side Javascript code with the relevant Ajax call to call this method:
var _this = this;
var documentViewer = null;
window.onload = function () {
    var createOptions = new lt.Documents.UI.DocumentViewerCreateOptions();
    createOptions.viewCreateOptions.useElements = true;
    createOptions.thumbnailsCreateOptions.useElements = true;
    createOptions.thumbnailsContainer = document.getElementById("thumbnails");
    createOptions.viewContainer = document.getElementById("documentViewer");
    _this.documentViewer = lt.Documents.UI.DocumentViewerFactory.createDocumentViewer(createOptions);

    _this.documentViewer.view.preferredItemType = lt.Documents.UI.DocumentViewerItemType.svg;
    _this.documentViewer.thumbnails.imageViewer.viewHorizontalAlignment = lt.Controls.ControlAlignment.center;

    lt.Documents.DocumentFactory.serviceHost = "http://localhost:19001"; // or wherever your host is
    lt.Documents.DocumentFactory.servicePath = ""; // the path to the root of the service, which is nothing for this example
    lt.Documents.DocumentFactory.serviceApiPath = "api"; // Routing occurs at "/api", unless you change the routing in the DocumentsService

    var url = "http://demo.leadtools.com/images/pdf/leadtools.pdf";

        type: 'POST',
        contentType: "application/json; charset=utf-8",
        url: "http://localhost:19001/api/Factory/ApplyWatermark?uri=" + url,
        success: function (cacheId) {
            // Call the "LoadFromUri" and pass success and failure callbacks to the promise
                .done(function (document) {
                // Set the document in the viewer

File Attachment(s):
Watermark DocumentViewer Demo.zip (14,043kb) downloaded 266 time(s).
Hadi Chami
Developer Support Manager
LEAD Technologies, Inc.


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.

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.053 seconds.