?
Solved

itextsharp image could not display in pdfpcell

Posted on 2014-01-12
4
Medium Priority
?
2,063 Views
Last Modified: 2014-02-04
not sure what causes the problem

when i use plain text and colspan 3, it seems working fine

but when i use colspan 4 and try to place an image, it seems got problem

   Document document = new Document(PageSize.A4, 20f, 20f, 20f, 20f);
       
        try
        {

            PdfWriter.GetInstance(docu<wbr ></wbr>ment, new FileStream(filename, FileMode.Create));
       
         //   PdfAWriter.GetInstance(doc<wbr ></wbr>ument, new FileStream(Request.Physica<wbr ></wbr>lApplicati<wbr ></wbr>onPath + "Scripts\\1.pdf", FileMode.Create));
        //    document.Open();
            PdfPTable table = new PdfPTable(4);
            table.TotalWidth = 400f;
            //fix the absolute width of the table
            table.LockedWidth = true;

            //relative col widths in proportions - 1/3 and 2/3
            float[] widths = new float[] { 2f, 4f, 6f };
            table.SetWidths(widths);
            table.HorizontalAlignment = 0;
            //leave a gap before and after the table
            table.SpacingBefore = 20f;
            table.SpacingAfter = 30f;

            PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
         
            cell.Colspan = 4;
            cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right

            table.AddCell(cell);

            iTextSharp.text.Image image = iTextSharp.text.Image.GetI<wbr ></wbr>nstance(@"<wbr ></wbr>~/images/he<wbr ></wbr>ader.jpg")<wbr ></wbr>;
            image.ScaleAbsolute(800f, 300f);
            PdfPCell cell1 = new PdfPCell(image);

            cell1.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
          
            table.AddCell(cell1);
            table.AddCell("Col 1 Row 1");
            table.AddCell("Col 2 Row 1");
            table.AddCell("Col 1 Row 1");
            table.AddCell("Col 2 Row 1");
            table.AddCell("Col 3 Row 1");
            table.AddCell("Col 1 Row 2");
            table.AddCell("Col 2 Row 2");
            table.AddCell("Col 3 Row 2");
                               
            document.Open();
            document.Add(table);
        }
        catch (Exception ex)
        {
        }
        finally
        {
            document.Close();
            ShowPdf(filename);
          //  Response.Redirect("Scripts<wbr ></wbr>/1.pdf");
        }

Open in new window

0
Comment
Question by:doramail05
[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
  • 4
4 Comments
 
LVL 1

Author Comment

by:doramail05
ID: 39775911
Notice that if i put the image before table, it will appear the image with the following code,

just what if it is inside the cell? which i couldnt display 'em inside the cell

  PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create));
            document.Open();

          //  document.Add(new Paragraph("JPG"));
            iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imagepath + "/header1.jpg");
            jpg.ScaleAbsolute(550f, 120f);
            document.Add(jpg);

        // //   PdfAWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "Scripts\\1.pdf", FileMode.Create));
        ////    document.Open();
            PdfPTable table = new PdfPTable(3);
            table.TotalWidth = 400f;
0
 
LVL 1

Author Comment

by:doramail05
ID: 39775940
just discover that it just have to put



cell.AddElement(jpg);

table.AddCell(cell);



now was wondering what if would like to add a background image to the table, but couldnt find quite relevant resource on net,
0
 
LVL 1

Accepted Solution

by:
doramail05 earned 0 total points
ID: 39834876
solved with this

iTextSharp.text.Image imgmrstext = iTextSharp.text.Image.GetInstance(imagepath + "/mrs_text.jpg");
            imgmrstext.ScaleAbsolute(35f, 35f);

   PdfPCell cell118 = new PdfPCell();
            cell118.BorderColor = iTextSharp.text.BaseColor.WHITE;
            cell118.AddElement(imgmrstext);
            table.AddCell(cell118);
0
 
LVL 1

Author Closing Comment

by:doramail05
ID: 39834877
figured out
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

*Adobe Acrobat 9 was used for this article. Particular steps may vary depending on software versions. 1. Create a framework of your form in Word, leaving space where you’d ultimately like the Adobe fields to appear.  (Note: I use the blank lines …
In a previous article published here at Experts Exchange, Signature Image with Transparent Background (http://www.experts-exchange.com/Web_Development/Document_Imaging/A_12380-Signature-Image-with-Transparent-Background.html), I explained how to cre…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Suggested Courses

770 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