public static void AddImageAsPage() { PdfDocument document = new PdfDocument(); using (Stream imgStream = File.OpenRead("sample.jpg")) { iDiTect.Pdf.Resources.ImageSource image = new iDiTect.Pdf.Resources.ImageSource(imgStream); //Create a new page with image's size PdfPage page = new PdfPage(); page.Size = new Size(image.Width, image.Height); PageContentBuilder builder = new PageContentBuilder(page); //draw image to page at position (0,0) builder.DrawImage(image); document.Pages.Add(page); } using (FileStream fs = File.OpenWrite("ConvertImageToPdf.pdf")) { PdfFile pdfFile = new PdfFile(); pdfFile.Export(document, fs); } }
public static void AddImageWatermark() { PdfFile pdfFile = new PdfFile(); PdfDocument document; using (FileStream fs = File.OpenRead("sample.pdf")) { //Read pdf document from stream document = pdfFile.Import(fs); } //Get first page of pdf PdfPage page = document.Pages[0]; PageContentBuilder builder = new PageContentBuilder(page); //Set watermark image position builder.Position.Translate(100, 100); using (Stream stream = File.OpenRead("watermark.png")) { //Insert watermark image as original size builder.DrawImage(stream); //Insert watermark image in customized size //builder.DrawImage(stream, new Size(80, 80)); } using (FileStream fs = File.OpenWrite("ImageWatermark.pdf")) { pdfFile.Export(document, fs); } }
public static void AddImage() { PdfFile pdfFile = new PdfFile(); PdfDocument document; using (FileStream fs = File.OpenRead("sample.pdf")) { //Read pdf document from stream document = pdfFile.Import(fs); } //Get first page of pdf PdfPage page = document.Pages[0]; //Create page level builder PageContentBuilder builder = new PageContentBuilder(page); //Add image using builder's DrawImage directly using (Stream imgStream = File.OpenRead("sample.jpg")) { //Set image position builder.Position.Translate(100, 100); //insert image as original size builder.DrawImage(imgStream); //insert image with customized size //builder.DrawImage(imgStream, new Size(80, 80)); } //Add image using Block object using (Stream imgStream = File.OpenRead("sample2.jpg")) { //Set image position builder.Position.Translate(100, 400); Block block = new Block(); //insert image as original size //block.InsertImage(imgStream); //insert image with customized size block.InsertImage(imgStream, new Size(100, 100)); builder.DrawBlock(block); } using (FileStream fs = File.OpenWrite("InsertImage.pdf")) { pdfFile.Export(document, fs); } }
public static void AddImageSignature2PDF() { PdfFile pdfFile = new PdfFile(); PdfDocument document = pdfFile.Import(File.ReadAllBytes("sample.pdf")); //Input your certificate and password X509Certificate2 certificate = new X509Certificate2("test.pfx", "iditect"); //First you need create a SignatureField with unique name SignatureField signatureField = new SignatureField("iDiTect Sign Field"); //Add signature object to a signature field, so we can add a visualization to it signatureField.Signature = new Signature(certificate); //Add signature info as need signatureField.Signature.Properties.Reason = "Sign by iDiTect"; signatureField.Signature.Properties.Location = "World Wide Web"; signatureField.Signature.Properties.ContactInfo = "1234567"; //Apply a visible signature widiget to represent the contents of the signature field. SignatureWidget widget = signatureField.Widgets.AddWidget(); //Set signature position and size widget.Rect = new System.Windows.Rect(new System.Windows.Point(200, 200), new System.Windows.Size(50, 50)); widget.RecalculateContent(); //Customize signature appearance, insert an image as signature displaying PageContentBuilder signatureAppearance = new PageContentBuilder(widget.Content.NormalContentSource); using (Stream imgStream = File.OpenRead("sample.jpg")) { signatureAppearance.DrawImage(imgStream); } //Add this signature to first PDF page document.Pages[0].Annotations.Add(widget); document.AcroForm.FormFields.Add(signatureField); //Digital sign feature need the stream support reading, so you have to use ReadWrite mode here using (FileStream fs = new FileStream("signed.pdf", FileMode.Create, FileAccess.ReadWrite)) { pdfFile.Export(document, fs); } }