Pdfium.Net.Free 一个免费的Pdfium的 .net包装器--可视化编辑pdf

Pdfium.Net.Free 支持

  • .NETFramework 4.0

  • .NETFramework 4.5

  • .NETStandard 2.0

  • .Net8.0

可以和PdfiumViewer.Free共同使用预览pdf,也可以直接引用Pdfium.Net.Free 操作pdf,解决部分.NetCore 调用的问题,Pdfium.Net.Free封装了现有Pdfium的函数,实现了部分操作pdf的功能,部分功能等待后续~~

项目地址:

Pdfium.Nethttps://github.com/1000374/Pdfium.Net.Free

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

操作pdf对象

  • 获取PdfPageobject对象
  • 把操作转换成矩阵
  • 生成pdf页内容

转换矩阵请查看:什么是转换矩阵以及如何使用它

获取pdf对象:

复制代码
 var infos = new List<ObjectInformation>();
 var count = page.GetObjectsCount();
 for (int j = 0; j < count; j++)
 {
     var obj = page.GetObject(j);
     GetObject(obj, infos, i, new List<int> { j }, page.PageText);
 }

  private void GetObject(PdfPageobject obj, List<ObjectInformation> infos, int ipage, List<int> objIndex, PdfTextpage textpage)
  {
      if (!obj.IsNull)
      {
          var objType = obj.PageObjGetObjType();
          var rect = obj.PageObjGetBounds().ToRectangle();
          var cainfo = new ObjectInformation(ipage, obj, objIndex, objType, rect);
          var sunObjCount = obj.FormObjCountObjects();
          if (sunObjCount > 0)
          {
              cainfo.SubObjectInformations = new List<ObjectInformation>();
              for (int sub = 0; sub < sunObjCount; sub++)
              {
                  var subObj = obj.FormObjGetObject(sub);
                  var indexs = new List<int>(objIndex);
                  indexs.Add(sub);
                  GetObject(subObj, cainfo.SubObjectInformations, ipage, indexs, textpage);
              }
          }

          if (_isEditMinimum)
          {
              if (cainfo.PageObjType == PdfPageObjType.Text || cainfo.PageObjType == PdfPageObjType.Image || cainfo.SubObjectInformations?.Count > 0)
                  infos.Add(cainfo);
          }
          else
          {
              infos.Add(cainfo);
          }
      }
  }

把操作转换成矩阵

复制代码
 var obj = _currObjectInformation.PdfPageobject;
 var matrix = obj.PageObjGetMatrix();
 bool res = false;
 switch (_cursorMode)
 {
     case PdfViewerCursorMode.Move:
         {
             res = obj.SetMatrix(1, 0, 0, 1, width, -height);
             break;
         }
     case PdfViewerCursorMode.LeftTop:
         {
             res = obj.SetMatrix(1 - width / matrix.A, 0, 0, 1 - height / matrix.D, width, 0);
             break;
         }
     case PdfViewerCursorMode.Top:
         {
             res = obj.SetMatrix(1, 0, 0, 1 - height / matrix.D, 0, 0);
             break;
         }
     case PdfViewerCursorMode.RightTop:
         {
             res = obj.SetMatrix(1 + width / matrix.A, 0, 0, 1 - height / matrix.D, 0, 0);
             break;
         }
     case PdfViewerCursorMode.Right:
         {
             res = obj.SetMatrix(1 + width / matrix.A, 0, 0, 1, 0, 0);
             break;
         }
     case PdfViewerCursorMode.RightBottom:
         {
             res = obj.SetMatrix(1 + width / matrix.A, 0, 0, 1 + height / matrix.D, 0, -height);
             break;
         }
     case PdfViewerCursorMode.Bottom:
         {
             res = obj.SetMatrix(1, 0, 0, 1 + height / matrix.D, 0, -height);
             break;
         }
     case PdfViewerCursorMode.LeftBottom:
         {
             res = obj.SetMatrix(1 - width / matrix.A, 0, 0, 1 + height / matrix.D, width, -height);
             break;
         }
     case PdfViewerCursorMode.Left:
         {
             res = obj.SetMatrix(1 - width / matrix.A, 0, 0, 1, width, 0);
             break;
         }
     case PdfViewerCursorMode.Rotation:
         {
             var angle = 90 - CharacterHelper.Direction(width, -height);
             _currObjectInformation.DirectionEnd = point2.Location;
             double angleValue = (angle / 180.0d) * Math.PI;

             res = obj.SetMatrix((float)Math.Cos(angleValue), (float)-Math.Sin(angleValue), (float)Math.Sin(angleValue), (float)Math.Cos(angleValue), 0, 0);
             break;
         }
     default:
         break;
 }

生成pdf页内容

复制代码
 var page = Document.Pages[_currObjectInformation.Page];
 if (page.GenerateContent())
{}

效果

1.编辑文字和图像(图片缩放、移动,文字编辑、移动)

2.删除文字和图像

3.操作完成后保存pdf