前端js给pdf文件添加印章,前端使用PDF.js获取pdf文件的坐标位置,后端使用C# iTextSharp

老年新手一枚,一直以来都是在网上cv大神们的代码。内心既感激又崇拜。最近在工作中遇到一个问题,需要在电子文档里添加印章,所以,开始接触到pdf.js插件。

 PDF.js 官网:PDF.js

百度了一下,找到第一种方法是通过获取点击点的标签文本,然后后台使用iTextSharp通过关键字查询获取关键词坐标点。这种办法虽可实现功能,但是缺点是非常不灵活,体验也不好。

所以长时间研究查询后,决定在通过获取鼠标点击点相对于pdf文档的坐标来实现项目要求。

如何使用pdf.js打开文档等相关操作,这里不做赘述。

直接进入主题,使用pdf.js的默认展示页面viewer.html打开一个文档后,查看源码找到id="viewerContainer"的元素标签,仔细观察发现pdf文件的每一页内容对应一个class="page"的标签,而每一个page标签下有一个class="canvasWrapper"的元素(里面包含一个canvas元素和一个class="textLayer"的div元素)。注意每个元素的属性,这些属性可以帮助我们更容易获取到坐标。

在class="textLayer"的元素里,pdf文档的每段内容都是一个span元素,元素中有role="presentation"的属性,style样式中有left和top两个属性,并且是百分比值。如下图。

当鼠标点击事件的target元素是role="presentation"的span元素时,直接获取style的left和top属性值即可。这也就大概获取到了鼠标点击点相对于pdf页左上角的坐标。

当点击到pdf页的空白位置时,则需要通过点击事件的target元素的offsetWidth ,offsetHeight属性来获取坐标值,再除以class="textLayer"的元素的宽高获取坐标百分比。

鼠标点击点的坐标已获取到,那么通过iTextSharp在坐标点添加印章则水到渠成。

另外需要注意的是在js中坐标原点为左上角,而iTextSharp中,坐标原点为左下角。因此,在前端js中获取到的纵坐标需要使用差值。

好的,第一次发表文章,希望对大家有些帮助,若有更好的方法,希望能不吝赐教。大家一起进步。能混到一口更好的饭。哈哈。

本文为原创,如需转载,请注明作者及出处。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
这个问题需要用到 iTextSharp 库来解决。首先,你需要安装该库并导入它。然后,你可以使用以下代码获取指定关键字的坐标位置信息: ```c# using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using iTextSharp.text.pdf; using iTextSharp.text.pdf.parser; namespace PdfKeywordCoordinates { class Program { static void Main(string[] args) { string filename = @"C:\example.pdf"; // pdf 文件路径 string keyword = "example keyword"; // 指定关键字 using (PdfReader reader = new PdfReader(filename)) { for (int page = 1; page <= reader.NumberOfPages; page++) { ITextExtractionStrategy strategy = new LocationTextExtractionStrategy(); string currentText = PdfTextExtractor.GetTextFromPage(reader, page, strategy); if (currentText.Contains(keyword)) { var kwLocation = new List<RectAndText>(); var renderFilter = new RenderFilter[1]; renderFilter[0] = new RegionTextRenderFilter(new Rectangle(0, 0, 1000, 1000)); var textExtractionStrategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), renderFilter); PdfContentStreamProcessor processor = new PdfContentStreamProcessor(textExtractionStrategy); processor.ProcessContent(reader.GetPageContent(page)); kwLocation = ((LocationTextExtractionStrategy)textExtractionStrategy).GetLocations(); foreach (RectAndText rectAndText in kwLocation) { if (rectAndText.text.Contains(keyword)) { Console.WriteLine("Page: " + page + " X: " + rectAndText.rect.Left + " Y: " + rectAndText.rect.Bottom); } } } } } Console.ReadLine(); } } public class RectAndText { public iTextSharp.text.Rectangle rect; public String text; public RectAndText(iTextSharp.text.Rectangle rect, String text) { this.rect = rect; this.text = text; } } } ``` 这个代码将在指定的 PDF 文件中查找指定的关键字,并输出该关键字在每一页中的坐标位置信息。注意,这个代码是使用 C# 编写的,如果你使用的是 Python,你需要使用 Python 版本的 iTextSharp 库,并使用相应的语法来实现相同的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值