Set the position of text or image in pdf using itextsharp (C # / asp.net)

I have added image and text to pdf using iTextSharp. However, I would like to place the image and text in a specific position in pdf. How to do it?

So far I tried

img.SetAbsolutePosition(10000f,10000f);

But it does not work. Here is my complete pdf creation code,

 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();
    }

 }
+4
source share
1 answer

If you tried img.SetAbsolutePosition(10000f,10000f);, then your image comes out of the visible area of ​​the PDF. You create yours Documentas follows:

Document document = new Document(PageSize.A4, 188f, 88f, 5f, 10f);

This means that the page size is 595 x 842 user units. Use x = 10000and y = 10000does not fit inside the 595 x 842 rectangle.

:

img.SetAbsolutePosition(0,0);

, .

, iText . . :

, SetAbsolutePosition().

Update:

. . . -.

:

. ?, showTextAligned():

ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
    new Phrase("Some text"), 100, 100, 0);

, , , , canvas.

:

?

ColumnText ct = new ColumnText(cb);
ct.SetSimpleColumn(rect);
ct.AddElement(new Paragraph("This is the text added in the rectangle"));
ct.Go();

, , , cb rect.

+8

All Articles