PDF 用 Patagames 删除水印 C#

有个pdf文件,不管是用福昕,还是adobe,都无法正常删除水印。

 用 Patagames 库,去掉它

核心代码 :


using Patagames.Pdf;
using Patagames.Pdf.Net;

        private void pdfHandler(string name) {
            string path = name.Substring(0, name.LastIndexOf('\\') + 1);
            string[] fileNameExt = name.Substring(name.LastIndexOf('\\') + 1).Split('.');
            string newName = path + fileNameExt[0] + "_"+DateTime.Now.ToString("yyyy_MM_dd-HHmmss") + "." + fileNameExt[1];
            PdfDocument document = PdfDocument.Load(name);
            for(int i = 0; i < document.Pages.Count; i++) {
                PdfPage collection = document.Pages[i];
                Console.WriteLine(collection.Text);
                FS_SIZEF pageSize = document.GetPageSizeByIndex(i);
                //Image image = new Image();
                Console.WriteLine("page: " + i + ", " + collection.PageObjects.Count);
                int removeCount = 0;
                for(int j = collection.PageObjects.Count - 1; j >= 0; j--) {
                    FS_RECTF rec = collection.PageObjects[j].BoundingBox;
                    float area = rec.Height * rec.Width;
                    if(isFilter((int)area)) {
                        removeCount++;
                        Console.WriteLine(string.Format("pages: {0}, RemoveAt Ojbect: {1} , area : {2}", i, j, area));
                        collection.PageObjects.RemoveAt(j);
                    }
                    //Console.WriteLine(string.Format("pages: {0}, Ojbect: {1} , area : {2}", i, j, area));
                }
                if(removeCount != 2) {
                    Console.WriteLine(string.Format("pages: {0} error", removeCount));
                }
                collection.GenerateContent();
            }
            document.WriteBlock += (s, ex) => {
                using(var stream = new FileStream(newName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
                    stream.Seek(0, SeekOrigin.End);
                    stream.Write(ex.Buffer, 0, ex.Buffer.Length);
                }
            };
            document.Save(Patagames.Pdf.Enums.SaveFlags.NoIncremental | Patagames.Pdf.Enums.SaveFlags.RemoveUnusedObjects);
            document.Dispose();
        }


        private void Form_Load(object sender, EventArgs e) {
            // regedit web https://patagames.com/request-trial/
            // how to use the license
            //Initialize the SDK library with license key
            //You have to call this function before you can call any PDF processing functions.
            PdfCommon.Initialize("04040B50*********");

            //Open and load a PDF document from a file.
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值