C# .NET Project to Add Multiple Images in PDF Document Using iTextSharp Library
Jan 9, 2025
Get the full source code of application here: https://gist.github.com/gauti123456/9d1224b22cc3ba63a240f2094e2a360a
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video we will look at how to
0:05
actually insert images inside your PDF
0:08
document inside
0:11
cop.net project and you'll be building a
0:14
simple command line application which
0:16
will insert images inside your PDF
0:18
document and for this purpose we using
0:20
the it text Shar Library which is a C
0:24
library.net library for working with PDF
0:27
files so what I have is that we have a
0:31
folder called as
0:33
images which contains these four images
0:36
first second third fourth it contains
0:39
jpg PNG webp images of all extensions so
0:42
you can just see we are having these
0:45
images we are targeting jpg PNG webp and
0:49
we are constructing this output.pdf file
0:52
so once I run this script if I run this
0:54
now in Visual Studio click this it will
0:57
open this PDF document now it will
0:59
contain four pages and it will contain
1:01
each image in each individual page so
1:04
now it has con constructed this PDF
1:06
document which contains these four
1:08
Images this is the first image second
1:10
image third image and the fourth image
1:13
so all these images that was there in
1:17
this images folder has been constructed
1:20
into a PDF document and we will be
1:22
looking at how to develop this project
1:25
so for this we are using this Library it
1:27
text sharp which is actually a famous
1:31
c.net library for working with PDF files
1:35
so let's get started so for building
1:37
this application inside our Visual
1:39
Studio first of all go to tools and you
1:42
need to install this package go to
1:44
nugget package manager manage nugget
1:46
manager for solution and go to browse
1:49
section and just search for this Library
1:52
itext
1:53
sharp so if you just search for this
1:56
package the very
1:58
first you will basically you see this
2:02
itex PDF generation written entirely in
2:05
C for the net
2:07
platform and uh we will be using this so
2:11
now
2:12
simply make
2:14
your for first of all we'll be importing
2:17
all these packages it text sharp package
2:21
it contains the text module we need to
2:24
import this and then we need to import
2:27
the PDF module as
2:31
well
2:32
PDF and lastly we need the system
2:36
built-in module of
2:46
Diagnostics so after this uh you will
2:49
simply put a namespace PDF generator
2:53
lets or PDF images to
2:57
PDF so we'll have this uh program main
3:02
class static void main
3:05
function a basic CA program that we are
3:09
writing right here and it will work in
3:11
the command line
3:13
so and right for first of all we do need
3:16
to create a PDF document for this for
3:20
creating the PDF document we'll put a
3:23
document here which is actually a part
3:25
of itex Chap and you'll be instantiating
3:28
a new instance of document using this
3:31
Constructor and here we need to actually
3:33
pass some properties related to the PDF
3:35
document so what page size that you want
3:38
to construct so it has various sizes A4
3:41
A5 so the most common size is A4 so I
3:45
will select page size A4 then we set
3:48
need to set the margin left and the
3:50
margin right I will set it to 10 comma
3:53
10 and then for the top margin and the
3:56
bottom margin I will set it to 10 comma
3:58
10 as well last property is
4:02
actually a I think a Boolean parameter
4:05
that you need to
4:06
set okay okay that's all we need to set
4:09
we have set the page size the margins in
4:12
all the positions top left bottom right
4:14
corner so after you set this document we
4:17
need to actually provide the output file
4:20
that we need to construct so you'll be
4:22
constructing this result. PDF which will
4:24
create in the same
4:26
directory so this output file will get
4:30
created after this application executes
4:33
so now we need to actually create a
4:36
object for this which is PDF writer
4:38
object so this line we need to write
4:41
which is PDF
4:42
writer this is actually a class of
4:46
PDF uh itex uh itex chap Library here
4:51
you simply say PDF writer uh dot get
4:56
instance and here you need to Simply
4:58
tell
5:00
pass the document that we need to save
5:04
and here it takes the second argument
5:06
the look uh file stream we will be
5:10
simply be telling we need to actually
5:12
save this at this location and the file
5:15
mode will be to actually create because
5:19
we are creating the PDF file so that's
5:21
why we'll put here fil mode. create so
5:24
after you do this guys we need to open
5:28
the document first of all
5:30
for opening the document we'll use the
5:32
open function which is there in this
5:35
object after opening it we need to
5:38
basically say get all the
5:40
images which are present in the images
5:44
folder so basically guys uh if you see
5:47
in the project
5:49
directory in my project directory if I
5:51
open this folder in file explorer so in
5:55
the bin folder debug folder we have this
5:57
images folder right here where all all
5:59
the images are present so these four
6:02
Images are present this PNG jpg image so
6:06
all these four Images we need to pass so
6:09
we will actually read all these images
6:11
one by one so for reading it we will
6:15
actually concatenate the path here
6:17
inside this array string array we'll
6:20
construct this all files array here and
6:23
for looping through all the files we'll
6:25
simply say directory. get files function
6:29
and here we be passing the directory in
6:32
which we need to search so we need to
6:34
search for the images folder which is
6:36
present in the same directory and here
6:38
we need to explicitly tell the
6:40
extensions so jpg star.jpg
6:44
we need and then we need to Simply
6:48
change these methods we need to
6:53
concatenate we also want the files which
6:56
are PNG as well so you'll simply say
6:58
directory get
7:00
files again the same folder but this
7:03
time we also need the files of PNG as
7:06
well and same thing we will do this one
7:09
more time for the webp files as well
7:13
so so put a semi
7:19
Coler uh I think uh what is the problem
7:27
here okay we do need to
7:30
convert this to into array so at last we
7:33
need to Simply call this function to
7:35
array to actually convert this into a
7:37
array so now all these uh this code
7:41
simply means that we have declared this
7:43
all files array which will actually
7:45
search for this folder images it will
7:48
pick all the files which are jpg PNG
7:50
webp and it will construct the array by
7:52
using this function to array and it will
7:55
store all the paths inside this all
7:57
files object so if you try to console
8:00
this let's
8:04
suppose so if you run this now what you
8:07
will see in the console it will print
8:09
out all the paths you will see it will
8:11
say system do string it's a string array
8:15
so it will contain all the
8:19
values so now we need to Simply Loop
8:24
through all the images for looping
8:27
through we'll be using the for each Loop
8:29
in
8:30
C and you'll simply say for each image
8:33
in all the files you'll see
8:35
that we need to Simply uh create a new
8:39
image object in the PDF for creating it
8:42
you'll be using the image Constructor
8:46
image. get
8:49
instance and we'll pass the actual file
8:52
that we are looping through so here we
8:54
are simply telling uh we just need to
8:56
create a brand new image by using the we
8:59
using using it itex sharp library and we
9:02
using this image Constructor like
9:08
this so after we do this we need to
9:11
Simply calculate the position of this
9:14
image calculate the scale and the
9:18
position and the coordinate x and y
9:20
coordinate to actually fit in the page
9:23
for this you'll simply calculate the
9:25
width of the PDF Page by using doc do
9:29
page size dot
9:32
width and here we'll be simply using
9:35
some maths so we will
9:39
subtract this from the left
9:43
margin minus the right margin you can
9:47
change these values accordingly once you
9:49
get the code all the source code is
9:51
given in the
9:53
description so we will calculate this in
9:57
this way whatever is the width of the
9:59
page we are subtracting the left margin
10:01
and the right margin similarly uh we
10:04
will calculate the height as
10:07
well so we'll simply make it dock
10:12
height for this this will become
10:17
height for this we need to subtract the
10:20
top
10:21
margin and the bottom
10:27
margin so then we need to constru conct
10:29
the image width how much you want to put
10:33
the image width so image equal to image
10:35
scaled width whatever is the scaled
10:37
width of the image and similarly the
10:40
height as
10:42
well scaled width
10:48
sorry this will be scaled
10:51
height so whatever is the original width
10:54
and the height of the image we are
10:56
simply storing it in these two variables
10:58
and then we need to Simply say the scale
11:00
factor what is the scale factor you can
11:04
just calculate this by using math. Min
11:08
and then we simply divide the doc
11:11
width divide this by image
11:15
width and similarly the height will be
11:18
dock height divided by the image
11:21
original
11:23
height this is simple maths and uh then
11:27
we need to Simply set this Factor
11:29
by using the function called as scale
11:33
percent and a scale factor whatever is
11:36
the scale factor calculated we'll
11:38
multiply this by 100 to like this and
11:42
then lastly we need to add this page to
11:46
the PDF
11:48
document for this we'll simply say
11:52
Doc new page you need to basically add a
11:56
new page to the PDF document by using
11:58
this new page function and lastly we
12:00
need to add the
12:01
image so for adding this we have the add
12:05
function and here we will be simply add
12:07
the actual image that we
12:10
constructed after setting all the width
12:12
height scale factor lastly we add this
12:14
image to the PDF document that Sol by
12:16
using the add function so this is
12:19
actually in the loop here so this Loop
12:21
will run for each image which is present
12:23
in that images folder so once all the
12:25
images are done we will come to the out
12:28
of this Loop and simply close the PDF
12:31
document so for closing the PDF document
12:34
we have the close function which is
12:36
available it will close the connection
12:39
and after this we can simply open the
12:43
PDF
12:44
file
12:48
automatically so for doing this we
12:50
simply need to say process. start it
12:52
will start a new process in C by using
12:56
this new process start info Constructor
13:00
here we need to Simply pass the location
13:02
where the output file is present output
13:04
file and the second parameter we need to
13:07
set is use
13:12
shell uh use cell
13:15
execute is equal to
13:23
True uh I think let me see what is the
13:28
problem I think I put a
13:31
parenthesis like this you need to do
13:33
like this uh no comma is there so this
13:37
is actually a Constructor we pass the
13:38
actual output file use shell execute is
13:41
s to true this will open the PDF file
13:43
automatically so this is actually the
13:45
program guys and try to uh let me run
13:49
this file now click run you will see it
13:51
will open the PDF document and now it is
13:53
containing all those four images in this
13:56
PDF document if you see the quality of
13:58
the images look tremendous in the PDF
14:00
document it is taking the full width
14:02
full height of that original image so it
14:05
is containing jpg PNG web P images as
14:08
well so all these four Images is
14:10
successfully added to this PDF document
14:13
and we have used this library of c.net
14:16
itext Shar for working with PDF
14:19
documents for inserting images so please
14:21
hit that like button subscribe the
14:23
Channel all the source code is given in
14:24
the description of this video and I will
14:26
be seeing you in the next one
#Computer Education
#Windows & .NET