basic introduction
Business needs, we need to add functionality to the two-dimensional code pdf drawings, the implementation process record
Download Library
Add Reference
Add Namespace
using System.IO; using iTextSharp.text.pdf;
Insert Picture handler
/// <Summary> /// added to the pdf image /// </ Summary> /// <param name = "oldp"> Source address pdf </ param> /// <param name = "Imp"> image URL </ param> /// <param name = "X"> X-axis coordinate </ param> /// <param name = "Y"> Y-axis coordinate </ param> protected void AddImg ( String oldp, String Imp, int X, int Y) { String newp = Path.GetDirectoryName (oldp) + the Path.GetFileNameWithoutExtension(oldP) + "_temp.pdf"; ; try { using (Stream inputPdfStream = new FileStream(oldP, FileMode.Open, FileAccess.Read, FileShare.Read)) using (Stream inputImageStream = new FileStream(imP, FileMode.Open, FileAccess.Read, FileShare.Read)) using (Stream outputPdfStream = new FileStream(newP, FileMode.Create, FileAccess.Write, FileShare.None)) { var reader = new PdfReader(inputPdfStream);//读取原有pdf var stamper = newPdfStamper (Reader, outputPdfStream); var pdfContentByte stamper.GetOverContent = ( . 1 ); // Get a first pdf page content iTextSharp.text.Image Image = iTextSharp.text.Image.GetInstance (inputImageStream); // get image image.ScalePercent ( 40 ); // set the image ratio image.SetAbsolutePosition (X, Y); // set the absolute position of the image pdfContentByte.AddImage (image); stamper.Close (); } } the catch (Exception EX) { the throw EX; } finally { File.Copy(newP, oldP, true); File.Delete(newP); } }
achieve
private void Button1_Click(object sender, EventArgs e) { try { string destPhysicalFile = @"C:\Users\Administrator\Desktop\K0000113_舱容图.pdf"; string Img = @"C:\Users\Administrator\Desktop\1572568425.png";//图片文件路径 AddImg(destPhysicalFile, Img, 129, 574); MessageBox.Show("ok"); } catch (Exception ex) { throw ex; } }
effect