using Spire.Pdf; using System.Drawing; using System.Drawing.Imaging; /// <summary> /// 添加二维码到PDF /// </summary> /// <param name="sourcePdf">pdf文件路径</param> /// <param name="sourceImg">二维码图片路径</param> private void AddQrCodeToPdf(string sourcePdf,string sourceImg) { //初始化PdfDocument实例 Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument(); //加载现有文档 doc.LoadFromFile(sourcePdf); //获取第二页 PdfPageBase page = doc.Pages[1]; //加载图片到Image对象 Image image = Image.FromFile(sourceImg); //调整图片大小 int width = image.Width; int height = image.Height; float scale = 0.18f; //缩放比例0.18f; Size size = new Size((int)(width * scale), (int)(height * scale)); Bitmap scaledImage = new Bitmap(image, size); //加载缩放后的图片到PdfImage对象 Spire.Pdf.Graphics.PdfImage pdfImage = Spire.Pdf.Graphics.PdfImage.FromImage(scaledImage); //设置图片位置 float x = 516f; float y = 8f; //在指定位置绘入图片 page.Canvas.DrawImage(pdfImage, x, y); //保存文档 doc.SaveToFile(@sourcePdf); doc.Close(); //释放图片资源 image.Dispose(); }