> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/android/get-started/samples/pdfdrawtest.md).

# PDFDraw

Learn to convert pdf to image format using WebViewer or programatically using code without WebViewer UI

Sample Java code to use Apryse SDK's built-in rasterizer to render PDF images on the fly and save the resulting images in various raster image formats (such as PNG, JPEG, BMP, TIFF). Learn more about our [Android SDK](/core/get-started/languages/java.md) and [PDF Conversion Library](/core/conversion/conversion.md).

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

package com.pdftron.android.pdfnetsdksamples.samples;

import com.pdftron.android.pdfnetsdksamples.OutputListener;
import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
import com.pdftron.android.pdfnetsdksamples.R;
import com.pdftron.android.pdfnetsdksamples.util.Utils;
import com.pdftron.common.Matrix2D;
import com.pdftron.pdf.PDFDoc;
import com.pdftron.pdf.PDFDraw;
import com.pdftron.pdf.PDFRasterizer;
import com.pdftron.pdf.Page;
import com.pdftron.pdf.PageIterator;
import com.pdftron.pdf.Rect;
import com.pdftron.sdf.Obj;
import com.pdftron.sdf.ObjSet;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;

public class PDFDrawTest extends PDFNetSample {

	private static OutputListener mOutputListener;

	private static ArrayList<String> mFileList = new ArrayList<>();

    public PDFDrawTest() {
        setTitle(R.string.sample_pdfdraw_title);
        setDescription(R.string.sample_pdfdraw_description);
    }

