C# .NET Project to Convert Text File to PDF Document Using iTextSharp Library in Terminal
Jan 9, 2025
Get the full source code of application here:
https://gist.github.com/gauti123456/5a9d280695d75a8af4463f4a58fa21ca
Show More Show Less View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video we will look at a very
0:03
simple example in c.net to actually
0:06
convert your text file into PDF document
0:09
so we will be using uh itex Shar Library
0:13
which is open source C library for
0:16
working with PDF and net so it text chab
0:20
actually has a way to actually convert
0:23
your text file into PDF so I already
0:27
have a text file let me show you if I
0:30
open with open this folder in file
0:33
explorer so this uh I have this
0:37
input.txt file and if you see a lot of
0:40
text is there sample text raw text is
0:44
there so if I need to now convert this I
0:46
will simply press the Run button to
0:49
actually run this file so you can
0:52
actually see PDF file generated
0:54
successfully and uh let me now show you
0:57
the output the output for while I call
1:00
this as output.pdf so let me show
1:05
you so you can basically see guys the
1:08
formatting looks really good the spacing
1:11
the margin everything it totally takes
1:13
care of automatically the library will
1:16
take over it will calculate the spacing
1:19
if you if the text is larger then it
1:21
will split the text into multiple Pages
1:24
you'll see that now this contains two
1:26
pages this is the first page and uh you
1:29
can just see the margin spacing
1:31
everything is perfect and every text
1:35
everything which was there in the text
1:36
file is successfully converted to PDF
1:39
we'll be looking at a very simple
1:41
example so sometimes you need to
1:43
actually convert your text file into PDF
1:46
so the library we are talking about is
1:48
itex Shar which is a open-source C
1:52
package in net work with the PDF
1:55
documents so now to get started first of
1:58
all you do need to install this package
2:00
go to tools and nugget package manager
2:03
manage nugget Solutions and here you
2:06
need to search for this package which is
2:08
the itex
2:10
shop the very first link which comes
2:13
this is actual package 40.7 million
2:16
downloads simply install it I already
2:18
installed it now coming back to the
2:21
program you need to now include
2:25
this package which is using it teex shop
2:29
and we need to use the text module of it
2:32
to actually convert the text to PDF and
2:35
then we will again use I text CH and we
2:38
also need the PDF module as well so we
2:40
are using the text module and the PDF
2:43
module and then we need to Simply say
2:45
the Nam
2:47
space Sorry it text. text.pdf this
2:52
module name space you can declare it
2:56
text to PDF and uh
3:00
we'll have a simple program
3:05
class and then a simple main
3:13
function so inside this guys we need to
3:16
first of
3:17
all check if the input file input txt
3:22
file exist or not for that we'll have if
3:26
condition that if that file that you
3:29
talking about
3:30
exist or not which is let me take for
3:33
example we are converting input.txt so
3:36
we are simply checking if this file is
3:38
already present or not so if this file
3:41
is not present then we will simply tell
3:43
the user that file not found so it's a
3:47
command line terminal application so it
3:49
will simply turn tell you in the
3:50
terminal that the file doesn't exist you
3:53
need to create it so if the file exist
3:56
then we need to Simply read the content
3:58
of the file
4:00
so now to read the content we will
4:02
simply declare a variable of string and
4:05
then again you'll use the file class it
4:07
does contain a method read all text if
4:10
you see read all
4:13
text so in this file class we will say
4:16
read all
4:20
text and we are providing the path of
4:22
the file which is input.
4:26
txt so whatever is the text present in
4:29
this this input.txt we'll read all the
4:31
text and store it in the text variable
4:33
then we need to create the PDF document
4:37
creating it we'll use the document class
4:39
you'll make a new object of it it is
4:42
present inside the it text sharp Library
4:45
where you need to specify the page size
4:47
so you simply say the page size various
4:50
sizes are available A4 A5 the most
4:53
common one is A4 size so we'll set the
4:56
page size of this to A4 and then we need
5:00
to Simply say PDF writer again class in
5:03
itex
5:05
shop using this class we will
5:10
simply get get instance this method
5:13
we'll use we'll pass the actual document
5:16
and the new file
5:20
stream file stream class and here you
5:23
need to pass the output file name so let
5:25
me say I need to give it output PDF the
5:28
name or I can change it
5:30
suppose uh free media
5:33
tools.pdf
5:35
and then the file mode will be here to
5:39
actually create because we are creating
5:40
the file that's why this is actual line
5:44
that guys you will see that we have
5:45
providing the output file right here and
5:47
then the file mode is
5:50
create so after you do this uh we need
5:54
to open the PDF document and insert the
5:57
text so we need to open the the PDF
6:00
document for opening it we'll simply say
6:02
document. open open function and then to
6:06
add it you'll simply say document. add
6:09
so you can add images text as well so
6:12
now to add that text we'll simply say
6:14
new
6:15
paragraph text so basically it will add
6:19
that text into that PDF document and
6:23
then you'll simply close the document so
6:27
you'll simply use the close function to
6:29
actually close the PDF all the source
6:32
code that I'm writing is given in the
6:34
description so if you need the full
6:35
source code you can go to the
6:37
description so console right line and
6:40
simply say PDF
6:49
generated that's very simple program
6:52
guys you look at in Tex shop we load the
6:55
file we make the document set the page
6:58
size then we put the file name output
7:01
file name open the PDF add the text in
7:04
the form of paragraph and close it if I
7:07
now run this you will see that PDF
7:09
generated successfully and if I now
7:12
check my
7:13
directory you will actually see free
7:15
media tools uh PDF will be generated if
7:20
I open this you will see
7:23
that all the text which are which is
7:26
actually written in this input txt file
7:28
is successfully fully converted so it
7:31
has contained two pages in The PDF so it
7:33
totally depends upon the text let me
7:36
again copy this text and paste
7:39
it it can have as many text as you want
7:42
let's Suppose there is no restriction
7:44
that how many characters you can convert
7:47
it can have you can see I'm just
7:48
pasting and now if I do the same thing
7:51
you will see it will contain lot more
7:57
pages so refresh
8:02
so we need to Simply Save this file
8:03
first of
8:15
all so the file is
8:18
generated and now you can see that guys
8:20
the pages have been turned to 26 so you
8:24
can
8:25
see as you increase the length of the
8:27
text
8:29
so it's a very good example guys in itex
8:33
Shar how you can do this inside net in C
8:36
itex Shar it's a really good Library
8:38
thank you very much for watching this
8:40
video please hit that like button
8:41
subscribe the channel for more videos
8:43
like this and I will be seeing you in
8:45
the next one
#Windows & .NET
#Open Source
