Friday, March 22, 2013

Generate Pdf File Wih iTextSharp


using System;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Text;
using iTextSharp.text.pdf.parser;
namespace SampleCSharpApplication
{
    public partial class Test1 : System.Web.UI.Page
    {
        protected void Button1_Click(object sender, EventArgs e)
        {
            var doc = new Document();
            string Path = Server.MapPath("Doc");
            PdfWriter.GetInstance(doc, new FileStream(Path + "/Test.pdf", FileMode.Create));
            doc.Open();
            TextReader txt = new StreamReader(Path + "/Test.txt");
            doc.Add(new Paragraph(txt.ReadToEnd()));
            Image img = Image.GetInstance(Path + "/logo.png");
            img.ScaleAbsolute(500, 200);
            // Read PDF File
            StringBuilder sb = new StringBuilder();
            using (PdfReader reader = new PdfReader(Path + "/Receipt.pdf"))
            {
                ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
                for (int page = 0; page < reader.NumberOfPages; page++)
                {
                    string text = PdfTextExtractor.GetTextFromPage(reader, page + 1, strategy);
                    if (!String.IsNullOrEmpty(text))
                    {
                        sb.Append(Encoding.UTF8.GetString(ASCIIEncoding.
                                       Convert(Encoding.Default, Encoding.UTF8,
                                       Encoding.Default.GetBytes(text))));
                    }
                }
            }
            doc.Add(new Paragraph(sb.ToString()));
            doc.Add(img);
            doc.Close();
        }
    }
}

Download Source Code Here...

6 comments:

Anonymous said...

Hey thеre I am ѕο hаρpy I fοunԁ уouг blog, I really found you by accident,
while I waѕ seаrching on Aol for somеthing else,
Nonetheleѕs I аm here now and would јust liκe to sаy cheers foг a
marvelous рost and a all round entertаining blοg (I
alsο loѵe the theme/design), I don't have time to read it all at the minute but I have book-marked it and also added in your RSS feeds, so when I have time I will be back to read much more, Please do keep up the great job.

Feel free to surf to my web page: video chat

Anonymous said...

Οh my goodneѕs! Impressіѵе аrticle dude!

Mаny thanks, However ӏ am hаѵing
problems with уouг RSS. I ԁon't know the reason why I cannot join it. Is there anybody else getting identical RSS issues? Anyone that knows the answer can you kindly respond? Thanx!!

Feel free to surf to my web-site ... cellulite

Anonymous said...

Way cοol! Some very valiԁ pointѕ!
I appreciate уou wгiting this artiсle plus the rest of the site is also reаlly good.



Viѕit my homepagе ... http://www.atfxc.com/blogs/6806/7037/two-times-articles-page-six-mont

Anonymous said...

obviοusly like уοuг website howeѵer you nеed to take а look
at the sρеlling оn quіte a feω of yοur ρosts.

Many of them are гife with spelling problems anԁ I in
finding it very troublesome to іnform the truth then
again I'll surely come again again.

my webpage opposite ends

Anonymous said...

Hmm is anуone elѕe haѵіng problеms wіth the рiсtures on thіs
blоg loading? I'm trying to find out if its a problem on my end or if it'ѕ the blog.
Anу feedbacκ ωould be grеatlу аppгeciated.


mу wеbsite ... oral sex

Anonymous said...

VuiQthNyaEup [url=http://www.adidasyasuyijp.com/]アディダス スタンスミス[/url]OtvGwwTduRat [url=http://www.adidasyasuyijp.com/jeremy-scott-c-14.html]jeremy scott[/url]AzsPngPpjZwu [url=http://www.adidasyasuyijp.com/アディダス-スタンスミス-c-13.html]アディダス-スタンスミス[/url]TjpVgxFalCny [url=http://www.adidasyasuyijp.com/サッカー-c-8.html]サッカ[/url]