iTextSharp - Inserting Text as an Image

  • Follow


I'm using iTextSharp to populate a PDF form-field template.

Forgive me if this is a dumb or obvious question, but would it be
possible to convert a string of characters into an image and insert it
into the document using PdfContentByte? For example, below is the code
I'm using to overlay a barcode onto the document:

                PdfContentByte cb = stamper.GetUnderContent(1);
                Barcode39 code39BCD = new Barcode39();
                code39BCD.Code = SOMEVARIABLE;
                code39BCD.Size = 0.65f;
                code39BCD.BarHeight = 16;
                code39BCD.Extended = false;
                Image imageCode39 = code39BCD.CreateImageWithBarcode
(cb, null, null);
                imageCode39.SetAbsolutePosition(235, 578);
                cb.AddImage(imageCode39);

....there is no object I can find equivilent to "Barcode39" which will
take a string and a font and convert that text to a image with
something like "CreateImageWithBarcode".

The reason for inserting a text image rather than using an embedded
font is that I cannot embed any fonts in this particular document.

Thanks in advance.
0
Reply gjwatersjr994 (4) 8/28/2009 11:14:54 PM

I'm not sure about your Barcode class. What does it do?

I created images of Barcodes a few time ago with the normal "Drawing"
classes of .NET and inserted them into PDF files with iTextSharp later.
That's no problem.

        public static String createBarcodeFromString(String s)
        {
            PrivateFontCollection pfc = new PrivateFontCollection();
            pfc.AddFontFile(Application.StartupPath + "\\BC.ttf");
            Bitmap b = new Bitmap(1, 1);
            Graphics g = Graphics.FromImage(b);
            SizeF size = g.MeasureString(s, new Font(pfc.Families[0], 50));
            b = new Bitmap((int)size.Width, (int)size.Height);
            g = Graphics.FromImage(b);
            g.FillRectangle(Brushes.White, 0, 0, (int)size.Width,
(int)size.Height);
            g.DrawString(s, new Font(pfc.Families[0], 50), Brushes.Black,
new Point(0, 0));
            String save = Logik.getPaths()[1] + "\\" +
DateTime.Now.Ticks.ToString() + ".jpg";
            b.Save(save, System.Drawing.Imaging.ImageFormat.Jpeg);
            return save;
        }

This snippet is not the best solution and I know it. But I think you can
take the parts that you'd like to use in your project. This function gets a
String as an argument and returns the path to the saved JPG file.
"BC.ttf" is a free Code39 font file, but you can use any font file you
prefer.
Remember to add the * charakter at the beginning and at the end of your
Barcode string.

MfG
S.H.


0
Reply SH 9/4/2009 7:57:55 PM


On Sep 4, 3:57=A0pm, "SH" <trash_mail_acco...@web.de> wrote:
> I'm not sure about your Barcode class. What does it do?

iTextSharp includes special Barcode classes:
http://twinzlover.blogspot.com/2008/01/barcode-generation-using-itext.html

The barcodes are inserted as vector graphics (but not as fonts).
0
Reply Ross 9/21/2009 5:45:08 PM

2 Replies
885 Views

(page loaded in 0.094 seconds)

Similiar Articles:













7/20/2012 10:12:24 PM


Reply: