PDF - com imagem

<< Click to Display Table of Contents >>

Navigation:  ASP.NET > Dicas avançadas > PDF com iTextSharp >

PDF - com imagem

Previous pageReturn to chapter overviewNext page

Tela

 

pdf5

 

Default.aspx

 

Nada

 

Default.aspx.cs

 

using System;

using System.IO;

using iTextSharp.text;

using iTextSharp.text.pdf;

 

public partial class _Default : System.Web.UI.Page

{

  protected void Page_Load(object sender, EventArgs e)

   {

      /*Logo Path*/

      string strLogoPath = Server.MapPath("eu.jpg");

      Document doc = new Document(PageSize.LETTER, 25F, 25F, 50F, 25F);

      PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("arquivo.pdf"), FileMode.Create));

       doc.Open();

      Rectangle page = doc.PageSize;

      Font Verdana = FontFactory.GetFont("Verdana", 14F, Font.NORMAL, BaseColor.BLACK);

      PdfPTable table = new PdfPTable(2);

       table.TotalWidth = page.Width;

      PdfPCell cell = new PdfPCell();

       cell.Colspan = 3;

       cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right

       iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(strLogoPath);

       AddImageInCell(cell, image, 100f, 100f, 1);

       cell.Border = 0;

       AddtextCell(table, cell, 0, 0, 0);

      PdfPCell cell2 = new PdfPCell();

      PdfPTable table2 = new PdfPTable(1);

      PdfPCell line_2 = new PdfPCell(new Phrase("Nome: Flavio de Souza Junior", Verdana));

       AddtextCell(table2, line_2);

      PdfPCell line_3 = new PdfPCell(new Phrase("Email: flavio.junior@gmail.com", Verdana));

       AddtextCell(table2, line_3);

      PdfPCell line_4 = new PdfPCell(new Phrase("Empresa: Market Automações Ltda", Verdana));

       AddtextCell(table2, line_4);

       cell2.AddElement(table2);

       cell2.Border = 0;

       AddtextCell(table, cell2, 0, 0, 0);

       doc.Add(table);

       doc.Close();

 

       MostrarPdf(Server.MapPath("arquivo.pdf"));

   }

 

  private static void AddImageInCell(PdfPCell cell, iTextSharp.text.Image image, float fitWidth, float fitHight, int Alignment)

   {

       image.ScaleToFit(fitWidth, fitHight);

       image.Alignment = Alignment;

       cell.AddElement(image);

   }

 

  private void AddtextCell(PdfPTable table, PdfPCell cell)

   {

       cell.Colspan = 3;

       cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right

       cell.Border = 0;

       table.AddCell(cell);

   }

 

  private void AddtextCell(PdfPTable table, PdfPCell cell, float paddingLeft, float paddingRight)

   {

       cell.Colspan = 3;

       cell.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right

       cell.PaddingLeft = paddingLeft;

       cell.PaddingRight = paddingRight;

       cell.Border = 0;

       table.AddCell(cell);

   }

 

  private void AddtextCell(PdfPTable table, PdfPCell cell, float paddingLeft, float paddingRight, int hAlign)

   {

       cell.Colspan = 3;

       cell.HorizontalAlignment = hAlign; //0=Left, 1=Centre, 2=Right

       cell.PaddingLeft = paddingLeft;

       cell.PaddingRight = paddingRight;

       cell.Border = 0;

       table.AddCell(cell);

   }

 

  private static void AddtextCell(PdfPTable table, PdfPCell cell, int Colspan, int HorizontalAlignment, int Border)

   {

       cell.Colspan = Colspan;

       cell.HorizontalAlignment = HorizontalAlignment; //0=Left, 1=Centre, 2=Right

       cell.Border = Border;

       table.AddCell(cell);

   }

 

  private void MostrarPdf(string strS)

   {

       Response.ClearContent();

       Response.ClearHeaders();

       Response.ContentType = "application/pdf";

       Response.AddHeader("Content-Disposition", "attachment; filename=" + strS);

       Response.TransmitFile(strS);

       Response.End();

 

       Response.Flush();

       Response.Clear();

   }

}