Skip to main content

Code samples for PDF to Image Converter

Product update

This product has been replaced by the Pdftools SDK.

PDF to Image Converter lets you transform PDF documents into single page or multi-page raster images such as TIFF or JPEG. Here you'll find some examples of how to integrate the code in your development.

Basic Conversion

Convert PDF to JPEG

1// Create the converter
2pConverter = Pdf2ImgCreateObject();
3
4// Open input file
5if (!Pdf2ImgOpen(pConverter, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szOutputPath, Pdf2ImgGetErrorMessage(pConverter), Pdf2ImgGetErrorCode(pConverter));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Set image quality
13Pdf2ImgSetImageQuality(pConverter, iQuality);
14
15// Loop over all pages of input file and create each time a new JPEG
16for (int iPage = 1; iPage <= Pdf2ImgGetPageCount(pConverter); iPage++)
17{
18    // Change file name and extension
19    _tcstok(szOutputPath, _T("."));
20    _stprintf(szImagePath, _T("%s%d.jpg"), szOutputPath, iPage);
21
22    // Create output JPEG
23    if (!Pdf2ImgCreateImage(pConverter, szImagePath))
24    {
25        _tprintf(_T("Output image %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szInputPath, Pdf2ImgGetErrorMessage(pConverter), Pdf2ImgGetErrorCode(pConverter));
26        iReturnValue = 1;
27        goto cleanup;
28    }
29
30    // Render page from PDF to output JPEG
31    if (!Pdf2ImgRenderPage(pConverter, iPage))
32    {
33        _tprintf(_T("Page %d of PDF %s could not be rendered to JPEG %s. %s (ErrorCode: 0x%08x).\n"), iPage, szInputPath, szImagePath, Pdf2ImgGetErrorMessage(pConverter), Pdf2ImgGetErrorCode(pConverter));
34        iReturnValue = 1;
35        goto cleanup;
36    }
37
38    // Close output image
39    if (!Pdf2ImgCloseImage(pConverter))
40    {
41        _tprintf(_T("Output image %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szImagePath, Pdf2ImgGetErrorMessage(pConverter), Pdf2ImgGetErrorCode(pConverter));
42        iReturnValue = 1;
43        goto cleanup;
44    }
45}
46
Download code sample
1// Create the converter
2using (Converter converter = new Converter())
3{
4    // Open input file
5    if (!converter.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, converter.ErrorMessage, converter.ErrorCode));
8
9    // Set image quality
10    converter.ImageQuality = quality;
11
12    // Loop over all pages of input file and create each time a new JPEG.
13    for (int pageNo = 1; pageNo <= converter.PageCount; pageNo++)
14    {
15        string imagePath = Path.ChangeExtension(outputPath, null) + pageNo.ToString() + ".jpg";
16
17        // Create output JPEG
18        if (!converter.CreateImage(imagePath))
19            throw new Exception(String.Format("Output image {0} cannot be created. {1} " + 
20                "(ErrorCode: 0x{2:x}).", imagePath, converter.ErrorMessage,
21                converter.ErrorCode));
22
23        // Render page from PDF to output JPEG
24        if (!converter.RenderPage(pageNo))
25            throw new Exception(String.Format("Page {0} of PDF {1} could not be rendered to " + 
26                "JPEG {2}. {3} (ErrorCode: 0x{4:x}).", pageNo, inputPath, imagePath, 
27                converter.ErrorMessage, converter.ErrorCode));
28
29        // Close output image
30        if (!converter.CloseImage())
31            throw new Exception(String.Format("Output image {0} cannot be closed. {1} " + 
32                "(ErrorCode: 0x{2:x}).", imagePath, converter.ErrorMessage, converter.ErrorCode));
33    }
34}
35
Download code sample
1// Create the converter
2converter = new Pdf2Img();
3
4// Open input file
5if (!converter.open(inputPath, "")) 
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
7        inputPath, converter.getErrorMessage(), converter.getErrorCode()));
8
9converter.setImageQuality(quality);
10
11// Loop over all pages of input file and create each time a new JPEG
12for (int pageNo = 1; pageNo <= converter.getPageCount(); pageNo++)
13{
14    // Construct path of output image in case of a multi-page input PDF
15    String imagePath = outputPath.substring(0, outputPath.lastIndexOf(".")) + pageNo + ".jpg";
16
17    // Create output JPEG
18    if (!converter.createImage(imagePath))
19        throw new IOException(String.format("Output image %s cannot be created. " + 
20            "%s (ErrorCode: 0x%08x).", imagePath, converter.getErrorMessage(), 
21            converter.getErrorCode()));
22
23    // Render page from PDF to output JPEG
24    if (!converter.renderPage(pageNo))
25        throw new IOException(String.format("Page %d of PDF %d could not be rendered to JPEG %s. " +
26            "%s (ErrorCode: 0x%08x).", pageNo, inputPath, imagePath, converter.getErrorMessage(), 
27            converter.getErrorCode()));
28
29    // Close output image
30    if (!converter.closeImage())
31        throw new IOException(String.format("Output image %s cannot be closed. %s " + 
32            "(ErrorCode: 0x%08x).", imagePath, converter.getErrorMessage(), 
33            converter.getErrorCode()));
34}
35
Download code sample

Convert PDF to PNG

1// Create the converter
2pConverter = Pdf2ImgCreateObject();
3
4// Open input file
5if (!Pdf2ImgOpen(pConverter, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szOutputPath, Pdf2ImgGetErrorMessage(pConverter), Pdf2ImgGetErrorCode(pConverter));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Loop over all pages of input file and create each time a new PNG
13for (int iPage = 1; iPage <= Pdf2ImgGetPageCount(pConverter); iPage++)
14{
15    // Change file name and extension
16    _tcstok(szOutputPath, _T("."));
17    _stprintf(szImagePath, _T("%s%d.png"), szOutputPath, iPage);
18
19    // Create output PNG
20    if (!Pdf2ImgCreateImage(pConverter, szImagePath))
21    {
22        _tprintf(_T("Output image %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szImagePath, Pdf2ImgGetErrorMessage(pConverter), Pdf2ImgGetErrorCode(pConverter));
23        iReturnValue = 1;
24        goto cleanup;
25    }
26
27    // Render page from PDF to output PNG
28    if (!Pdf2ImgRenderPage(pConverter, iPage))
29    {
30        _tprintf(_T("Page %d of PDF %s could not be rendered to PNG %s. %s (ErrorCode: 0x%08x).\n"), iPage, szInputPath, szImagePath, Pdf2ImgGetErrorMessage(pConverter), Pdf2ImgGetErrorCode(pConverter));
31        iReturnValue = 1;
32        goto cleanup;
33    }
34
35    // Close output image
36    if (!Pdf2ImgCloseImage(pConverter))
37    {
38        _tprintf(_T("Output image %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szImagePath, Pdf2ImgGetErrorMessage(pConverter), Pdf2ImgGetErrorCode(pConverter));
39        iReturnValue = 1;
40        goto cleanup;
41    }
42}
43
Download code sample
1// Create the converter
2using (Converter converter = new Converter())
3{
4    // Open input file
5    if (!converter.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, converter.ErrorMessage, converter.ErrorCode));
8
9    // Loop over all pages of input file and create each time a new PNG.
10    for (int pageNo = 1; pageNo <= converter.PageCount; pageNo++)
11    {
12        string imagePath = Path.ChangeExtension(outputPath, null) + pageNo.ToString() + ".png";
13
14        // Create output PNG
15        if (!converter.CreateImage(imagePath))
16            throw new Exception(String.Format("Output image {0} cannot be created. {1} " + 
17                "(ErrorCode: 0x{2:x}).", imagePath, converter.ErrorMessage, converter.ErrorCode));
18
19        // Render page from PDF to output PNG
20        if (!converter.RenderPage(pageNo))
21            throw new Exception(String.Format("Page {0} of PDF {1} could not be rendered to " + 
22                "PNG {2}. {3} (ErrorCode: 0x{4:x}).", pageNo, inputPath, imagePath,
23                converter.ErrorMessage, converter.ErrorCode));
24
25        // Close output image
26        if (!converter.CloseImage())
27            throw new Exception(String.Format("Output image {0} cannot be closed. {1} " + 
28                "(ErrorCode: 0x{2:x}).", imagePath, converter.ErrorMessage, converter.ErrorCode));
29    }
30}
31
Download code sample
1// Create the converter
2converter = new Pdf2Img();
3
4// Open input file
5if (!converter.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
7        inputPath, converter.getErrorMessage(), converter.getErrorCode()));
8
9// Loop over all pages of input file and create each time a new PNG
10for (int pageNo = 1; pageNo <= converter.getPageCount(); pageNo++)
11{
12    // Construct path of output image in case of a multi-page input PDF
13    String imagePath = outputPath.substring(0, outputPath.lastIndexOf(".")) + pageNo + ".png";
14
15    // Create output PNG
16    if (!converter.createImage(imagePath))
17        throw new IOException(String.format("Output image %s cannot be created. " + 
18            "%s (ErrorCode: 0x%08x).",imagePath, converter.getErrorMessage(), 
19            converter.getErrorCode()));
20
21    // Render page from PDF to output PNG
22    if (!converter.renderPage(pageNo))
23        throw new IOException(String.format("Page %d of PDF %d could not be rendered to PNG %s. " + 
24            "%s (ErrorCode: 0x%08x).", pageNo, inputPath, imagePath, converter.getErrorMessage(), 
25            converter.getErrorCode()));
26
27    // Close output image
28    if (!converter.closeImage())
29        throw new IOException(String.format("Output image %s cannot be closed. " + 
30            "%s (ErrorCode: 0x%08x).", imagePath, converter.getErrorMessage(), 
31            converter.getErrorCode()));
32}
33
Download code sample

Convert PDF to TIFF and set specific compression

1// Create the converter
2pConverter = Pdf2ImgCreateObject();
3
4// Open input file
5if (!Pdf2ImgOpen(pConverter, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, Pdf2ImgGetErrorMessage(pConverter), Pdf2ImgGetErrorCode(pConverter));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Change file name and extension
13_tcstok(szOutputPath, _T("."));
14_stprintf(szImagePath, _T("%s.tif"), szOutputPath);
15
16// Set compression and quality
17Pdf2ImgSetCompression(pConverter, eCompression);
18Pdf2ImgSetQuality(pConverter, iQuality);
19
20// Create output TIFF
21if (!Pdf2ImgCreateImage(pConverter, szImagePath))
22{
23    _tprintf(_T("Output image %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szImagePath, Pdf2ImgGetErrorMessage(pConverter), Pdf2ImgGetErrorCode(pConverter));
24    iReturnValue = 1;
25    goto cleanup;
26}
27
28// Loop over all pages of input file and create each time a new TIFF
29for (int iPage = 1; iPage <= Pdf2ImgGetPageCount(pConverter); iPage++)
30{
31    // Render pages from PDF to output TIFF
32    if (!Pdf2ImgRenderPage(pConverter, iPage))
33    {
34        _tprintf(_T("Page %d of PDF %s could not be rendered to TIFF %s. %s (ErrorCode: 0x%08x).\n"), iPage, szInputPath, szImagePath, Pdf2ImgGetErrorMessage(pConverter), Pdf2ImgGetErrorCode(pConverter));
35        iReturnValue = 1;
36        goto cleanup;
37    }
38}
39// Close output image
40if (!Pdf2ImgCloseImage(pConverter))
41{
42    _tprintf(_T("Output image %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szImagePath, Pdf2ImgGetErrorMessage(pConverter), Pdf2ImgGetErrorCode(pConverter));
43    iReturnValue = 1;
44    goto cleanup;
45}
46
Download code sample
1// Create the converter
2using (Converter converter = new Converter())
3{
4    // Open input file
5    if (!converter.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. {1} (ErrorCode: {2}).",
7            inputPath, converter.ErrorMessage, converter.ErrorCode));
8
9    // Set compression and quality
10    converter.Compression = compression;
11    converter.ImageQuality = quality;
12
13    // Create output TIFF
14    if (!converter.CreateImage(outputPath))
15        throw new Exception(String.Format("Output image {0} cannot be created. " +
16            "{1} (ErrorCode: {2}).", outputPath, converter.ErrorMessage, converter.ErrorCode));
17
18    // Loop over all pages of input file and create a multi-page TIFF file
19    for (int pageNo = 1; pageNo <= converter.PageCount; pageNo++)
20    {
21        // Render pages from PDF to output TIFF
22        if (!converter.RenderPage(pageNo))
23            throw new Exception(String.Format("Page {0} of PDF {1} could not be rendered to " +
24                "TIFF {2}. {3} (ErrorCode: {4}).", pageNo, inputPath, outputPath,
25                converter.ErrorMessage, converter.ErrorCode));
26    }
27    // Close output image
28    if (!converter.CloseImage())
29        throw new Exception(String.Format("Output image {0} cannot be closed. " +
30            "{1} (ErrorCode: {2}).", outputPath, converter.ErrorMessage, converter.ErrorCode));
31}
32
Download code sample
1// Create the converter
2converter = new Pdf2Img();
3
4// Open input file
5if (!converter.open(inputPath, ""))
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
7        inputPath, converter.getErrorMessage(), converter.getErrorCode()));
8
9// Set compression and quality
10converter.setCompression(compression);
11converter.setImageQuality(quality);
12
13// Create output TIFF 
14if (!converter.createImage(outputPath))
15    throw new IOException(String.format("Output image %s cannot be created. " + 
16        "%s (ErrorCode: 0x%08x).", outputPath, converter.getErrorMessage(),
17        converter.getErrorCode()));
18
19// Loop over all pages of input file and create multi-page TIFF file
20for (int pageNo = 1; pageNo <= converter.getPageCount(); pageNo++)
21{
22    // Render pages from PDF to output TIFF
23    if (!converter.renderPage(pageNo))
24        throw new IOException(String.format("Page %d of PDF %d could not be rendered to TIFF %s." +
25            " %s (ErrorCode: 0x%08x).", pageNo, inputPath, outputPath, converter.getErrorMessage(), 
26            converter.getErrorCode()));
27}
28// Close output image
29if (!converter.closeImage())
30    throw new IOException(String.format("Output image %s cannot be closed. " +
31        "%s (ErrorCode: 0x%08x).", outputPath, converter.getErrorMessage(), 
32        converter.getErrorCode()));
33
Download code sample

Features and Configuration

Convert PDF to image and set fax settings

1// Create the converter
2pConverter = Pdf2ImgCreateObject();
3
4// Open input file
5if (!Pdf2ImgOpen(pConverter, szInputPath, _T("")))
6{
7    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
8        Pdf2ImgGetErrorMessage(pConverter), Pdf2ImgGetErrorCode(pConverter));
9    iReturnValue = 1;
10    goto cleanup;
11}
12
13// Set dithering
14Pdf2ImgSetDithering(pConverter, eDithering);
15Pdf2ImgSetBitsPerPixel(pConverter, 1);
16
17Pdf2ImgSetFitPage(pConverter, 1);
18Pdf2ImgSetCenter(pConverter, 1);
19TPDFRendererOption2 renderOptions = Pdf2ImgGetOptions2(pConverter);
20renderOptions |= eOptionNoAntialiasing | eOptionUseBoxFilter | eOptionFitPaths;
21Pdf2ImgSetOptions2(pConverter, renderOptions);
22Pdf2ImgSetRotateMode(pConverter, eRotatePortrait);
23
24// Set the TIFF type fax settings
25Pdf2ImgFaxHSetting(pConverter);
26
27// Create output image
28if (!Pdf2ImgCreateImage(pConverter, szOutputPath))
29{
30    _tprintf(_T("Output image %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
31        Pdf2ImgGetErrorMessage(pConverter), Pdf2ImgGetErrorCode(pConverter));
32    iReturnValue = 1;
33    goto cleanup;
34}
35
36// Loop over all pages of input file and create a multi-page TIFF file
37for (int iPage = 1; iPage <= Pdf2ImgGetPageCount(pConverter); iPage++)
38{
39    // Render pages from PDF to output image
40    if (!Pdf2ImgRenderPage(pConverter, iPage))
41    {
42        _tprintf(_T("Page %d of PDF %s could not be rendered to image %s. %s (ErrorCode: 0x%08x).\n"),
43            iPage, szInputPath, szOutputPath, Pdf2ImgGetErrorMessage(pConverter),
44            Pdf2ImgGetErrorCode(pConverter));
45        iReturnValue = 1;
46        goto cleanup;
47    }
48}
49
50// Close output image
51if (!Pdf2ImgCloseImage(pConverter))
52{
53    _tprintf(_T("Output image %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
54        Pdf2ImgGetErrorMessage(pConverter), Pdf2ImgGetErrorCode(pConverter));
55    iReturnValue = 1;
56    goto cleanup;
57}
58
Download code sample
1// Create the converter
2using (Converter converter = new Converter())
3{
4    // Open input file
5    if (!converter.Open(inputPath, ""))
6        throw new Exception(String.Format("Input file {0} cannot be opened. " +
7            "{1} (ErrorCode: 0x{2:x}).", inputPath, converter.ErrorMessage, converter.ErrorCode));
8
9    // Set dithering
10    converter.Dithering = dithering;
11    converter.BitsPerPixel = 1;
12
13    converter.FitPage = true;
14    converter.Center = true;
15    PDFRendererOption2 rendererOptions = converter.Options2;
16    rendererOptions |= PDFRendererOption2.eOptionNoAntialiasing |
17        PDFRendererOption2.eOptionUseBoxFilter | PDFRendererOption2.eOptionFitPaths;
18    converter.Options2 = rendererOptions;
19    converter.RotateMode = PDFRotateMode.eRotatePortrait;
20
21    // Set the TIFF type fax settings
22    converter.FaxHSetting();
23
24    // Create output image
25    if (!converter.CreateImage(outputPath))
26        throw new Exception(String.Format("Output image {0} cannot be created. {1} " +
27            "(ErrorCode: 0x{2:x}).", outputPath, converter.ErrorMessage, converter.ErrorCode));
28
29    // Loop over all pages of input file and create each time a new image.
30    for (int pageNo = 1; pageNo <= converter.PageCount; pageNo++)
31    {
32        // Render page from PDF to output image
33        if (!converter.RenderPage(pageNo))
34            throw new Exception(String.Format("Page {0} of PDF {1} could not be rendered to " +
35                "image {2}. {3} (ErrorCode: 0x{4:x}).", pageNo, inputPath, outputPath,
36                converter.ErrorMessage, converter.ErrorCode));
37    }
38
39    // Close output image
40    if (!converter.CloseImage())
41        throw new Exception(String.Format("Output image {0} cannot be closed. {1} " +
42            "(ErrorCode: 0x{2:x}).", outputPath, converter.ErrorMessage, converter.ErrorCode));
43}
44
Download code sample
1// Create the converter
2converter = new Pdf2Img();
3
4// Open input file
5if (!converter.open(inputPath, "")) 
6    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
7        inputPath, converter.getErrorMessage(), converter.getErrorCode()));
8
9// Set dithering
10converter.setDithering(dithering);
11converter.setBitsPerPixel(1);
12
13converter.setFitPage(1);
14converter.setCenter(true);
15int renderOptions = converter.getOptions2();
16renderOptions |= NativeLibrary.RENDEREROPTION2.eOptionNoAntialiasing | 
17        NativeLibrary.RENDEREROPTION2.eOptionUseBoxFilter | 
18        NativeLibrary.RENDEREROPTION2.eOptionFitPaths;
19converter.setOptions2(renderOptions);
20converter.setRotateMode(NativeLibrary.ROTATEMODE.eRotatePortrait);
21
22// Set the TIFF type fax settings
23converter.faxHSetting();
24
25// Create output image 
26if (!converter.createImage(outputPath))
27    throw new IOException(String.format("Output image %s cannot be created. %s " + 
28        "(ErrorCode: 0x%08x).", outputPath, converter.getErrorMessage(), 
29        converter.getErrorCode()));
30
31// Loop over all pages of input file and create a multi-page TIFF file
32for (int pageNo = 1; pageNo <= converter.getPageCount(); pageNo++)
33{
34    // Render page from PDF to output image
35    if (!converter.renderPage(pageNo))
36        throw new IOException(String.format("Page %d of PDF %d could not be rendered to image %s." +
37            " %s (ErrorCode: 0x%08x).", pageNo, inputPath, outputPath, converter.getErrorMessage(), 
38            converter.getErrorCode()));
39}
40
41// Close output image
42if (!converter.closeImage())
43    throw new IOException(String.format("Output image %s cannot be closed. " + 
44        "%s (ErrorCode: 0x%08x).", outputPath, converter.getErrorMessage(), 
45        converter.getErrorCode()));
46
Download code sample

In Memory

Create image in memory from PDF

1// Create the converter
2pConverter = Pdf2ImgCreateObject();
3
4// Open input file
5if (!Pdf2ImgOpenMem(pConverter, pInputBuffer, nLength, _T("")))
6{
7    _tprintf(_T("Input buffer cannot be opened. %s (ErrorCode: 0x%08x).\n"), Pdf2ImgGetErrorMessage(pConverter), Pdf2ImgGetErrorCode(pConverter));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Loop over all pages of input buffer and create each time a new image
13for (int iPage = 1; iPage <= Pdf2ImgGetPageCount(pConverter); iPage++)
14{
15    // Change file name and extension
16    _tcstok(szOutputPath, _T("."));
17    _stprintf(szImagePath, _T("%s%d%s"), szOutputPath, iPage, szExtension);
18
19    // Create output buffer
20    if (!Pdf2ImgCreateImageInMemory(pConverter, szExtension))
21    {
22        _tprintf(_T("Output buffer cannot be created. %s (ErrorCode: 0x%08x).\n"), Pdf2ImgGetErrorMessage(pConverter), Pdf2ImgGetErrorCode(pConverter));
23        iReturnValue = 1;
24        goto cleanup;
25    }
26
27    // Render pages from PDF to output image
28    if (!Pdf2ImgRenderPage(pConverter, iPage))
29    {
30        _tprintf(_T("Page %d of PDF %s could not be rendered to image %s. %s (ErrorCode: 0x%08x).\n"), iPage, szInputPath, szImagePath, Pdf2ImgGetErrorMessage(pConverter), Pdf2ImgGetErrorCode(pConverter));
31        iReturnValue = 1;
32        goto cleanup;
33    }
34
35    // Get PDF as buffer
36    TPDFByteArray* pOutputBuffer = Pdf2ImgGetImage(pConverter);
37
38    if (!(pData = _tfopen(szOutputPath, _T("wb"))))
39    {
40        _tprintf(_T("Failed to create output file %s.\n"), szOutputPath);
41        iReturnValue = 1;
42        goto cleanup;
43    }
44
45    // Write bytes to output file
46    fwrite(pOutputBuffer->m_pData, pOutputBuffer->m_nLength, 1, pData);
47    fclose(pData);
48}
49
Download code sample
1// Create the converter
2using (Converter converter = new Converter())
3{
4    // Open input file
5    if (!converter.OpenMem(inputBuffer, ""))
6        throw new Exception(String.Format("Input buffer cannot be opened. " +
7            "{0} (ErrorCode: 0x{1:x}).", converter.ErrorMessage, converter.ErrorCode));
8
9    // Loop over all pages of input buffer and create each time a new image.
10    for (int pageNo = 1; pageNo <= converter.PageCount; pageNo++)
11    {
12        // Construct path of output image in case of a multi-page input PDF
13        string imagePath = converter.PageCount == 1 ? outputPath : Path.ChangeExtension(outputPath, null) + pageNo.ToString() +  extension;
14
15        // Create output buffer
16        if (!converter.CreateImageInMemory(extension))
17            throw new Exception(String.Format("Output buffer cannot be created. " + 
18                "{0} (ErrorCode: 0x{1:x}).", converter.ErrorMessage, converter.ErrorCode));
19
20        // Render page from input PDF to output image
21        if (!converter.RenderPage(pageNo))
22            throw new Exception(String.Format("Page {0} of PDF {1} could not be rendered to " +
23                "image {2}. {3} (ErrorCode: 0x{4:x}).", pageNo, inputPath,
24                imagePath, converter.ErrorMessage, converter.ErrorCode));
25
26        byte[] outputBuffer = converter.GetImage();
27
28        if (outputBuffer == null)
29            throw new Exception(String.Format("Getting buffer of output image failed." + 
30                "{0} (ErrorCode: 0x{1:x}).", converter.ErrorMessage, converter.ErrorCode));
31
32        // Write bytes to output file.
33        File.WriteAllBytes(imagePath, outputBuffer);
34    }
35}
36
Download code sample
1// Create the converter
2converter = new Pdf2Img();
3
4// Open input file
5if (!converter.openMem(inputBuffer, "")) 
6    throw new IOException(String.format("Input buffer cannot be opened. %s (ErrorCode: 0x%08x).",
7        converter.getErrorMessage(), converter.getErrorCode()));
8
9// Loop over all pages of input file and create each time a new image
10for (int pageNo = 1; pageNo <= converter.getPageCount(); pageNo++)
11{
12    // Construct path of output image in case of a multi-page input PDF
13    String imagePath = outputPath.substring(0, outputPath.lastIndexOf(".")) + pageNo + extension;
14
15    // Create output buffer 
16    if (!converter.createImageInMemory(extension))
17        throw new IOException(String.format("Output buffer cannot be created. " +
18            "%s (ErrorCode: 0x%08x).", converter.getErrorMessage(), converter.getErrorCode()));
19
20    // Render page from input PDF to output image
21    if (!converter.renderPage(pageNo))
22        throw new IOException(String.format("Page %d of PDF %s could not be rendered to image %s." +
23            " %s (ErrorCode: 0x%08x).", pageNo, inputPath, imagePath, converter.getErrorMessage(), 
24            converter.getErrorCode()));
25
26    // Get image as buffer
27    byte[] outputBuffer = converter.getImage();
28    if (outputBuffer == null)
29        throw new IOException(String.format("Getting buffer of output image failed. " + 
30            "%s (ErrorCode: 0x%08x).", converter.getErrorMessage(), converter.getErrorCode()));
31
32    // Write bytes to output file
33    Files.write(Paths.get(imagePath), outputBuffer, StandardOpenOption.CREATE_NEW);
34}
35
36// Close file of input buffer
37if (!converter.close())
38    throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
39        inputPath, converter.getErrorMessage(), converter.getErrorCode()));
40
Download code sample