PdfiumViewer组件扩展(Pdfium.Net.Free)--添加图片
项目地址:
Pdfium.Net:https://github.com/1000374/Pdfium.Net.Free
PdfiumViewer:https://github.com/1000374/PdfiumViewer
Pdfium.Net.Free 支持
-
.NETFramework 4.0
-
.NETFramework 4.5
-
.NETStandard 2.0
- .Net8.0
可以和PdfiumViewer.Free共同使用预览、编辑pdf,也可以直接引用Pdfium.Net.Free 操作pdf,Pdfium.Net.Free封装了现有Pdfium的函数,实现了部分操作pdf的功能,部分功能等待后续~~
Pdfium添加图片有三个重载,根据入参可对添加的图片进行缩放、旋转、裁切
1.添加图片路径到pdf
/// <summary> /// add Image /// </summary> /// <param name="imagePath">image file</param> /// <param name="posX">x</param> /// <param name="posY">y</param> /// <param name="scalex">scale x</param> /// <param name="scaley">scale y</param> /// <param name="rotate">rotate</param> /// <param name="shear">shear</param> /// <returns></returns> public bool AddImage(string imagePath, float posX, float posY, float scalex = 1f, float scaley = 1, float rotate = 0, float shear = 0)
代码示例:
using (var doc = PdfDocument.CreateNew()) { var imagePath = "./Pdfium.NetTests/resources/mona_lisa.jpg"; var page = doc.Pages.Add(612, 792); page.AddImage(imagePath, 50, 100); doc.Save("./Pdfium.NetTests/AddImage.pdf"); }
2.添加流到pdf
/// <summary> /// add Image /// </summary> /// <param name="stream">new MemoryStream(File.ReadAllBytes(imagePath));</param> /// <param name="posX">x</param> /// <param name="posY">y</param> /// <param name="scalex">scale x</param> /// <param name="scaley">scale y</param> /// <param name="rotate">rotate</param> /// <param name="shear">shear</param> /// <returns></returns> public bool AddImage(Stream stream, float posX, float posY, float scalex = 1f, float scaley = 1, float rotate = 0, float shear = 0)
示例代码:
using (var doc = PdfDocument.CreateNew()) { var imagePath = "./Pdfium.NetTests/resources/mona_lisa.jpg"; var page = doc.Pages.Add(612, 792); var stream = new MemoryStream(File.ReadAllBytes(imagePath)); page.AddImage(stream, 50, 100); doc.Save("./Pdfium.NetTests/AddImage.pdf"); }
3.添加image 到pdf
/// <summary> /// add Image /// </summary> /// <param name="image">Image</param> /// <param name="posX">x</param> /// <param name="posY">y</param> /// <param name="scalex">scale x</param> /// <param name="scaley">scale y</param> /// <param name="rotate">rotate</param> /// <param name="shear">shear</param> /// <returns></returns> public bool AddImage(Image image, float posX, float posY, float scalex = 1f, float scaley = 1, float rotate = 0, float shear = 0)
代码示例:
using (var doc = PdfDocument.CreateNew()) { var imagePath = "./Pdfium.NetTests/resources/mona_lisa.jpg"; var image = Image.FromFile(imagePath, true); var page = doc.Pages.Add(612, 792); page.AddImage(image, 50, 100); doc.Save("./Pdfium.NetTests/AddImage1.pdf"); }