// // PDFNet Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved. // using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using pdftron; using pdftron.Common; using pdftron.Filters; using pdftron.SDF; using pdftron.PDF; namespace JBIG2Test { /// /// This sample project illustrates how to recompress bi-tonal images in an /// existing PDF document using JBIG2 compression. The sample is not intended /// to be a generic PDF optimization tool. /// /// You can download a sample scanned document using the following link: /// http://www.pdftron.com/net/samplecode/data/US061222892.pdf /// /// Also a sample page compressed using CCITT Fax compression is located under /// 'PDFNet/Samples/TestFiles' folder. /// public class JBIG2TestDlg : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Label label1; private System.Windows.Forms.Button button3; private System.Windows.Forms.CheckBox lossy; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public JBIG2TestDlg() { // // Required for Windows Form Designer support // InitializeComponent(); ObjSet hint_set = new ObjSet(); _JBIG2_hint = hint_set.CreateArray(); _JBIG2_hint.PushBackName("JBIG2"); _JBIG2_hint.PushBackName("Lossless"); } Obj _JBIG2_hint; /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.button3 = new System.Windows.Forms.Button(); this.lossy = new System.Windows.Forms.CheckBox(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(160, 104); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(184, 40); this.button1.TabIndex = 0; this.button1.Text = "Load PDF"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(160, 160); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(184, 40); this.button2.TabIndex = 1; this.button2.Text = "Save using JBIG2"; this.button2.Click += new System.EventHandler(this.button2_Click); // // label1 // this.label1.Location = new System.Drawing.Point(32, 16); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(304, 64); this.label1.TabIndex = 2; this.label1.Text = "This sample project illustrates how to recompress bi-tonal images in an existing " + "PDF document using JBIG2 compression. The sample is not intended to be a generic" + " PDF optimization tool."; // // button3 // this.button3.Location = new System.Drawing.Point(160, 216); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(184, 40); this.button3.TabIndex = 3; this.button3.Text = "Quit"; this.button3.Click += new System.EventHandler(this.button3_Click); // // lossy // this.lossy.Location = new System.Drawing.Point(24, 160); this.lossy.Name = "lossy"; this.lossy.Size = new System.Drawing.Size(120, 32); this.lossy.TabIndex = 4; this.lossy.Text = "Lossy Compression"; // // JBIG2TestDlg // this.AutoScaleBaseSize = new System.Drawing.Size(6, 15); this.ClientSize = new System.Drawing.Size(384, 284); this.Controls.Add(this.lossy); this.Controls.Add(this.button3); this.Controls.Add(this.label1); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Name = "JBIG2TestDlg"; this.Text = "JBIG2 Test"; this.ResumeLayout(false); } #endregion // On exit, finish using PDFNet private void button3_Click(object sender, System.EventArgs e) { Application.Exit(); } private PDFDoc doc = null; // On Load ... private void button1_Click(object sender, System.EventArgs e) { OpenFileDialog dlg=new OpenFileDialog(); dlg.Filter="PDF (*.pdf)|*.pdf|All files (*.*)|*.*"; if(dlg.ShowDialog()==DialogResult.OK) { if (doc != null) { doc.Close(); } doc = new PDFDoc(dlg.FileName); doc.InitSecurityHandler(); } } // On Save... private void button2_Click(object sender, System.EventArgs e) { SaveFileDialog dlg=new SaveFileDialog(); dlg.Filter="PDF (*.pdf)|*.pdf|All files (*.*)|*.*"; if(dlg.ShowDialog()==DialogResult.OK) { if (doc != null) { Recompress(); doc.Save(dlg.FileName, SDFDoc.SaveOptions.e_remove_unused); doc.Close(); doc = null; } } } byte[] _img_buf = null; private void Recompress() { if (lossy.Checked) { _JBIG2_hint.GetAt(1).SetName("Lossy"); } else { _JBIG2_hint.GetAt(1).SetName("Lossless"); } SDFDoc cos_doc = doc.GetSDFDoc(); int num_objs = cos_doc.XRefSize(); for (int i=1; i 128) { acc |= 0x01; } if (x!=0 && (x%8)==0) { _img_buf[out_pos] = acc; ++out_pos; } } if (pad!=0) { acc <<= (8 - pad); } _img_buf[out_pos] = acc; ++out_pos; } new_image = pdftron.PDF.Image.Create( cos_doc, _img_buf, width, height, 1, ColorSpace.CreateDeviceGray(), _JBIG2_hint // A hint to image encoder to use JBIG2 compression ); } Obj new_img_obj = new_image.GetSDFObj(); // Copy any important entries from the image dictionary itr = obj.Find("Decode"); if (itr.HasNext()) new_img_obj.Put("Decode", itr.Value()); itr = obj.Find("ImageMask"); if (itr.HasNext()) new_img_obj.Put("ImageMask", itr.Value()); itr = obj.Find("Mask"); if (itr.HasNext()) new_img_obj.Put("Mask", itr.Value()); cos_doc.Swap(i, new_image.GetSDFObj().GetObjNum()); } } } /// /// The main entry point for the application. /// [STAThread] static void Main() { // Initialize PDFNet before calling any other PDFNet function. PDFNet.Initialize(); PDFNet.SetResourcesPath("../../../../../resources"); Application.Run(new JBIG2TestDlg()); PDFNet.Terminate(); } } }