Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

 }
like image 792
Shekar.gvr Avatar asked May 26 '16 10:05

Shekar.gvr


People also ask

How to set image position in Pdf?

Place an image or object into a PDFOpen the PDF in Acrobat, and then choose Tools > Edit PDF > Add Image . In the Open dialog box, locate the image file you want to place. Select the image file, and click Open. Click where you want to place the image, or click-drag to size the image as you place it.

What is ITextSharp used for?

Itextsharp is an advanced tool library which is used for creating complex pdf repors. itext is used by different techonologies -- Android , . NET, Java and GAE developer use it to enhance their applications with PDF functionality.

How to add image as header in Pdf in java?

To add an image to the pdf, create and an object of the image that is required to be added and add it using the add() method of the Document class. You can insert the image in a desired position on the document using the method setFixedPosition() of the Image class.


1 Answers

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:

  • How should I interpret the coordinates of a rectangle in PDF?
  • Where is the origin (x,y) of a PDF page?
  • ...

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.

like image 142
Bruno Lowagie Avatar answered Oct 16 '22 01:10

Bruno Lowagie