Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

iTextSharp - PDFStamper & Barcode

1,562 views
Skip to first unread message

localhost

unread,
Jul 23, 2007, 8:26:54 PM7/23/07
to
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

chicks

unread,
Jul 24, 2007, 10:50:57 AM7/24/07
to
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());
}
}

localhost

unread,
Jul 24, 2007, 12:55:37 PM7/24/07
to
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

unread,
Jul 24, 2007, 2:16:36 PM7/24/07
to
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/

rpresser

unread,
Jul 24, 2007, 3:02:39 PM7/24/07
to

(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.

localhost

unread,
Jul 27, 2007, 3:30:21 PM7/27/07
to

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 new messages