找回密码
 立即注册

QQ登录

只需一步,快速开始

Richard.Ma 讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2017-7-17 09:37  /   查看:2749  /  回复:0
本帖最后由 Richard.Ma 于 2017-7-17 09:43 编辑

附件是一个用JS和C#编写的VS2017项目,允许用户在使用Bates Stamp Annotation加载文档之前对其进行水印:
https://www.leadtools.com/help/l ... /annbatesstamp.html
https://www.leadtools.com/help/l ... sstampcomposer.html

该项目正在使用Document Viewer Demo的DocumentService的修改版本,以及Document Viewer演示中随附的Tutorial1项目的修改版本。

要运行此项目,请从安装中下载并将其解压缩到此文件夹:
C:\ LEADTOOLS 19 \ Examples \ JS \ Documents

运行结果截图


以下是相关代码:

在DocumentsService的FactoryController.cs文件中,我添加了一个新的接口URI的POST方法。 该方法将使用URI并创建一个水印图像,并将其保存到文档查看器缓存中,然后将客户端返回给DocumentCache ID,然后将其称为Document Factory的LoadFromCache方法。

服务控制器代码:
  1. [HttpPost]
  2. public string ApplyWatermark(Uri uri)
  3. {
  4.    string documentId = "";

  5.    using (RasterCodecs codecs = new RasterCodecs())
  6.    using (WebClient client = new WebClient())
  7.    {
  8.       var bytes = client.DownloadData(uri.ToString());
  9.       using (MemoryStream ms = new MemoryStream(bytes))
  10.       {
  11.          ms.Position = 0;
  12.          codecs.Options.Load.AllPages = true;
  13.          using (RasterImage image = codecs.Load(ms))
  14.          using (RasterImage watermarkImage = AddWatermark(image))
  15.          {
  16.             using (MemoryStream outputStream = new MemoryStream())
  17.             {
  18.                codecs.Save(watermarkImage, outputStream, RasterImageFormat.RasPdfJpeg411, 24);
  19.                var loadOptions = new LoadDocumentOptions()
  20.                {
  21.                   Cache = ServiceHelper.Cache,
  22.                   CachePolicy = ServiceHelper.CreatePolicy(),
  23.                   MimeType = "application/pdf"
  24.                };
  25.                using (var document = DocumentFactory.LoadFromStream(outputStream, loadOptions))
  26.                {
  27.                   document.AutoDeleteFromCache = false;
  28.                   documentId = document.DocumentId;
  29.                   document.SaveToCache();
  30.                }
  31.             }
  32.          }
  33.       }
  34.    }

  35.    return documentId;
  36. }
复制代码
以下是使用Bates Stamping将水印应用于RasterImage的每个页面的相关代码:
  1. private RasterImage AddWatermark(RasterImage image)
  2. {
  3.    using (AnnBatesStampComposer batesStampComposer = new AnnBatesStampComposer())
  4.    {
  5.       AnnBatesStampComposer.RenderingEngine = new AnnWinFormsRenderingEngine();
  6.       AnnContainer batesStampContainer = new AnnContainer();
  7.       batesStampContainer.Mapper.MapResolutions(image.XResolution, image.YResolution, image.XResolution, image.YResolution);
  8.       batesStampContainer.Size = batesStampContainer.Mapper.SizeToContainerCoordinates(LeadSizeD.Create(image.Width, image.Height));

  9.       using (AnnBatesStamp batesStamp = new AnnBatesStamp())
  10.       {
  11.          batesStamp.HorizontalAlignment = AnnHorizontalAlignment.Left;
  12.          batesStamp.VerticalAlignment = AnnVerticalAlignment.Top;
  13.          batesStamp.Logo.Opacity = 0.5;
  14.          batesStamp.Logo.Angle = -45;
  15.          batesStamp.Logo.StretchLogo = true;

  16.          string imageFile = HostingEnvironment.ApplicationPhysicalPath + "Resources\\Images\\watermark.png";
  17.          batesStamp.Logo.Picture = new AnnPicture(imageFile);
  18.          batesStampComposer.Stamps.Add(batesStamp);
  19.          batesStampComposer.TargetContainers.Add(batesStampContainer);

  20.          RasterImage watermarkedImage = null;
  21.          for (int i = 1; i <= image.PageCount; i++)
  22.          {
  23.             image.Page = i;
  24.             if (watermarkedImage == null)
  25.                watermarkedImage = AnnBatesStampComposer.RenderingEngine.RenderOnImage(batesStampContainer, image).Clone();
  26.             else
  27.                watermarkedImage.AddPage(AnnBatesStampComposer.RenderingEngine.RenderOnImage(batesStampContainer, image).Clone());
  28.          }
  29.          return watermarkedImage;
  30.       }
  31.    }
  32. }
复制代码
这里是客户端的Javascript代码与相关的Ajax调用来调用这个方法:
  1. var _this = this;
  2. var documentViewer = null;
  3. window.onload = function () {
  4.     var createOptions = new lt.Documents.UI.DocumentViewerCreateOptions();
  5.     createOptions.viewCreateOptions.useElements = true;
  6.     createOptions.thumbnailsCreateOptions.useElements = true;
  7.     createOptions.thumbnailsContainer = document.getElementById("thumbnails");
  8.     createOptions.viewContainer = document.getElementById("documentViewer");
  9.     _this.documentViewer = lt.Documents.UI.DocumentViewerFactory.createDocumentViewer(createOptions);

  10.     _this.documentViewer.commands.run(lt.Documents.UI.DocumentViewerCommands.interactivePanZoom);
  11.     _this.documentViewer.view.preferredItemType = lt.Documents.UI.DocumentViewerItemType.svg;
  12.     _this.documentViewer.thumbnails.imageViewer.viewHorizontalAlignment = lt.Controls.ControlAlignment.center;

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

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

  17.     $.ajax({
  18.         type: 'POST',
  19.         contentType: "application/json; charset=utf-8",
  20.         url: "http://localhost:19001/api/Factory/ApplyWatermark?uri=" + url,
  21.         success: function (cacheId) {
  22.             // Call the "LoadFromUri" and pass success and failure callbacks to the promise
  23.             lt.Documents.DocumentFactory.loadFromCache(cacheId)
  24.                 .done(function (document) {
  25.                 // Set the document in the viewer
  26.                 documentViewer.setDocument(document);
  27.             });
  28.         }
  29.     });
  30. };
复制代码


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部