首页 > 其他分享 >Pdfium.Net.Free 一个免费的Pdfium的 .net包装器--删除或编辑pdf内容

Pdfium.Net.Free 一个免费的Pdfium的 .net包装器--删除或编辑pdf内容

时间:2024-01-31 18:14:44浏览次数:26  
标签:obj doc Pdfium Free var pdf page0

项目地址:

Pdfium.Net:https://github.com/1000374/Pdfium.Net

PdfiumViewer:https://github.com/1000374/PdfiumViewer

如需删除或者编辑pdf中的内容,首先要获取pdf内需要修改或者删除的对象,所有对页面编辑操作都需要调用GenerateContent函数方才生效

获取pdf所有对象的方法:

返回的信息包含当前对象的index、文字及字体信息(如对象是文本)、位置信息

 var pathPdf = "./Pdfium.NetTests/resources/fontText.pdf";
 using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf))))
 {
     var page0 = doc.Pages[0];
     var infos = page0.GetCharacterInformation();
 }

 

如上述不能满足需求,请使用下示例获取:

  //How to know "GetObject" index?
  var pathPdf = "./Pdfium.NetTests/resources/fontText.pdf";
  using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf))))
  {
      var arr = "武则天".ToCharArray();
      var page0 = doc.Pages[0];
      var count = page0.GetObjectsCount();
      for (int i = 0; i < count; i++)
      {
          var obj = page0.GetObject(0);
          if (!obj.IsNull)
          {
              var objType = obj.PageObjGetObjType();
              switch (objType)
              {
                  case FpdfPageObj.FPDF_PAGEOBJ_UNKNOWN:
                      {

                      }
                      break;
                  case FpdfPageObj.FPDF_PAGEOBJ_TEXT:
                      {
                          var txt = obj.TextObjGetText(page0.PageText);
                      }
                      break;
                  case FpdfPageObj.FPDF_PAGEOBJ_PATH:
                      {
                          var res = obj.PathMoveTo(10, 20);
                      }
                      break;
                  case FpdfPageObj.FPDF_PAGEOBJ_IMAGE:
                      {
                          /* Matrix: | a, c, e| ==> | width, 0,      offsetX|
                           *         | b, d, f|     | 0,     height, offsetY|*/
                          var bitmap = obj.ImageObjGetBitmap();
                          var res = obj.ImageObjSetMatrix(bitmap.Width, 0.1, 0, bitmap.Height, 100, 100);
                          if (!bitmap.IsNull)
                          {
                              //There is a feature request discussing this: https://crbug.com/pdfium/1930 (disclaimer: I'm the reporter)
                              //TLDR The functions you mention do provide the main data stream, but for some filters complementary data would be needed to actually re - construct the image, which pdfium does not provide.

                              //For CCITTDecode, as the TIFF format can use, pdfium's public API does not tell the CCITT group, but this would be needed to re-construct the TIFF header, which the PDF format strips. And I think BlackIs1 info would also be needed; possibly more.
                              //JBIG2Decode may optionally use a separate JBIG2Globals stream, which again pdfium does not provide.I had filed a separate bug about this: https://crbug.com/pdfium/1927. However, I guess the raw JBIG2 data might not be very useful except for re-insertion into a PDF. IIRC the way pikepdf handles JBIG2 extraction to files is to just decode the data and re-encode to some other format. From a programmatic POV that's not ideal, but I guess the context is that standalone JBIG2 isn't really supported by end-user apps.
                              //Concerning FPDFImageObj_GetImageDataDecoded(), note that it does not fully decode images; it only applies "simple" filters(see https://crbug.com/pdfium/1203#c7), so the function name is a bit misleading.

                              //For the plain pixel data, you can use FPDFImageObj_GetBitmap(), FPDFBitmap_GetBuffer() & co, but note that FPDF_BITMAP is limited in supported pixel formats and bit depth(e.g.no CMYK, B / W, > 8bpc RGB(A), ...).
                          }
                      }
                      break;
                  case FpdfPageObj.FPDF_PAGEOBJ_SHADING:
                      {

                      }
                      break;
                  case FpdfPageObj.FPDF_PAGEOBJ_FORM:
                      {

                      }
                      break;
              }
          }
      }
      page0.GenerateContent();
  }

  

编辑对象代码示例(文本):经测试、对于中文替换有时会出现乱码,暂未发现设置字库的方式,可通过先删除在添加文本的方式修改

  var pathPdf = "./Pdfium.NetTests/resources/hello_world.pdf";
  using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf))))
  {
      //var fontPath = @"c:\Windows\fonts\simhei.ttf";
      //doc.LoadFont(fontPath);
      var page0 = doc.Pages[0];
      var obj = page0.GetObject(0);
      if (!obj.IsNull)
      {
          if (!obj.IsNull)
          {
              var objType = obj.PageObjGetObjType();
              switch (objType)
              {
                  case FpdfPageObj.FPDF_PAGEOBJ_TEXT:
                      {
                          var txt = obj.TextObjGetText(page0.PageText);
                          var res = obj.TextSetText("Changed for SetText test");
                      }
                      break;
              }
          }
          page0.GenerateContent();
          doc.Save("./Pdfium.NetTests/TextObjFontChange.pdf");
      }
  }

 

