Solved

Adding image & text to PDF using iTextSharp

Posted on 2010-08-19
3
1,337 Views
Last Modified: 2012-05-10
Hello,

I want to do something like what' shown in code section using iTextSharp. I've got the GIF image and two texts that are to be aligned to the left of it, with some spacing between the image and between the lines as well ( a simple newline is OK). Then I want to flush the whole thing and start writing below the image, from the left border. The top of the image is to be aligned with the top of Text1. Can you provide me with a working code, please? I've been trying to achieve this for a while, but I generally fail with the top-alignment (IMage.ALIGN_TOP does not seem to work) and don't know how to skip below the image after Text2. Please do not provide links to general iTextSharp guides, just working code. Thanks.

=========  Text 1
|       |
| IMAGE |  Text 2
|       |
=========

Some other text

Open in new window

0
Comment
Question by:bovlk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 16

Accepted Solution

by:
Vikram Singh Saini earned 500 total points
ID: 33481943
Hi,

Please check the code files provided by Author at link:
http://blog.rubypdf.com/itextsharp/tutorial01/

Regards,
VSS
0
 
LVL 6

Expert Comment

by:r3nder
ID: 33483145
Ty this
//Try this
//Add reference to the ITextSharp.dll
using iTextSharp.text;  
using iTextSharp.text.pdf; 
//Add Namespace
//Lets see this code which will be place image into PDF document

//view sourceprint?01 // Now image in the pdf file  

       string imageFilePath = Server.MapPath(".") + "/image/Sunset.jpg";  

       iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);  

       //Resize image depend upon your need  

       jpg.ScaleToFit(280f, 260f);  

       //Give space before image  

       jpg.SpacingBefore = 30f;  

       //Give some space after the image  

       jpg.SpacingAfter = 1f;  

       jpg.Alignment = Element.ALIGN_LEFT;  

       doc.Add(paragraph); // add paragraph to the document  

          

       doc.Add(jpg); //add an image to the created pdf document 

Now to see full code you can place the below code in any event like on button click

In C# sample code function

view sourceprint?01 private void CreatePDFFile()  

 {  

         Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);  

         try 

         {  

            string pdfFilePath = Server.MapPath(".") + "/pdf/myPdf.pdf";  

    

             //Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin  

             PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdfFilePath, FileMode.Create));  

             doc.Open();//Open Document to write  

    

             //Write some content into pdf file  

             Paragraph paragraph = new Paragraph("This is my first line using Paragraph.");  

               

             // Now image in the pdf file  

            string imageFilePath = Server.MapPath(".") + "/image/Sunset.jpg";  

             iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);  

    

             //Resize image depend upon your need  

             jpg.ScaleToFit(280f, 260f);  

    

             //Give space before image  

             jpg.SpacingBefore = 30f;  

    

             //Give some space after the image  

            jpg.SpacingAfter = 1f;  

             jpg.Alignment = Element.ALIGN_CENTER;  

    

             doc.Add(paragraph); // add paragraph to the document  

                

             doc.Add(jpg); //add an image to the created pdf document  

         }  

         catch (DocumentException docEx)  

         {  

             //handle pdf document exception if any  

         }  

         catch (IOException ioEx)  

         {  

             // handle IO exception  

         }  

         catch (Exception ex)  

         {  

             // ahndle other exception if occurs  

         }  

         finally 

         {  

             //Close document and writer  

             doc.Close();  

               

         }  

    } 

Open in new window

0
 

Author Comment

by:bovlk
ID: 33514566
I finally placed all the things into a table which solved all the problems easily. Anyway, assigning points to vs00saini for pointing me to the guide.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question