Some test text!

Search
Hamburger Icon

Android / Guides

Color modes

There are a few themes which can be adjusted to change the output color mode.

Page color mode

PDFViewCtrl has three special color modes: night mode, inverted color mode, and custom color mode. In night mode, colors are adjusted to improve reading at night, in inverted color mode, all colors are inverted, and in custom color mode, you can set a custom color for text and the background color.

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

Set the color mode

To set the color mode:

  1. Find the constant value of the color mode

    Color modeConstant value
    Normal color modePDFRasterizer.e_postprocess_none
    Night modePDFRasterizer.e_postprocess_night_mode
    Inverted color modePDFRasterizer.e_postprocess_invert
    Custom color modePDFRasterizer.e_postprocess_gradient_map
  2. Call setColorPostProcessMode(int).

  3. Optionally, set the PDFViewCtrl background color for the best visual result. You can set background color by calling setClientBackgroundColor(int, int, int, boolean).

    Background area

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

    mPdfViewCtrl.setClientBackgroundColor(0, 0, 0, false);
  4. If you are using postprocess_gradient_map, call PDFViewCtrl.setColorPostProcessColors(int, int) to set the white and black points. For example, for a sepia effect, try:

    int darkBrown = Color.rgb(61, 38, 10);
    int lightBrown = Color.rgb(245, 224, 202);
    mPdfViewCtrl.setColorPostProcessColors(lightBrown, darkBrown);
  5. Update PDFViewCtrl to redraw the contents:

    mPdfViewCtrl.update(true);

    Sample result when using postprocess_gradient_map and a light/dark brown for the white/black color: custom color mode

    The example below shows all of this functionality being used to set up a custom color mode for PDFViewCtrl:

    private PDFViewCtrl mPdfViewCtrl;
    // ...
    // Sets background color to dark gray, as well as text color to red.
    private void setCustomColorMode(@ColorInt int backgroundColor, @ColorInt int textColor) throws PDFNetException {
        // Sets color process mode
        mPdfViewCtrl.setColorPostProcessMode(PDFRasterizer.e_postprocess_gradient_map);
        // Sets client area color to backgroundColor
        mPdfViewCtrl.setClientBackgroundColor(Color.red(backgroundColor), Color.green(backgroundColor), Color.blue(backgroundColor), false);
        // Sets background color and text color
        mPdfViewCtrl.setColorPostProcessColors(backgroundColor, textColor);
        // Updates mPdfViewCtrl
        mPdfViewCtrl.update(true);
    }

    Result:

    custom color mode

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:

    custom mode filter
    To learn more about res/raw resource directory, see Providing Resources
  1. 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);
    }
  2. 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.

  3. 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: custom color mode

Trial setup questions? Ask experts on Discord
Need other help? Contact Support
Pricing or product questions? Contact Sales