f



iTextSharp - PDFStamper & Barcode

I'm mass-filling a template with PDFStamper via iTextSharp in C#. It's
running successfully. I'd like to overlay a barcode on the PDF from
the same script. I've seen Java samples to do this, but am having no
luck getting them to work in my app. Below is the Barcode-code I have
now. It runs without error, but no barcode is produced on the
documents.

PdfContentByte cb = stamper.GetUnderContent(1);
PdfLayer layer = new PdfLayer("one", stamper.Writer);
Barcode39 barcodeParcel = new Barcode39();
barcodeParcel.Code = "TESTVALUE";
barcodeParcel.StartStopText = false;
Image imageBarcodeParcel = barcodeParcel.CreateImageWithBarcode(cb,
null, null);
stamper.Writer.Add(new Phrase(new Chunk(imageBarcodeParcel, 225,
333)));
stamper.Writer.Add(new Phrase(new Chunk(imageBarcodeParcel, 225,
157)));

Have any of you got this to work? If so, I'd appreciate any samples.

Thanks,
-Gary

0
7/24/2007 12:26:54 AM
comp.text.pdf 5600 articles. 0 followers. ramon (1518) is leader. Post Follow

5 Replies
6755 Views

Similar Articles

[PageSpeed] 5

This (java) code works for me in my pdftk extension at
http://www.esnips.com/web/PDFTools :

	/**
		Adds a barcode to <code>cb</code>
	*/
	private void addBarcode(PdfContentByte cb, String symbology, String
value, float x1, float y1, float x2, float y2)
	{
		float h = y2-y1;
		float w = x2-x1;

		try
		{
			if(symbology.equalsIgnoreCase("postnet")) {
				BarcodePostnet postNetBCD = new BarcodePostnet();
				postNetBCD.setCode(value);
				Image imagePostnet = postNetBCD.createImageWithBarcode(cb, null,
null);
				imagePostnet.setAbsolutePosition(x1, y1);
				cb.addImage(imagePostnet);
			}
			else if(symbology.equalsIgnoreCase("code128")) {
				Barcode128 code128BCD = new Barcode128();
				code128BCD.setCode(value);
				code128BCD.setFont(null);
				code128BCD.setBarHeight((int)h);
				Image imageCode128 = code128BCD.createImageWithBarcode(cb, null,
null);
				imageCode128.setAbsolutePosition(x1, y1);
				cb.addImage(imageCode128);
			}
			else if(symbology.equalsIgnoreCase("code39")) {
				Barcode39 code39BCD = new Barcode39();
				code39BCD.setCode(value);
				code39BCD.setFont(null);
				code39BCD.setBarHeight((int)h);
				Image imageCode39 = code39BCD.createImageWithBarcode(cb, null,
null);
				imageCode39.setAbsolutePosition(x1, y1);
				cb.addImage(imageCode39);
			}
			else if(symbology.equalsIgnoreCase("pdf417")) {
				BarcodePDF417 pdf417BCD = new BarcodePDF417();
				pdf417BCD.setText(value);
				Image imagePDF417 = pdf417BCD.getImage();
				imagePDF417.scaleAbsolute(w, h);
				imagePDF417.setAbsolutePosition(x1, y1);
				cb.addImage(imagePDF417);
			}
		}
		catch (Exception e)
		{
			System.err.println(e.toString());
		}
	}

0
chicks
7/24/2007 2:50:57 PM
On Jul 24, 7:50 am, chicks <sea....@gmail.com> wrote:
> This (java) code works for me in my pdftk extension athttp://www.esnips.com/web/PDFTools:
>

Thanks for the help! The following C# code --based off of the Java
provided-- is working...

PdfContentByte cb = stamper.GetUnderContent(1);
Barcode39 code39BCD = new Barcode39();
code39BCD.Code = parcel;
code39BCD.Font = null;
code39BCD.BarHeight = 16;
Image imageCode39 = code39BCD.CreateImageWithBarcode(cb, null, null);
imageCode39.SetAbsolutePosition(225, 342);
cb.AddImage(imageCode39);

