Reputation: 1689
I have added an image and some text in the pdf using iTextSharp
. However, I would like to position my image and text at specific position in the pdf. How do I do it?
So far I tried,
img.SetAbsolutePosition(10000f,10000f);
But it is not working. Here is my complete code for generating the pdf,
private void generatepdf(byte[] byteImage)
{
//byte[] imageBytes = Convert.FromBase64String(base64);
string text1= "Some Text";
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(byteImage);
image.ScalePercent(0.3f * 100);
string logopath = Server.MapPath("~/images/img1.png");
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(logopath);
img.SetAbsolutePosition(1000f,1000f);
img.ScaleAbsolute(1500f, 0f);
img.ScalePercent(0.5f*100);
Paragraph ShopName = new Paragraph(text1);
Paragraph id = "Some Text";
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
Document document = new Document(PageSize.A4, 188f, 88f, 5f, 10f);
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
document.Add(img);
document.Add(ShopName);
document.Add(image);
document.Add(id);
document.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=QRCode.pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
}
}
Upvotes: 7
Views: 39736
Reputation: 331
It's an old question, BUT in case anyone comes throught the pain of setting an image with a certain layout. At least from my experience it's a good idea to set the position by using tables.
PdfPTable tableSignature = new PdfPTable(4);
tableSignature.DefaultCell.Border = 0;
tableSignature.DefaultCell.BorderColor = Color.WHITE;
tableSignature.WidthPercentage = 90;
var encodedImage = signatureString.Split(',')[1];
var bytesSignature = Convert.FromBase64String(encodedImage);
iTextSharp.text.Image imageSignature = iTextSharp.text.Image.GetInstance(bytesSignature);
imageSignature.BorderColor = iTextSharp.text.Color.BLACK;
// this size seems right but, we need to check in more samples.
imageSignature.ScaleToFit(120, 60);
imageSignature.Alignment = iTextSharp.text.Image.ALIGN_BOTTOM;
PdfPCell cellSignature = new PdfPCell(imageSignature);
cellSignature.HorizontalAlignment = PdfPCell.ALIGN_TOP;
cellSignature.BorderColor = iTextSharp.text.Color.WHITE;
tableSignature.AddCell(cellSignature);
tableSignature.AddCell(" ");
tableSignature.AddCell(" ");
PdfPCell cellDateSignatureText = new PdfPCell(new Phrase(dateFilledInDocument));
((Chunk)(cellDateSignatureText.Phrase[0])).Font = new iTextSharp.text.Font(((Chunk)(cellDateSignatureText.Phrase[0])).Font.Family, 12f);
cellDateSignatureText.BorderColor = iTextSharp.text.Color.WHITE;
cellDateSignatureText.HorizontalAlignment = PdfPCell.ALIGN_BOTTOM;
cellDateSignatureText.VerticalAlignment = PdfCell.ALIGN_BOTTOM;
cellDateSignatureText.BorderWidthBottom = 1;
cellDateSignatureText.BorderColorBottom = Color.BLACK;
tableSignature.AddCell(cellDateSignatureText);
PdfPCell signatureCellText = new PdfPCell(new Phrase("Signature"));
((Chunk)(signatureCellText .Phrase[0])).Font = new iTextSharp.text.Font(((Chunk)(signatureCellText .Phrase[0])).Font.Family, 12f);
signatureCellText .BorderColor = iTextSharp.text.Color.WHITE;
signatureCellText .HorizontalAlignment = PdfPCell.ALIGN_BOTTOM;
tableSignature.AddCell(signatureCellText);
tableSignature.AddCell(" ");
tableSignature.AddCell(" ");
tableSignature.AddCell("Date");
document.Add(tableSignature);
For example in the code above we inserted a signature and a date at the end of a pdf.
Hope this works for someone out there.
Upvotes: 1
Reputation: 77606
If you tried img.SetAbsolutePosition(10000f,10000f);
then your image is way out of the visible area of the PDF. You are creating your Document
like this:
Document document = new Document(PageSize.A4, 188f, 88f, 5f, 10f);
This means that the size of the page is 595 x 842 user units. Using x = 10000
and y = 10000
doesn't fit inside a rectangle of 595 x 842.
Please try:
img.SetAbsolutePosition(0,0);
When you use these coordinates, the lower-left corner of the image will coincide with the lower-left corner of the page.
Please consult the official iText documentation and search for coordinate system. See for instance:
This will help you find how to define the coordinates for the SetAbsolutePosition()
method.
Update:
You are also asking about adding text at absolute positions. Here we have to make the distinction between a single line of text and a block of text. See also the section Absolute positioning of text on the official web site.
A single line of text:
See for instance How to position text relative to page? and you'll find the showTextAligned()
method:
ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
new Phrase("Some text"), 100, 100, 0);
Please make sure that you read other examples to so that you discover what the canvas
object is about.
A block of text:
Take a look at How to add text inside a rectangle?
ColumnText ct = new ColumnText(cb);
ct.SetSimpleColumn(rect);
ct.AddElement(new Paragraph("This is the text added in the rectangle"));
ct.Go();
Please take a look at the full example to find out what cb
and rect
are about.
Upvotes: 12