Sivakumar

Sivakumar

  • NA
  • 551
  • 219.6k

how to align itextsharp image top left in c#

Apr 26 2016 2:15 AM
Hi This is Generated pdf :
 
 
 
 
and This is my download pdf code :
 
Byte[] bytes;
int orderID = Convert.ToInt32(e.CommandArgument);
var templateData = ordersBL.GetTemplateDetails(orderID);
// Document document = new Document(PageSize.A4, 10, 10, 10, 10);

using (MemoryStream ms = new MemoryStream())
{
using (Document document = new Document(PageSize.POSTCARD.Rotate()))
{

PdfWriter writer = PdfWriter.GetInstance(document, ms);
Phrase phrase = null;
PdfPCell cell = null;
PdfPTable table = null;
foreach (var temp in templateData)
{
string message = temp.Message;
string tempimage = Convert.ToBase64String(temp.logo);
string base64 = tempimage;
byte[] imageBytes = Convert.FromBase64String(base64);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageBytes);
if (image.Height > image.Width)
{
//Maximum height is 800 pixels.
float percentage = 0.0f;
percentage = 700 / image.Height;
image.ScalePercent(percentage * 100);
}
else
{
//Maximum width is 600 pixels.
//float percentage = 0.0f;
// percentage = 140 / image.Width;
//image.ScalePercent(percentage * 100);
image.ScaleToFit(100f, 100f);
//image.Alignment = iTextSharp.text.Image.ALIGN_LEFT;

image.SetAbsolutePosition(document.Left, document.Top - 180);

image.BorderWidth= 2f;
}
//only happens on the first run!
if (!document.IsOpen())
{
//Open the document for writing
document.Open();
}

table = new PdfPTable(1);
table.TotalWidth = 400f;
table.LockedWidth = true;
table.SetWidths(new float[] { 1f });

//Company Name and Address
phrase = new Phrase();
//phrase.Add(new Chunk("Microsoft Northwind Traders Company\n\n", FontFactory.GetFont("Arial", 16, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.RED)));
phrase.Add(new Chunk(temp.Number + "," + "\n", FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK)));
phrase.Add(new Chunk(temp.Street + "," + "\n", FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK)));
//phrase.Add(new Chunk(temp.City + "," + "\n", FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK)));
phrase.Add(new Chunk(temp.State + "," + "\n", FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK)));
phrase.Add(new Chunk(temp.Zip + "\n", FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_RIGHT);
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
table.AddCell(cell);

document.Add(table);
document.Add(image);


using (var htmlWorker = new HTMLWorker(document))
{

//HTMLWorker doesn't read a string directly but instead needs a TextReader (which StringReader subclasses)
using (var sr = new StringReader(message))
{

//Parse the HTML
htmlWorker.Parse(sr);
}
}

Paragraph paragraph = new Paragraph();
paragraph.SpacingBefore = 10;
paragraph.SpacingAfter = 10;
paragraph.Alignment = Element.ALIGN_LEFT;
// paragraph.Font = FontFactory.GetFont(FontFactory.HELVETICA, 12f, BaseColor.GREEN);
// paragraph.Add(text);
document.Add(paragraph);
document.NewPage();

}
document.Close();

}


bytes = ms.ToArray();


}

Response.Clear();
string pdfName = "template";
Response.AddHeader("Content-Disposition", "attachment; filename=" + orderID + "_" + "template" + ".pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();



}
 
Please tell me how to align image logo  top left 

Answers (2)