本帖最后由 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方法。
服务控制器代码:
- [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;
- }
复制代码 以下是使用Bates Stamping将水印应用于RasterImage的每个页面的相关代码:
- 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;
- }
- }
- }
复制代码 这里是客户端的Javascript代码与相关的Ajax调用来调用这个方法:
- 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);
- });
- }
- });
- };
复制代码
|