	@Override
	public void run(OutputListener outputListener) {
		super.run(outputListener);
		mOutputListener = outputListener;
		mFileList.clear();
		printHeader(outputListener);
        try {
            // The first step in every application using PDFNet is to initialize the
            // library and set the path to common PDF resources. The library is usually
            // initialized only once, but calling Initialize() multiple times is also fine.

            // Optional: Set ICC color profiles to fine tune color conversion
            // for PDF 'device' color spaces...

            //PDFNet.setResourcesPath("../../../resources");
            //PDFNet.setColorManagement();
            //PDFNet.setDefaultDeviceCMYKProfile("D:/Misc/ICC/USWebCoatedSWOP.icc");
            //PDFNet.setDefaultDeviceRGBProfile("AdobeRGB1998.icc"); // will search in PDFNet resource folder.

            // ----------------------------------------------------
            // Optional: Set predefined font mappings to override default font
            // substitution for documents with missing fonts...

            // PDFNet.addFontSubst("StoneSans-Semibold", "C:/WINDOWS/Fonts/comic.ttf");
            // PDFNet.addFontSubst("StoneSans", "comic.ttf");  // search for 'comic.ttf' in PDFNet resource folder.
            // PDFNet.addFontSubst(PDFNet.e_Identity, "C:/WINDOWS/Fonts/arialuni.ttf");
            // PDFNet.addFontSubst(PDFNet.e_Japan1, "C:/Program Files/Adobe/Acrobat 7.0/Resource/CIDFont/KozMinProVI-Regular.otf");
            // PDFNet.addFontSubst(PDFNet.e_Japan2, "c:/myfonts/KozMinProVI-Regular.otf");
            // PDFNet.addFontSubst(PDFNet.e_Korea1, "AdobeMyungjoStd-Medium.otf");
            // PDFNet.addFontSubst(PDFNet.e_CNS1, "AdobeSongStd-Light.otf");
            // PDFNet.addFontSubst(PDFNet.e_GB1, "AdobeMingStd-Light.otf");

            PDFDraw draw = new PDFDraw();  // PDFDraw class is used to rasterize PDF pages.
            ObjSet hint_set = new ObjSet();

            //--------------------------------------------------------------------------------
            // Example 1) Convert the first page to PNG and TIFF at 92 DPI.
            // A three step tutorial to convert PDF page to an image.
            try (PDFDoc doc = new PDFDoc((Utils.getAssetTempFile(INPUT_PATH + "tiger.pdf").getAbsolutePath()))) {
                // A) Open the PDF document.

                // Initialize the security handler, in case the PDF is encrypted.
                doc.initSecurityHandler();

                // B) The output resolution is set to 92 DPI.
                draw.setDPI(92);

                // C) Rasterize the first page in the document and save the result as PNG.
                Page pg = doc.getPage(1);
                draw.export(pg, (Utils.createExternalFile("tiger_92dpi.png", mFileList).getAbsolutePath()));
                // output "tiger_92dpi.png"

                mOutputListener.println("Example 1: tiger_92dpi.png");

                // Export the same page as TIFF
                draw.export(pg, (Utils.createExternalFile("tiger_92dpi.tif", mFileList).getAbsolutePath()), "TIFF");
                // output "tiger_92dpi.tif"
            } catch (Exception e) {
                mOutputListener.printError(e.getStackTrace());
            }

            //--------------------------------------------------------------------------------
            // Example 2) Convert the all pages in a given document to JPEG at 72 DPI.
            mOutputListener.println("Example 2:");
            try (PDFDoc doc = new PDFDoc((Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath()))) {
                // Initialize the security handler, in case the PDF is encrypted.
                doc.initSecurityHandler();

                draw.setDPI(72); // Set the output resolution is to 72 DPI.

                // Use optional encoder parameter to specify JPEG quality.
                Obj encoder_param = hint_set.createDict();
                encoder_param.putNumber("Quality", 80);

                // Traverse all pages in the document.
                for (PageIterator itr = doc.getPageIterator(); itr.hasNext(); ) {
                    Page current = itr.next();
                    String filename = "newsletter" + current.getIndex() + ".jpg";
                    mOutputListener.println(filename);
                    draw.export(current, Utils.createExternalFile(filename, mFileList).getAbsolutePath(), "JPEG", encoder_param);
                }

                mOutputListener.println("Done.");
            } catch (Exception e) {
                mOutputListener.printError(e.getStackTrace());
            }

            FileOutputStream fos = null;
            // Examples 3-5
            try (PDFDoc tiger_doc = new PDFDoc((Utils.getAssetTempFile(INPUT_PATH + "tiger.pdf").getAbsolutePath()))) {
                // Common code for remaining samples.
                // Initialize the security handler, in case the PDF is encrypted.
                tiger_doc.initSecurityHandler();
                Page page = tiger_doc.getPageIterator().next();

                //--------------------------------------------------------------------------------
                // Example 3) Convert the first page to raw bitmap. Also, rotate the
                // page 90 degrees and save the result as RAW.
                draw.setDPI(100); // Set the output resolution is to 100 DPI.
                draw.setRotate(Page.e_90);  // Rotate all pages 90 degrees clockwise.

                // create a Java image
                android.graphics.Bitmap image = draw.getBitmap(page);

                //
                int width = image.getWidth(), height = image.getHeight();
                int[] arr = new int[width * height];
                image.getPixels(arr, 0, width, 0, 0, width, height);
                // pg.grabPixels();

                // convert to byte array
                ByteBuffer byteBuffer = ByteBuffer.allocate(arr.length * 4);
                IntBuffer intBuffer = byteBuffer.asIntBuffer();
                intBuffer.put(arr);
                byte[] rawByteArray = byteBuffer.array();
                // finally write the file
                fos = new FileOutputStream(Utils.createExternalFile("tiger_100dpi_rot90.raw", mFileList).getAbsolutePath());
                fos.write(rawByteArray);
                mOutputListener.println("Example 3: tiger_100dpi_rot90.raw");

                draw.setRotate(Page.e_0);  // Disable image rotation for remaining samples.

                //--------------------------------------------------------------------------------
                // Example 4) Convert PDF page to a fixed image size. Also illustrates some
                // other features in PDFDraw class such as rotation, image stretching, exporting
                // to grayscale, or monochrome.

                // Initialize render 'gray_hint' parameter, that is used to control the
                // rendering process. In this case we tell the rasterizer to export the image as
                // 1 Bit Per Component (BPC) image.
                Obj mono_hint = hint_set.createDict();
                mono_hint.putNumber("BPC", 1);

                // SetImageSize can be used instead of SetDPI() to adjust page  scaling
                // dynamically so that given image fits into a buffer of given dimensions.
                draw.setImageSize(1000, 1000);        // Set the output image to be 1000 wide and 1000 pixels tall

                draw.export(page, (Utils.createExternalFile("tiger_1000x1000.png", mFileList).getAbsolutePath()), "PNG", mono_hint);
                mOutputListener.println("Example 4: tiger_1000x1000.png");

                draw.setImageSize(200, 400); // Set the output image to be 200 wide and 300 pixels tall
                draw.setRotate(Page.e_180); // Rotate all pages 90 degrees clockwise.

                // 'gray_hint' tells the rasterizer to export the image as grayscale.
                Obj gray_hint = hint_set.createDict();
                gray_hint.putName("ColorSpace", "Gray");

                draw.export(page, (Utils.createExternalFile("tiger_200x400_rot180.png", mFileList).getAbsolutePath()), "PNG", gray_hint);
                mOutputListener.println("Example 4: tiger_200x400_rot180.png");

                draw.setImageSize(400, 200, false);  // The third parameter sets 'preserve-aspect-ratio' to false.
                draw.setRotate(Page.e_0);    // Disable image rotation.
                draw.export(page, (Utils.createExternalFile("tiger_400x200_stretch.jpg", mFileList).getAbsolutePath()), "JPEG");
                // output "tiger_400x200_stretch.jpg"
                mOutputListener.println("Example 4: tiger_400x200_stretch.jpg");

                //--------------------------------------------------------------------------------
                // Example 5) Zoom into a specific region of the page and rasterize the
                // area at 200 DPI and as a thumbnail (i.e. a 50x50 pixel image).
                Rect zoom_rect = new Rect(216, 522, 330, 600);
                page.setCropBox(zoom_rect);    // Set the page crop box.

                // Select the crop region to be used for drawing.
                draw.setPageBox(Page.e_crop);
                draw.setDPI(900);  // Set the output image resolution to 900 DPI.
                draw.export(page, (Utils.createExternalFile("tiger_zoom_900dpi.png", mFileList).getAbsolutePath()), "PNG");
                // output "tiger_zoom_900dpi.png"
                mOutputListener.println("Example 5: tiger_zoom_900dpi.png");

                // -------------------------------------------------------------------------------
                // Example 6)
                draw.setImageSize(50, 50);       // Set the thumbnail to be 50x50 pixel image.
                draw.export(page, (Utils.createExternalFile("tiger_zoom_50x50.png", mFileList).getAbsolutePath()), "PNG");
                // output "tiger_zoom_50x50.png"
                mOutputListener.println("Example 6: tiger_zoom_50x50.png");
            } catch (Exception e) {
                mOutputListener.printError(e.getStackTrace());
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (Exception ignored) {
                    }
                }
            }

            Obj cmyk_hint = hint_set.createDict();
            cmyk_hint.putName("ColorSpace", "CMYK");

            //--------------------------------------------------------------------------------
            // Example 7) Convert the first PDF page to CMYK TIFF at 92 DPI.
            // A three step tutorial to convert PDF page to an image
            try (PDFDoc doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "tiger.pdf").getAbsolutePath())) {
                // A) Open the PDF document.

                // Initialize the security handler, in case the PDF is encrypted.
                doc.initSecurityHandler();

                // B) The output resolution is set to 92 DPI.
                draw.setDPI(92);

                // C) Rasterize the first page in the document and save the result as TIFF.
                Page pg = doc.getPage(1);
                draw.export(pg, Utils.createExternalFile("out1.tif", mFileList).getAbsolutePath(), "TIFF", cmyk_hint);
                // output "out1.tif"
                mOutputListener.println("Example 7: out1.tif");
            } catch (Exception e) {
                mOutputListener.printError(e.getStackTrace());
            }

            //--------------------------------------------------------------------------------
            // Example 8) PDFRasterizer can be used for more complex rendering tasks, such as 
            // strip by strip or tiled document rendering. In particular, it is useful for 
            // cases where you cannot simply modify the page crop box (interactive viewing,
            // parallel rendering).  This example shows how you can rasterize the south-west
            // quadrant of a page.
            try (PDFDoc doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "tiger.pdf").getAbsolutePath())) {
                // A) Open the PDF document.
                // Initialize the security handler, in case the PDF is encrypted.
                doc.initSecurityHandler();

                // B) Get the page matrix 
                Page pg = doc.getPage(1);
                int box = Page.e_crop;
                Matrix2D mtx = pg.getDefaultMatrix(true, box, 0);
                // We want to render a quadrant, so use half of width and height
                double pg_w = pg.getPageWidth(box) / 2;
                double pg_h = pg.getPageHeight(box) / 2;

                // C) Scale matrix from PDF space to buffer space
                double dpi = 96.0;
                double scale = dpi / 72.0; // PDF space is 72 dpi
                double buf_w = Math.floor(scale * pg_w);
                double buf_h = Math.floor(scale * pg_h);
                int bytes_per_pixel = 4; // BGRA buffer
                mtx.translate(0, -pg_h); // translate by '-pg_h' since we want south-west quadrant
                mtx = (new Matrix2D(scale, 0, 0, scale, 0, 0)).multiply(mtx);

                // D) Rasterize page into memory buffer, according to our parameters
                PDFRasterizer rast = new PDFRasterizer();
                byte[] buf = rast.rasterize(pg, (int) buf_w, (int) buf_h, (int) buf_w * bytes_per_pixel, bytes_per_pixel, true, mtx, null);

                mOutputListener.println("Example 8: Successfully rasterized into memory buffer.");
            } catch (Exception e) {
                mOutputListener.printError(e.getStackTrace());
            }

            //--------------------------------------------------------------------------------
            // Example 9) Export raster content to PNG using different image smoothing settings.
            try (PDFDoc text_doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "lorem_ipsum.pdf").getAbsolutePath())) {
                text_doc.initSecurityHandler();

                draw.setImageSmoothing(false, false);
                String filename = "raster_text_no_smoothing.png";
                draw.export(text_doc.getPageIterator().next(), Utils.createExternalFile(filename, mFileList).getAbsolutePath());
                mOutputListener.println("Example 9 a): " + filename + ". Done.");

                filename = "raster_text_smoothed.png";
                draw.setImageSmoothing(true, false /*default quality bilinear resampling*/);
                draw.export(text_doc.getPageIterator().next(), Utils.createExternalFile(filename, mFileList).getAbsolutePath());
                mOutputListener.println("Example 9 b): " + filename + ". Done.");

                filename = "raster_text_high_quality.png";
                draw.setImageSmoothing(true, true /*high quality area resampling*/);
                draw.export(text_doc.getPageIterator().next(), Utils.createExternalFile(filename, mFileList).getAbsolutePath());
                mOutputListener.println("Example 9 c): " + filename + ". Done.");
            } catch (Exception e) {
                mOutputListener.printError(e.getStackTrace());
            }

            //--------------------------------------------------------------------------------
            // Example 10) Export separations directly, without conversion to an output colorspace
            try (PDFDoc separation_doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "op_blend_test.pdf").getAbsolutePath())) {
                separation_doc.initSecurityHandler();

                Obj separation_hint = hint_set.createDict();
                separation_hint.putName("ColorSpace", "Separation");
                draw.setDPI(96);
                draw.setImageSmoothing(true, true);
                // set overprint preview to always on
                draw.setOverprint(1);

                String filename = new String("merged_separations.png");
                draw.export(separation_doc.getPage(1), Utils.createExternalFile(filename, mFileList).getAbsolutePath(), "PNG");
                mOutputListener.println("Example 10 a): " + filename + ". Done.");

                filename = new String("separation");
                draw.export(separation_doc.getPage(1), Utils.createExternalFile(filename, mFileList).getAbsolutePath(), "PNG", separation_hint);
                mOutputListener.println("Example 10 b): " + filename + "_[ink].png. Done.");

                filename = new String("separation_NChannel.tif");
                draw.export(separation_doc.getPage(1), Utils.createExternalFile(filename, mFileList).getAbsolutePath(), "TIFF", separation_hint);
                mOutputListener.println("Example 10 c): " + filename + ". Done.");
            } catch (Exception e) {
                mOutputListener.printError(e.getStackTrace());
            }

            // Calling Terminate when PDFNet is no longer in use is a good practice, but
            // is not required.
        } catch (Exception e) {
            mOutputListener.printError(e.getStackTrace());
        }

		for (String file : mFileList) {
			addToFileList(file);
		}
		printFooter(outputListener);
	}

}
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