BTW, The files are split across multiple directories containing 2500
pdfs a piece. I had planned to use your utility, pdftk, to merge the
output pdfs into a single document via a batch script. I'd like to
save a step and join all the files in a directory directly from the
script. If the code isn't too long, could you post pdftk's code
responsible for concatenating multiple files? (For example, "pdftk
*.pdf cat output combined.pdf").

Thanks,
-Gary

0
localhost
7/24/2007 4:55:37 PM
On Jul 24, 9:55 am, localhost <gjwater...@frontiernet.net> wrote:
> BTW, The files are split across multiple directories containing 2500
> pdfs a piece. I had planned to use your utility, pdftk, to merge the
> output pdfs into a single document via a batch script. I'd like to
> save a step and join all the files in a directory directly from the
> script. If the code isn't too long, could you post pdftk's code
> responsible for concatenating multiple files? (For example, "pdftk
> *.pdf cat output combined.pdf").

pdftk isn't my utility, it belongs to Sid Steward.  I merely added my
own hack to it.

You can find the complete original source code here:

http://www.pdfhacks.com/pdftk/

0
chicks
7/24/2007 6:16:36 PM
On Jul 24, 12:55 pm, localhost <gjwater...@frontiernet.net> wrote:
> On Jul 24, 7:50 am, chicks <sea....@gmail.com> wrote:
>
> > This (java) code works for me in my pdftk extension athttp://www.esnips.com/web/PDFTools:
>
> Thanks for the help! The following C# code --based off of the Java
> provided-- is working...
>
> PdfContentByte cb = stamper.GetUnderContent(1);
> Barcode39 code39BCD = new Barcode39();
> code39BCD.Code = parcel;
> code39BCD.Font = null;
> code39BCD.BarHeight = 16;
> Image imageCode39 = code39BCD.CreateImageWithBarcode(cb, null, null);
> imageCode39.SetAbsolutePosition(225, 342);
> cb.AddImage(imageCode39);
>
> BTW, The files are split across multiple directories containing 2500
> pdfs a piece. I had planned to use your utility, pdftk, to merge the
> output pdfs into a single document via a batch script. I'd like to
> save a step and join all the files in a directory directly from the
> script. If the code isn't too long, could you post pdftk's code
> responsible for concatenating multiple files? (For example, "pdftk
> *.pdf cat output combined.pdf").
>
> Thanks,
> -Gary

(Chicks didn't write pdftk; he extended it.  Sid Steward wrote the
original pdftk.)

Off-the-cuff VB.NET code for concatenating:

Imports iTextSharp.text
Imports iTextSharp.text.pdf

Function ConcatPDF(byref input() as string, output as string)
        Dim doc As New Document(PageSize.LETTER)
        Dim writer As New PdfCopy(doc, New IO.FileStream(filename,
IO.FileMode.CreateNew))
        doc.Open()

        For Each f As String In input
            Dim rdr As New PdfReader(f)
            For i As Integer = 1 To rdr.NumberOfPages
                Dim pg As PdfImportedPage =
writer.GetImportedPage(rdr, i)
                writer.AddPage(pg)
            Next
        Next
        doc.Close()
End Function


This is a bit old fashioned, because I use PdfWriter and PdfReader
directly. It is possible that the somewhat newer PdfCopy class would
be better.

0
rpresser
7/24/2007 7:02:39 PM
On Jul 24, 12:02 pm, rpresser <rpres...@gmail.com> wrote:
> On Jul 24, 12:55 pm, localhost <gjwater...@frontiernet.net> wrote:
>
>
>
> > On Jul 24, 7:50 am, chicks <sea....@gmail.com> wrote:
>
> > > This (java) code works for me in my pdftk extension athttp://www.esnips.com/web/PDFTools:
>
> > Thanks for the help! The following C# code --based off of the Java
> > provided-- is working...
>
> > PdfContentByte cb = stamper.GetUnderContent(1);
> > Barcode39 code39BCD = new Barcode39();
> > code39BCD.Code = parcel;
> > code39BCD.Font = null;
> > code39BCD.BarHeight = 16;
> > Image imageCode39 = code39BCD.CreateImageWithBarcode(cb, null, null);
> > imageCode39.SetAbsolutePosition(225, 342);
> > cb.AddImage(imageCode39);
>
> > BTW, The files are split across multiple directories containing 2500
> > pdfs a piece. I had planned to use your utility, pdftk, to merge the
> > output pdfs into a single document via a batch script. I'd like to
> > save a step and join all the files in a directory directly from the
> > script. If the code isn't too long, could you post pdftk's code
> > responsible for concatenating multiple files? (For example, "pdftk
> > *.pdf cat output combined.pdf").
>
> > Thanks,
> > -Gary
>
> (Chicks didn't write pdftk; he extended it.  Sid Steward wrote the
> original pdftk.)
>
> Off-the-cuff VB.NET code for concatenating:
>
> Imports iTextSharp.text
> Imports iTextSharp.text.pdf
>
> Function ConcatPDF(byref input() as string, output as string)
>         Dim doc As New Document(PageSize.LETTER)
>         Dim writer As New PdfCopy(doc, New IO.FileStream(filename,
> IO.FileMode.CreateNew))
>         doc.Open()
>
>         For Each f As String In input
>             Dim rdr As New PdfReader(f)
>             For i As Integer = 1 To rdr.NumberOfPages
>                 Dim pg As PdfImportedPage =
> writer.GetImportedPage(rdr, i)
>                 writer.AddPage(pg)
>             Next
>         Next
>         doc.Close()
> End Function
>
> This is a bit old fashioned, because I use PdfWriter and PdfReader
> directly. It is possible that the somewhat newer PdfCopy class would
> be better.

Thanks for all the responses.

FYI; I ended up directly calling pdftk with the following code:

        private void CombineBills(int boxNumber)
        {
            Console.WriteLine(String.Format("Combining bills to{0}.",
                String.Format(pdftkOptions, outputRoot, boxNumber,
boxNumber, outputRoot, boxNumber)));

            ProcessStartInfo processInf = new ProcessStartInfo();
            processInf.FileName = pdftkPath;
            processInf.Arguments = String.Format(pdftkOptions,
outputRoot, boxNumber, boxNumber, outputRoot, boxNumber);
            processInf.CreateNoWindow = true;
            processInf.ErrorDialog = false;
            processInf.UseShellExecute = true;
            Process.Start(processInf);
        }

0
localhost
7/27/2007 7:30:21 PM
Reply: