Convert single or multi-page JBIG2 to PDF in C#

Images
11/10/2011

Downloads

This c# code sample shows how to convert a single or multi-page JBIG2 to PDF.

About JBIG2

JBIG2 is an image compression standard for bi-level images. It is suitable for both lossless and lossy compression. In its lossless mode JBIG2 typically generates files one third to one fifth the size of Fax Group 4 (TIFF).

C# code sample

1 Document document = new Document(); 2 3 // get number of pages/frames in JBIG2 file 4 string path = @"..\..\multipage.jb2"; 5 int frameCount = ImageShape.GetFrameCount(path); 6 7 // iterate over frames 8 for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) 9 { 10 // create the image (in full size) 11 ImageShape image = new ImageShape(path, frameIndex); 12 image.KeepAspectRatio = false; 13 14 // create new page and append to document 15 Page page = new Page(image.Width, image.Height); 16 document.Pages.Add(page); 17 page.VisualOverlay.Add(image); 18 } 19 20 // save 21 using (FileStream fs = new FileStream(@"..\..\out.pdf", FileMode.Create, FileAccess.Write)) 22 { 23 document.Write(fs); 24 }

VB.NET code sample

1 Dim document As New Document() 2 3 ' get number of pages/frames in JBIG2 file 4 Dim path As String = "..\..\multipage.jb2" 5 Dim frameCount As Integer = ImageShape.GetFrameCount(path) 6 7 ' iterate over frames 8 For frameIndex As Integer = 0 To frameCount - 1 9 ' create the image (in full size) 10 Dim image As New ImageShape(path, frameIndex) 11 image.KeepAspectRatio = False 12 13 ' create new page and append to document 14 Dim page As New Page(image.Width, image.Height) 15 document.Pages.Add(page) 16 page.VisualOverlay.Add(image) 17 Next 18 19 ' save 20 Using fs As New FileStream("..\..\out.pdf", FileMode.Create, FileAccess.Write) 21 document.Write(fs) 22 End Using