Night mode and custom color mode

PDFViewCtrl supports day mode, night mode, inverted color mode, and custom color mode. In day mode, documents are displayed in their original color. In night mode, colors are adjusted to improve reading at night. In inverted color mode, all colors are inverted. In custom color mode, you can set customized background color and text color to PDFViewCtrl.

Color modes from left to right: day mode, inverted color mode, night mode, custom color mode.

Setting day mode, night mode and inverted color mode

To set the color mode:

  1. Find the constant value of the color mode
Color modeConstant value
Day modePDFRasterizer.e_postprocess_none
Night modePDFRasterizer.e_postprocess_night_mode
Inverted color modePDFRasterizer.e_postprocess_invert
Custom color modePDFRasterizer.e_postprocess_gradient_map
  1. To set the color mode, call pdfViewCtrl.setColorPostProcessMode(int):

    For example, set PDFViewCtrl to night mode:

    pdfViewCtrl.setColorPostProcessMode (PDFRasterizer.e_postprocess_night_mode);

    where pdfViewCtrl is an instance of PDFViewCtrl.

  2. For the best visual results, set PDFViewCtrl client area background color. You can set the client area background color by calling pdfViewCtrl.setClientBackgroundColor(int, int, int, boolean).

    For example, set the client area color to black for night mode:

    pdfViewCtrl.setClientBackgroundColor(0, 0, 0, false);
  3. If you are using PDFRasterizer.e_postprocess_gradient_map, call PDFViewCtrl.setColorPostProcessColors(int, int) to set the white and black points. For example, set white point to dark gray and black points to red.

    // Sets background color to dark gray, as well as text color to red.
    int backgroundColor = Color.DKGRAY;
    int textColor = Color.RED;
    pdfViewCtrl.setColorPostProcessColors(backgroundColor, textColor);
  4. Update PDFViewCtrl to redraw the contents:

    pdfViewCtrl.update(true);

The example below shows all of this functionality being used to set the custom color mode with a dark gray background and red text color.

// Sets background color to dark gray, as well as text color to red.
int backgroundColor = Color.DKGRAY;
int textColor = Color.RED;
try {
  // Sets color process mode
  pdfViewCtrl.setColorPostProcessMode(PDFRasterizer.e_postprocess_gradient_map);
  // Sets client area color to backgroundColor
  pdfViewCtrl.setClientBackgroundColor(Color.red(backgroundColor, Color.green(backgroundColor), Color.blue(backgroundColor), false);
  // Sets background color and text color
  pdfViewCtrl.setColorPostProcessColors(backgroundColor, textColor);
  // Updates pdfViewCtrl
  pdfViewCtrl.update(true);
} catch (PDFNetException e) {
    e.printStackTrace();
}

Result:

Setting custom color mode by gradient map image

Alternatively, you can also set custom color mode by adding a gradient map image as follows:

  1. Add a gradient map .png image to your project res/raw folder.

    For example, add the following image to res/raw folder and name it custom_mode_filter.png:

    To learn more about res/raw resource directory, see Providing Resources
  2. Set the gradient image to a new MappedFile:

    InputStream is = null;
    OutputStream os = null;
    MappedFile mappedFile = null;
    try {
        File filterFile = new File(activity.getCacheDir(), "cachedCustomColorFilter.png");
        if (!filterFile.exists() || !filterFile.isFile()) {
            is = getResources().openRawResource(R.raw.custom_mode_filter);
            os = new FileOutputStream(filterFile);
            IOUtils.copy(is, os);
        }
        mappedFile = new MappedFile(filterFile.getAbsolutePath());
    }catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
  3. Set the mappedFile created above to PDFViewCtrl by calling PDFViewCtrl.setColorPostProcessMapFile(Filter).

    if (mappedFile != null) {
        try {
            pdfViewCtrl.setColorPostProcessMapFile(mappedFile);
        } catch (PDFNetException e) {
            e.printStackTrace();
        }
    }

    Where pdfViewCtrl is an instance of PDFViewCtrl.

  4. Set the color process mode PDFRasterizer.e_postprocess_gradient_map to pdfViewCtrl by calling pdfViewCtrl.setColorPostProcessMode(int). Then update pdfViewCtrl:

    try {
        pdfViewCtrl.setColorPostProcessMode(PDFRasterizer.e_postprocess_gradient_map);
        // Sets client area background color to rgb(138, 138, 138)
        pdfViewCtrl.setClientBackgroundColor(138, 138, 138, false);
        // Updates pdfViewCtrl
        pdfViewCtrl.update(true);
    } catch (PDFNetException e) {
        e.printStackTrace();
    }

Result: