locked
label using itextsharp on PDF RRS feed

  • Question

  • User-1254072439 posted

    how can we print label (width = 70mm and height=110mm)  with '0' margin (left,right,button and top) using by itextsharp. 

    Thursday, May 26, 2016 2:11 PM

Answers

  • User-821857111 posted

    iTextSharp works with Points (72 to an inch) rather than millimeters, so you first need to convert your millimeters to points (https://www.google.co.uk/?ion=1&espv=2#q=convert%20millimeters%20to%20points). Then, depending on your requirement, you can either use a table for repeated content (multiple labels on the same page) or you can use absolute positioning to place your single label on the document as you need e.g:

    Document document = new Document();
    path = Path.Combine(path, "Label.pdf");
    using (FileStream fileStream = new FileStream(path, FileMode.Create))
    {
        PdfWriter writer = PdfWriter.GetInstance(document, fileStream);
        document.Open();
        PdfContentByte canvas = writer.DirectContent;
        BaseFont baseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
        canvas.SetFontAndSize(baseFont, 8f);
        ColumnText column = new ColumnText(canvas);
        column.SetSimpleColumn(200,312,20,700); //width, height, xPos, yPos
        Chunk c1 = new Chunk("Recipient Name\n");
        Chunk c2 = new Chunk("Street Name\n");
        Chunk c3 = new Chunk("City\n");
        Chunk c4 = new Chunk("Post Code\n");
        column.AddText(c1);
        column.AddText(c2);
        column.AddText(c3);
        column.AddText(c4);
        column.Go();
        document.Close();
    }

    Here's an article on using tables in iTextsharp: http://www.mikesdotnetting.com/article/86/itextsharp-introducing-tables, and here's one on using columns to do the same thing: http://www.mikesdotnetting.com/article/89/itextsharp-page-layout-with-columns

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 27, 2016 7:42 AM

All replies

  • User-821857111 posted

    iTextSharp works with Points (72 to an inch) rather than millimeters, so you first need to convert your millimeters to points (https://www.google.co.uk/?ion=1&espv=2#q=convert%20millimeters%20to%20points). Then, depending on your requirement, you can either use a table for repeated content (multiple labels on the same page) or you can use absolute positioning to place your single label on the document as you need e.g:

    Document document = new Document();
    path = Path.Combine(path, "Label.pdf");
    using (FileStream fileStream = new FileStream(path, FileMode.Create))
    {
        PdfWriter writer = PdfWriter.GetInstance(document, fileStream);
        document.Open();
        PdfContentByte canvas = writer.DirectContent;
        BaseFont baseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
        canvas.SetFontAndSize(baseFont, 8f);
        ColumnText column = new ColumnText(canvas);
        column.SetSimpleColumn(200,312,20,700); //width, height, xPos, yPos
        Chunk c1 = new Chunk("Recipient Name\n");
        Chunk c2 = new Chunk("Street Name\n");
        Chunk c3 = new Chunk("City\n");
        Chunk c4 = new Chunk("Post Code\n");
        column.AddText(c1);
        column.AddText(c2);
        column.AddText(c3);
        column.AddText(c4);
        column.Go();
        document.Close();
    }

    Here's an article on using tables in iTextsharp: http://www.mikesdotnetting.com/article/86/itextsharp-introducing-tables, and here's one on using columns to do the same thing: http://www.mikesdotnetting.com/article/89/itextsharp-page-layout-with-columns

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 27, 2016 7:42 AM
  • User-1254072439 posted

    Thank you Mike

    Sunday, May 29, 2016 7:15 AM