1
string imgfile = @"C:\users\me\desktop\test.jpg";

Bitmap bmp = new Bitmap(imgfile);
Bitmap bw = ConvertTo1Bpp(bmp); //make b+w

Document doc = 
    new Document(new iTextSharp.text.Rectangle(bmp.Width, bmp.Height));

PdfWriter.GetInstance(doc, 
    new System.IO.FileStream(
        @"C:\users\me\desktop\test.pdf", 
        System.IO.FileMode.Create, 
        System.IO.FileAccess.ReadWrite));

iTextSharp.text.ImgJBIG2 i = 
    ((iTextSharp.text.ImgJBIG2)iTextSharp.text.ImgJBIG2.GetInstance(
        bmp, System.Drawing.Imaging.ImageFormat.Bmp));

doc.Open();
doc.Add(i);
doc.Close();

I cant find any good documentation for this with iTextSharp. What I am trying to do is take a Jpeg file and convert it into a pdf embedded as a black and white JBig2 image. The error I get is an InvalidCastException between "iTextSharp.text.ImageRaw" and "iTextSharp.text.ImageJBig2"... is there an alternative to what I have above?

EDIT
ImgJBig2 just represents an image already encoded in JBig2 I believe now. What I am looking for is something that will take a Bitmap and encode it into a BW JBig2 Bitmap that I can put into a Pdf.

Tom Fobear
  • 6,729
  • 7
  • 42
  • 74

1 Answers1

2

As far as I can tell there's not a lot of options for encoding JBig2 and no native free ones.

The last creates a CLI program that you might be able to P/Invoke into or at worst script against so I think that's your best option if you don't want to pay. Does JBIG2 offer such greater compression over other formats?

Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • yes, it does for B+W images. I am using jbig2enc in my toolkit now but I haven't taken the time to test it out on 64 bit. Unfortunately that is all our servers will run anymore. I will have to work it out though. thanks! – Tom Fobear Mar 23 '11 at 17:46
  • @Tom Fobear, are you P/Invoking it or scripting over the command line? I'm just curious in case this ever comes up again. – Chris Haas Mar 23 '11 at 21:35