This samples shows how to use images in your PDF documents.
There are several ways to add an image to your PDF document. You can create an image from a file in GIF/Tiff/Png/Bmp/Jpeg formats, or a buffer of image bytes, or an System.Drawing.Image object.
In order to add a new image to a PDF document, use PdfDocument.AddImage(...) method. After that use PdfCanvas.DrawImage(..) method to draw the added image on page or watermark. You can use overloads of PdfCanvas.DrawImage method to specify rotation angle or output size of the image to be drawn.
Each image can be drawn any number of times on any number of pages and with different output sizes or rotation angles.
using System.Diagnostics; using System.Drawing; namespace BitMiracle.Docotic.Pdf.Samples { public static class AddAndDrawImage { public static void Main() { // NOTE: // When used in trial mode, the library imposes some restrictions. // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx // for more information. string pathToFile = "AddAndDrawImage.pdf"; using (PdfDocument pdf = new PdfDocument()) { PdfCanvas canvas = pdf.Pages[0].Canvas; PdfImage image = pdf.AddImage("Sample data/ammerland.jpg"); canvas.DrawImage(image, 10, 50); using (Bitmap bitmap = new Bitmap("Sample data/pink.png")) { PdfImage image2 = pdf.AddImage(bitmap); canvas.DrawImage(image2, 400, 50, 100, 150, -45); } pdf.Save(pathToFile); } Process.Start(pathToFile); } } }
Imports System.Diagnostics Imports System.Drawing Imports System.IO Imports BitMiracle.Docotic.Pdf Namespace BitMiracle.Docotic.Pdf.Samples Public NotInheritable Class AddAndDrawImage Public Shared Sub Main() ' NOTE: ' When used in trial mode, the library imposes some restrictions. ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx ' for more information. Dim pathToFile As String = "AddAndDrawImage.pdf" Using pdf As New PdfDocument() Dim canvas As PdfCanvas = pdf.Pages(0).Canvas Dim image As PdfImage = pdf.AddImage("Sample data/ammerland.jpg") canvas.DrawImage(image, 10, 50) Using bitmap As New Bitmap("Sample data/pink.png") Dim image2 As PdfImage = pdf.AddImage(bitmap) canvas.DrawImage(image2, 400, 50, 100, 150, -45) End Using pdf.Save(pathToFile) End Using Process.Start(pathToFile) End Sub End Class End Namespace