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 : Friday, April 28, 2017 1:27:43 PM(UTC)

Hadi  
Hadi

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:
https://www.leadtools.co.../adoc/annbatesstamp.html
https://www.leadtools.co...nbatesstampcomposer.html

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:
UserPostedImage

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:

Code:
[HttpPost]
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;
                  document.SaveToCache();
               }
            }
         }
      }
   }

   return documentId;
}


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

Code:
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);
         batesStampComposer.Stamps.Add(batesStamp);
         batesStampComposer.TargetContainers.Add(batesStampContainer);

         RasterImage watermarkedImage = null;
         for (int i = 1; i <= image.PageCount; i++)
         {
            image.Page = i;
            if (watermarkedImage == null)
               watermarkedImage = AnnBatesStampComposer.RenderingEngine.RenderOnImage(batesStampContainer, image).Clone();
            else
               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:
Code:
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.commands.run(lt.Documents.UI.DocumentViewerCommands.interactivePanZoom);
    _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";

    $.ajax({
        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
            lt.Documents.DocumentFactory.loadFromCache(cacheId)
                .done(function (document) {
                // Set the document in the viewer
                documentViewer.setDocument(document);
            });
        }
    });
};


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

LEAD Logo
 

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