删除对象:

  var pathPdf = "./Pdfium.NetTests/resources/fontText.pdf";
  using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf))))
  {
      var page0 = doc.Pages[0];
      var obj = page0.GetObject(0);
      if (!obj.IsNull)
      {
          var res = page0.RemoveObject(obj);
          page0.GenerateContent();
          doc.Save("./Pdfium.NetTests/TextObjFont.pdf");
      }
  }

  

 

 

 

标签:obj,doc,Pdfium,Free,var,pdf,page0
From: https://www.cnblogs.com/xiaohemiao/p/17999826

相关文章

  • CSharp: create pdf file using iText 8.0 in donet 8.0
     /*IDE:VS202217.5OS:windows10.net8.0iText8.0System.Text.Encoding.CodePages*/namespaceConsoleAppPdfdemo{usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.IO;usingSystem.Text;......
  • python将pdf每页截图保存
    python将pdf每页保存成图片保存一、安装依赖包pipinstallpdf2image 二、代码importosfrompdf2imageimportconvert_from_pathdefconvert_pdf_to_images(pdf_file,output_folder):#创建输出文件夹os.makedirs(output_folder,exist_ok=True)#......
  • 播报 | 天空卫士入围FreeBuf《CCSIP 2023中国网络安全产业全景图》16个细分领域
    2024年1月24,国内安全行业门户FreeBuf旗下FreeBuf咨询正式发布《CCSIP2023中国网络安全产业全景图》(第六版)。天空卫士成功入围SASE、数据防泄露(DLP)、分类分级、数据安全治理(解决方案)、数据安全管控(平台型)、邮件安全、UEBA、Web应用扫描与监控、云访问安全、SWG、恶意内容检测、移......
  • 《C++ Primer Plus》(第六版)中文版——思维导图+附录PDF+源代码
    说明,以下文件可在异步社区免费下载不同之处在于原附录PDF文件没有书签,而本文分享的附录文件带有书签本文所有文件下载链接:https://www.123pan.com/s/lO3uVv-uaEKv.html思维导图(图片)以下仅为预览,高清图片可从文章开头下载链接中下载另外后续本人有空会制作XMind脑图版本,会添加......
  • 【专题】2023年直播、短视频行业报告汇总PDF合集分享(附原数据表)
    原文链接:https://tecdat.cn/?p=35077原文出处:拓端数据部落公众号中国直播电商行业正在经历蓬勃发展的时期,各大互联网平台之间的竞争日益激烈,而直播电商已成为品牌营销的常态。随着直播电商的崛起,对品牌提供了全新的产品营销和特惠促销渠道,同时也作为新产品生产和推广的媒体发布......
  • 【专题】2023年中国白酒行业消费白皮书报告PDF合集分享(附原数据表)
    原文链接:https://tecdat.cn/?p=34188原文出处:拓端数据部落公众号2023年中国白酒行业消费白皮书报告合集,总结了消费市场的两大传承和五大进化,以帮助白酒企业更好地理解消费者心理和供需变化,从而把握增长机会。两大传承包括争夺消费者的“第一口酒”以及品牌在消费决策中的关键作......
  • 【专题】2023年大语言模型综合评测报告PDF合集分享(附原数据表)
    原文链接:https://tecdat.cn/?p=33624原文出处:拓端数据部落公众号自2022年年末以来,人工智能大模型已成为技术领域甚至全球创新领域最受关注的话题。以ChatGPT为代表的大模型产品发展迅速,预测数据显示,到2030年,AIGC市场规模有望超过万亿元。2023年,国内主要厂商也相继推出自研的大语......
  • 《深入理解计算机系统(原书第3版)》PDF
    内容简介本书从程序员的视角详细阐述计算机系统的本质概念,并展示这些概念如何实实在在地影响应用程序的正确性、性能和实用性。全书共12章,主要内容包括信息的表示和处理、程序的机器级表示、处理器体系结构、优化程序性能、存储器层次结构、链接、异常控制流、虚拟存储器、系统级I/......
  • 《深入理解计算机系统(原书第3版)》PDF
    内容简介本书从程序员的视角详细阐述计算机系统的本质概念,并展示这些概念如何实实在在地影响应用程序的正确性、性能和实用性。全书共12章,主要内容包括信息的表示和处理、程序的机器级表示、处理器体系结构、优化程序性能、存储器层次结构、链接、异常控制流、虚拟存储器、系统......
  • python解密带密码的pdf文件
    ##coding:utf-8###用来存储一些通用模块fromPyPDF2importPdfReader#pdf的读取方法fromPyPDF2importPdfWriter#pdf的写入方法fromCrypto.CipherimportAES#高加密的方法,要引入不然会报错defget_reader(filename,password):#读取pdf的方法(自定义......