package com.pdftron.android.pdfnetsdksamples.samples

import com.pdftron.android.pdfnetsdksamples.OutputListener
import com.pdftron.android.pdfnetsdksamples.PDFNetSample
import com.pdftron.android.pdfnetsdksamples.R
import com.pdftron.android.pdfnetsdksamples.util.Utils
import com.pdftron.common.Matrix2D
import com.pdftron.pdf.*
import com.pdftron.sdf.ObjSet
import java.io.FileOutputStream
import java.nio.ByteBuffer
import java.util.*

class PDFDrawTest : PDFNetSample() {
    init {
        setTitle(R.string.sample_pdfdraw_title)
        setDescription(R.string.sample_pdfdraw_description)
    }

    override fun run(outputListener: OutputListener?) {
        super.run(outputListener)
        mOutputListener = outputListener
        mFileList.clear()
        printHeader(outputListener!!)
        try {
            // The first step in every application using PDFNet is to initialize the
            // library and set the path to common PDF resources. The library is usually
            // initialized only once, but calling Initialize() multiple times is also fine.

            // Optional: Set ICC color profiles to fine tune color conversion
            // for PDF 'device' color spaces...

            //PDFNet.setResourcesPath("../../../resources");
            //PDFNet.setColorManagement();
            //PDFNet.setDefaultDeviceCMYKProfile("D:/Misc/ICC/USWebCoatedSWOP.icc");
            //PDFNet.setDefaultDeviceRGBProfile("AdobeRGB1998.icc"); // will search in PDFNet resource folder.

            // ----------------------------------------------------
            // Optional: Set predefined font mappings to override default font
            // substitution for documents with missing fonts...

            // PDFNet.addFontSubst("StoneSans-Semibold", "C:/WINDOWS/Fonts/comic.ttf");
            // PDFNet.addFontSubst("StoneSans", "comic.ttf");  // search for 'comic.ttf' in PDFNet resource folder.
            // PDFNet.addFontSubst(PDFNet.e_Identity, "C:/WINDOWS/Fonts/arialuni.ttf");
            // PDFNet.addFontSubst(PDFNet.e_Japan1, "C:/Program Files/Adobe/Acrobat 7.0/Resource/CIDFont/KozMinProVI-Regular.otf");
            // PDFNet.addFontSubst(PDFNet.e_Japan2, "c:/myfonts/KozMinProVI-Regular.otf");
            // PDFNet.addFontSubst(PDFNet.e_Korea1, "AdobeMyungjoStd-Medium.otf");
            // PDFNet.addFontSubst(PDFNet.e_CNS1, "AdobeSongStd-Light.otf");
            // PDFNet.addFontSubst(PDFNet.e_GB1, "AdobeMingStd-Light.otf");

            val draw = PDFDraw()  // PDFDraw class is used to rasterize PDF pages.
            val hint_set = ObjSet()

            //--------------------------------------------------------------------------------
            // Example 1) Convert the first page to PNG and TIFF at 92 DPI.
            // A three step tutorial to convert PDF page to an image.
            try {
                // A) Open the PDF document.
                PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "tiger.pdf")!!.absolutePath).use { doc ->
                    // Initialize the security handler, in case the PDF is encrypted.
                    doc.initSecurityHandler()

                    // B) The output resolution is set to 92 DPI.
                    draw.setDPI(92.0)

                    // C) Rasterize the first page in the document and save the result as PNG.
                    val pg = doc.getPage(1)
                    draw.export(pg, Utils.createExternalFile("tiger_92dpi.png", mFileList).absolutePath)

                    mOutputListener!!.println("Example 1: tiger_92dpi.png")

                    // Export the same page as TIFF
                    draw.export(pg, Utils.createExternalFile("tiger_92dpi.tif", mFileList).absolutePath, "TIFF")
                }
            } catch (e: Exception) {
                mOutputListener!!.printError(e.stackTrace)
            }

