Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Insert an image into PDF using iTextSharp with C# (C-Sharp)

In this article, we are going to learn how to insert an image into PDF file using Itextsharp in asp.net with C#. First, you need to download iTextSharp dll from the internet. Click on the link to download https://github.com/itext/itextsharp.

Related article:

How to generate PDF file using iTextSharp in C#.

Once file is downloaded, extract it, now you will find 6 more .rar file. Again extract itextsharp-dll-core.rar file, after that add reference of itextsharp.dll to your project.

In code-behind file:

Add below nampespaces.


using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

Complete C# code:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
InsertImage();
}
}

public void InsertImage()
{
// create Filestream object
FileStream fs = new FileStream(Server.MapPath("Example.pdf"), FileMode.Create);

// create Document object
Document doc = new Document();

// create PdfWriter instance which will write at file filestream
PdfWriter.GetInstance(doc, fs);

// opening the dociment
doc.Open();

// creating paragraph object
Paragraph para = new Paragraph("Insert an image into pdf using C#.");
para.Alignment = Element.ALIGN_CENTER;

// adding pargraph to document
doc.Add(para);

// setting image path
string imagePath = Server.MapPath("Images") + "\\demo.PNG";
// string imagePath = Server.MapPath("Images\\demo.PNG") + "";

iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagePath);

image.Alignment = Element.ALIGN_CENTER;

// set width and height
image.ScaleToFit(180f, 250f);

// adding image to document
doc.Add(image);
// closing the document
doc.Close();
}

Below is the pdf file generated.



This post first appeared on ASPArticles, please read the originial post: here

Share the post

Insert an image into PDF using iTextSharp with C# (C-Sharp)

×

Subscribe to Asparticles

Get updates delivered right to your inbox!

Thank you for your subscription

×