项目地址:
Pdfium.Net:https://github.com/1000374/Pdfium.Net
PdfiumViewer:https://github.com/1000374/PdfiumViewer
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"); }
标签:Pdfium,--,doc,float,Free,AddImage,imagePath,var From: https://www.cnblogs.com/xiaohemiao/p/17997370