            //--------------------------------------------------------------------------------
            // Example 2) Convert the all pages in a given document to JPEG at 72 DPI.
            try {
                mOutputListener!!.println("Example 2:")
                PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath).use { doc ->
                    // Initialize the security handler, in case the PDF is encrypted.
                    doc.initSecurityHandler()

                    draw.setDPI(72.0) // Set the output resolution is to 72 DPI.

                    // Use optional encoder parameter to specify JPEG quality.
                    val encoder_param = hint_set.createDict()
                    encoder_param.putNumber("Quality", 80.0)

                    // Traverse all pages in the document.
                    val itr = doc.pageIterator
                    while (itr.hasNext()) {
                        val current = itr.next()!!
                        val filename = "newsletter" + current.getIndex() + ".jpg"
                        mOutputListener!!.println(filename)
                        draw.export(current, Utils.createExternalFile(filename, mFileList).absolutePath, "JPEG", encoder_param)
                    }

                    mOutputListener!!.println("Done.")
                }
            } catch (e: Exception) {
                mOutputListener!!.printError(e.stackTrace)
            }

            // Examples 3-5
            var fos: FileOutputStream? = null
            try {
                // Common code for remaining samples.
                PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "tiger.pdf")!!.absolutePath).use { tiger_doc ->
                    // Initialize the security handler, in case the PDF is encrypted.
                    tiger_doc.initSecurityHandler()
                    val page = tiger_doc.pageIterator.next()!!

                    //--------------------------------------------------------------------------------
                    // Example 3) Convert the first page to raw bitmap. Also, rotate the
                    // page 90 degrees and save the result as RAW.
                    draw.setDPI(100.0) // Set the output resolution is to 100 DPI.
                    draw.setRotate(Page.e_90)  // Rotate all pages 90 degrees clockwise.

                    // create a Java image
                    val image = draw.getBitmap(page)

                    //
                    val width = image.width
                    val height = image.height
                    val arr = IntArray(width * height)
                    image.getPixels(arr, 0, width, 0, 0, width, height)
                    // pg.grabPixels();

                    // convert to byte array
                    val byteBuffer = ByteBuffer.allocate(arr.size * 4)
                    val intBuffer = byteBuffer.asIntBuffer()
                    intBuffer.put(arr)
                    val rawByteArray = byteBuffer.array()
                    // finally write the file
                    fos = FileOutputStream(Utils.createExternalFile("tiger_100dpi_rot90.raw", mFileList).absolutePath)
                    fos!!.write(rawByteArray)
                    mOutputListener!!.println("Example 3: tiger_100dpi_rot90.raw")

                    draw.setRotate(Page.e_0)  // Disable image rotation for remaining samples.

                    //--------------------------------------------------------------------------------
                    // Example 4) Convert PDF page to a fixed image size. Also illustrates some
                    // other features in PDFDraw class such as rotation, image stretching, exporting
                    // to grayscale, or monochrome.

                    // Initialize render 'gray_hint' parameter, that is used to control the
                    // rendering process. In this case we tell the rasterizer to export the image as
                    // 1 Bit Per Component (BPC) image.
                    val mono_hint = hint_set.createDict()
                    mono_hint.putNumber("BPC", 1.0)

                    // SetImageSize can be used instead of SetDPI() to adjust page  scaling
                    // dynamically so that given image fits into a buffer of given dimensions.
                    draw.setImageSize(1000, 1000)        // Set the output image to be 1000 wide and 1000 pixels tall

                    draw.export(page, Utils.createExternalFile("tiger_1000x1000.png", mFileList).absolutePath, "PNG", mono_hint)
                    mOutputListener!!.println("Example 4: tiger_1000x1000.png")

                    draw.setImageSize(200, 400) // Set the output image to be 200 wide and 300 pixels tall
                    draw.setRotate(Page.e_180) // Rotate all pages 90 degrees clockwise.

                    // 'gray_hint' tells the rasterizer to export the image as grayscale.
                    val gray_hint = hint_set.createDict()
                    gray_hint.putName("ColorSpace", "Gray")

                    draw.export(page, Utils.createExternalFile("tiger_200x400_rot180.png", mFileList).absolutePath, "PNG", gray_hint)
                    mOutputListener!!.println("Example 4: tiger_200x400_rot180.png")

                    draw.setImageSize(400, 200, false)  // The third parameter sets 'preserve-aspect-ratio' to false.
                    draw.setRotate(Page.e_0)    // Disable image rotation.
                    draw.export(page, Utils.createExternalFile("tiger_400x200_stretch.jpg", mFileList).absolutePath, "JPEG")
                    mOutputListener!!.println("Example 4: tiger_400x200_stretch.jpg")

                    //--------------------------------------------------------------------------------
                    // Example 5) Zoom into a specific region of the page and rasterize the
                    // area at 200 DPI and as a thumbnail (i.e. a 50x50 pixel image).
                    val zoom_rect = Rect(216.0, 522.0, 330.0, 600.0)
                    page.setCropBox(zoom_rect)    // Set the page crop box.

                    // Select the crop region to be used for drawing.
                    draw.setPageBox(Page.e_crop)
                    draw.setDPI(900.0)  // Set the output image resolution to 900 DPI.
                    draw.export(page, Utils.createExternalFile("tiger_zoom_900dpi.png", mFileList).absolutePath, "PNG")
                    mOutputListener!!.println("Example 5: tiger_zoom_900dpi.png")

                    // -------------------------------------------------------------------------------
                    // Example 6)
                    draw.setImageSize(50, 50)       // Set the thumbnail to be 50x50 pixel image.
                    draw.export(page, Utils.createExternalFile("tiger_zoom_50x50.png", mFileList).absolutePath, "PNG")
                    mOutputListener!!.println("Example 6: tiger_zoom_50x50.png")
                }
            } catch (e: Exception) {
                mOutputListener!!.printError(e.stackTrace)
            } finally {
                if (fos != null) {
                    try {
                        fos!!.close()
                    } catch (ignored: Exception) {
                    }
                }
            }

            val cmyk_hint = hint_set.createDict()
            cmyk_hint.putName("ColorSpace", "CMYK")

            //--------------------------------------------------------------------------------
            // Example 7) Convert the first PDF page to CMYK TIFF at 92 DPI.
            // A three step tutorial to convert PDF page to an image
            try {
                // A) Open the PDF document.
                PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "tiger.pdf")!!.absolutePath).use { doc ->
                    // Initialize the security handler, in case the PDF is encrypted.
                    doc.initSecurityHandler()

                    // B) The output resolution is set to 92 DPI.
                    draw.setDPI(92.0)

                    // C) Rasterize the first page in the document and save the result as TIFF.
                    val pg = doc.getPage(1)
                    draw.export(pg, Utils.createExternalFile("out1.tif", mFileList).absolutePath, "TIFF", cmyk_hint)
                    mOutputListener!!.println("Example 7: out1.tif")
                }
            } catch (e: Exception) {
                mOutputListener!!.printError(e.stackTrace)
            }

            //--------------------------------------------------------------------------------
            // Example 8) PDFRasterizer can be used for more complex rendering tasks, such as
            // strip by strip or tiled document rendering. In particular, it is useful for
            // cases where you cannot simply modify the page crop box (interactive viewing,
            // parallel rendering).  This example shows how you can rasterize the south-west
            // quadrant of a page.
            try {
                // A) Open the PDF document.
                PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "tiger.pdf")!!.absolutePath).use { doc ->
                    // Initialize the security handler, in case the PDF is encrypted.
                    doc.initSecurityHandler()

                    // B) Get the page matrix
                    val pg = doc.getPage(1)
                    val box = Page.e_crop
                    var mtx = pg.getDefaultMatrix(true, box, 0)
                    // We want to render a quadrant, so use half of width and height
                    val pg_w = pg.getPageWidth(box) / 2
                    val pg_h = pg.getPageHeight(box) / 2

                    // C) Scale matrix from PDF space to buffer space
                    val dpi = 96.0
                    val scale = dpi / 72.0 // PDF space is 72 dpi
                    val buf_w = Math.floor(scale * pg_w)
                    val buf_h = Math.floor(scale * pg_h)
                    val bytes_per_pixel = 4 // BGRA buffer
                    mtx.translate(0.0, -pg_h) // translate by '-pg_h' since we want south-west quadrant
                    mtx = Matrix2D(scale, 0.0, 0.0, scale, 0.0, 0.0).multiply(mtx)

                    // D) Rasterize page into memory buffer, according to our parameters
                    val rast = PDFRasterizer()
                    val buf = rast.rasterize(pg, buf_w.toInt(), buf_h.toInt(), buf_w.toInt() * bytes_per_pixel, bytes_per_pixel, true, mtx, null)

                    mOutputListener!!.println("Example 8: Successfully rasterized into memory buffer.")
                }
            } catch (e: Exception) {
                mOutputListener!!.printError(e.stackTrace)
            }

            //--------------------------------------------------------------------------------
            // Example 9) Export raster content to PNG using different image smoothing settings.
            try {
                PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "lorem_ipsum.pdf")!!.absolutePath).use { text_doc ->
                    text_doc.initSecurityHandler()

                    draw.setImageSmoothing(false, false)
                    var filename = "raster_text_no_smoothing.png"
                    draw.export(text_doc.pageIterator.next(), Utils.createExternalFile(filename, mFileList).absolutePath)
                    mOutputListener!!.println("Example 9 a): $filename. Done.")

                    filename = "raster_text_smoothed.png"
                    draw.setImageSmoothing(true, false /*default quality bilinear resampling*/)
                    draw.export(text_doc.pageIterator.next(), Utils.createExternalFile(filename, mFileList).absolutePath)
                    mOutputListener!!.println("Example 9 b): $filename. Done.")

                    filename = "raster_text_high_quality.png"
                    draw.setImageSmoothing(true, true /*high quality area resampling*/)
                    draw.export(text_doc.pageIterator.next(), Utils.createExternalFile(filename, mFileList).absolutePath)
                    mOutputListener!!.println("Example 9 c): $filename. Done.")
                }
            } catch (e: Exception) {
                mOutputListener!!.printError(e.stackTrace)
            }

            //--------------------------------------------------------------------------------
            // Example 10) Export separations directly, without conversion to an output colorspace
            try {
                PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "op_blend_test.pdf")!!.absolutePath).use { separation_doc ->
                    separation_doc.initSecurityHandler()

                    val separation_hint = hint_set.createDict()
                    separation_hint.putName("ColorSpace", "Separation")
                    draw.setDPI(96.0)
                    draw.setImageSmoothing(true, true)
                    // set overprint preview to always on
                    draw.setOverprint(1)

                    var filename = "merged_separations.png"
                    draw.export(separation_doc.getPage(1), Utils.createExternalFile(filename, mFileList).absolutePath, "PNG")
                    mOutputListener!!.println("Example 10 a): $filename. Done.")

                    filename = "separation"
                    draw.export(separation_doc.getPage(1), Utils.createExternalFile(filename, mFileList).absolutePath, "PNG", separation_hint)
                    mOutputListener!!.println("Example 10 b): " + filename + "_[ink].png. Done.")

                    filename = "separation_NChannel.tif"
                    draw.export(separation_doc.getPage(1), Utils.createExternalFile(filename, mFileList).absolutePath, "TIFF", separation_hint)
                    mOutputListener!!.println("Example 10 c): $filename. Done.")
                }
            } catch (e: Exception) {
                mOutputListener!!.printError(e.stackTrace)
            }

            // Calling Terminate when PDFNet is no longer in use is a good practice, but
            // is not required.
        } catch (e: Exception) {
            mOutputListener!!.printError(e.stackTrace)
        }

        for (file in mFileList) {
            addToFileList(file)
        }
        printFooter(outputListener)
    }

    companion object {

        private var mOutputListener: OutputListener? = null

        private val mFileList = ArrayList<String>()
    }

}
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/android/get-started/samples/pdfdrawtest.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
