Tutorials > Convert image to DjVu |
The following code is a minimum sample to encode image files.
The output file name passed to Document.SaveAs Method can be PDF file name and then the method exports the document to high-compression PDF format.
img2djvu.cs |
Copy Code |
---|---|
using System; using DjVu; namespace EncodeSample { class EncodeSample { // Usage: img2djvu INPUT_IMAGE_FILENAME OUTPUT_DJVU_FILENAME static void Main(string[] args) { // TODO: ADD app's license script here or REMOVE the following line LicenseManager.LicenseScript = "......"; // encode using scan300 profile EncoderParams encParam = new EncoderParams("scan300"); using (Encoder enc = new Encoder(encParam)) { // add an image file specified on the command line enc.AddPages(args[0]); // obtain the encoded document and save it to the file using (Document doc = enc.GetEncodedDocument()) { // the output file format is determined according to the file extension; // you can pass .djvu/.djv, .pdf or .tif/.tiff. doc.SaveAs(args[1]); } } } } } |
![]() |
The Document object returned by GetEncodedDocument Method should be disposed correctly; you should use using statement or explicitly call Document.Dispose Method. |
![]() |
The loading of a profile may fail if the profile repository is not initialized correctly. See DjVu Encoder Profiles for more information. |
To save the file in indirect file, you can use Document.SaveAsIndirect Method instead of Document.SaveAs Method. For the usage, see Save in Indirect Format.
If you want to perform OCR on encode, see Perform OCR.
Because a file may have multiple pages (images), you may find that Encoder.AddPages Method is too simple to set page encode parameter for each page. To specify encode parameter page by page basis, you can use Encoder.UpdateEncoderParams Event. The event is called for each image added to the Encoder object.
encpxp.cs |
Copy Code |
---|---|
using System; using DjVu; namespace EncodePageByPage { class EncodePageByPage { static void Main(string[] args) { // TODO: ADD app's license script here or REMOVE the following line LicenseManager.LicenseScript = "......"; using (Encoder enc = new Encoder()) { enc.UpdateEncoderParams += updateEncoderParams; enc.AddPages(args[0]); using (Document doc = enc.GetEncodedDocument()) doc.SaveAs(args[1]); } } // called on every page image static void updateEncoderParams(ref DjVu.EncoderParams encParams, int width, int height, int dpi, Bitmap bmp) { Console.WriteLine("Page Added: {0}x{1}", bmp.Width, bmp.Height); // you can configure parameters according to bmp content encParams = new EncoderParams("scan300"); encParams.JB2Params.QualityPolicy = JB2QualityPolicy.Lossless; } } } |