Image to PDF Converter

Documentation & code samples

.NET Core
Java
C#
C/C++

Conversion

Convert Image to PDF and embed metadata

1// Create the converter
2using (Img2Pdf converter = new Img2Pdf())
3{
4    // Create output file
5    if (!converter.Create(outPath, "", "", PDFPermission.ePermNoEncryption))
6        throw new Exception(String.Format("Output file {0} cannot be created. {1} " +
7            "(ErrorCode: 0x{2:x})", outPath, converter.ErrorMessage, converter.ErrorCode));
8
9    // Set xmp metadata
10    converter.SetMetadata(metaFile);
11
12    // Convert pages from input image to PDF
13    if (!converter.CreatePageFromImage(inPath))
14        throw new Exception(String.Format("Pages cannot be created from image input {0}. " +
15            "{1} (ErrorCode: 0x{2:x})", inPath, converter.ErrorMessage, converter.ErrorCode));
16
17    // Close output file
18    if (!converter.Close())
19        throw new Exception(String.Format("Output file {0} cannot be closed. " +
20            "{1} (ErrorCode: 0x{2:x})", outPath, converter.ErrorMessage, converter.ErrorCode));
21}
22
Download code samples for
1// Create the converter
2converter = new Img2Pdf();
3
4// Create output file
5if (!converter.create(outPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
6    throw new Exception(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
7            outPath, converter.getErrorMessage(), converter.getErrorCode()));
8
9// Set xmp metadata
10converter.setMetadata(metaFile);
11
12// Convert pages from input image to PDF
13if (!converter.createPageFromImage(inPath))
14    throw new Exception(String.format("Pages cannot be created from image input %s. %s " +
15            "(ErrorCode: 0x%08x).", inPath, converter.getErrorMessage(), converter.getErrorCode()));
16
17// Close output file
18if (!converter.close())
19    throw new Exception(String.format("Output file %s cannot be closed. %s (ErrorCode: 0x%08x).",
20            outPath, converter.getErrorMessage(), converter.getErrorCode()));
21
Download code samples for
1// Create the converter
2pConverter = Img2PdfCreateObject();
3
4// Create output file
5if (!Img2PdfCreate(pConverter, szOutPath, _T(""), _T(""), ePermNoEncryption))
6{
7    _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Set xmp metadata
13Img2PdfSetMetadata(pConverter, szMetaFile);
14
15// Convert pages from input image to PDF
16if (!Img2PdfCreatePageFromImage(pConverter, szInPath))
17{
18    _tprintf(_T("Pages cannot be created from image input %s. %s (ErrorCode: 0x%08x).\n"), szInPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
19    iReturnValue = 1;
20    goto cleanup;
21}
22
23// Close output file
24if (!Img2PdfClose(pConverter))
25{
26    _tprintf(_T("Output file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szOutPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
27    iReturnValue = 1;
28    goto cleanup;
29}
30
Download code samples for

Convert Image to PDF and set compression

1// Create the converter
2using (Img2Pdf converter = new Img2Pdf())
3{
4    // Create output file
5    if (!converter.Create(outPath, "", "", PDFPermission.ePermNoEncryption))
6        throw new Exception(String.Format("Output file {0} cannot be created. {1}" + 
7            "(ErrorCode: 0x{2:x})", outPath, converter.ErrorMessage, converter.ErrorCode));
8
9    // Set compression
10    converter.ContinuousCompression = compression;
11
12    // Set image quality
13    converter.ImageQuality = quality;
14
15    // Convert pages from input image to PDF
16    if (!converter.CreatePageFromImage(inPath))
17        throw new Exception(String.Format("Pages cannot be created from image input {0}. " +
18            "{1} (ErrorCode: 0x{2:x})", inPath, converter.ErrorMessage, converter.ErrorCode));
19
20    // Close output file
21    if (!converter.Close())
22        throw new Exception(String.Format("Output file {0} cannot be closed. {1}" + 
23            "(ErrorCode: 0x{2:x})", outPath, converter.ErrorMessage, converter.ErrorCode));
24}
25
Download code samples for
1// Create the converter
2converter = new Img2Pdf();
3
4// Create output file
5if (!converter.create(outPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
6    throw new Exception(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
7        outPath, converter.getErrorMessage(), converter.getErrorCode()));
8
9// Set compression
10converter.setContinuousCompression(compression);
11
12// Set image quality
13converter.setImageQuality(quality);
14
15// Convert pages from input image to PDF
16if (!converter.createPageFromImage(inPath))
17    throw new Exception(String.format("Pages cannot be created from image input %s. %s " + 
18        "(ErrorCode: 0x%08x).", inPath, converter.getErrorMessage(), converter.getErrorCode()));
19
20// Close output file
21if (!converter.close())
22    throw new Exception(String.format("Output file %s cannot be closed. %s (ErrorCode: 0x%08x).",
23        outPath, converter.getErrorMessage(), converter.getErrorCode()));
24
Download code samples for
1// Create the converter
2pConverter = Img2PdfCreateObject();
3
4// Create output file
5if (!Img2PdfCreate(pConverter, szOutPath, _T(""), _T(""), ePermNoEncryption))
6{
7    _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Set compression
13Img2PdfSetContinuousCompression(pConverter, eCompression);
14
15// Set image quality
16Img2PdfSetImageQuality(pConverter, iQuality);
17
18// Convert pages from input image to PDF
19if (!Img2PdfCreatePageFromImage(pConverter, szInPath))
20{
21    _tprintf(_T("Pages cannot be created from image input %s. %s (ErrorCode: 0x%08x).\n"), szInPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
22    iReturnValue = 1;
23    goto cleanup;
24}
25
26// Close output file
27if (!Img2PdfClose(pConverter))
28{
29    _tprintf(_T("Output file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szOutPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
30    iReturnValue = 1;
31    goto cleanup;
32}
33
Download code samples for

Convert Image to PDF/A

1// Create the converter
2using (Img2Pdf converter = new Img2Pdf())
3{
4    // Set PDF Compliance level
5    converter.Compliance = compliance;
6
7    // Create output file
8    if (!converter.Create(outPath, "", "", PDFPermission.ePermNoEncryption))
9        throw new Exception(String.Format("Output file {0} cannot be created. {1} "+ 
10            "(ErrorCode: 0x{2:x})", outPath, converter.ErrorMessage, converter.ErrorCode));
11
12    // Convert pages from input image to PDF
13    if (!converter.CreatePageFromImage(inPath))
14        throw new Exception(String.Format("Pages cannot be created from image input {0}. " + 
15            "{1} (ErrorCode: 0x{2:x})", inPath, converter.ErrorMessage, converter.ErrorCode));
16
17    // Close output file
18    if (!converter.Close())
19        throw new Exception(String.Format("Output file {0} cannot be closed. " + 
20            "{1} (ErrorCode: 0x{2:x})", outPath, converter.ErrorMessage, converter.ErrorCode));
21}
22
Download code samples for
1// Create the converter
2converter = new Img2Pdf();
3
4// Set PDF Compliance level 
5converter.setCompliance(compliance);
6
7// Create output file
8if (!converter.create(outPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
9    throw new Exception(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
10        outPath, converter.getErrorMessage(), converter.getErrorCode()));
11
12// Convert pages from input image to PDF 
13if (!converter.createPageFromImage(inPath))
14    throw new Exception(String.format("Pages cannot be created from image input %s. %s " + 
15        "(ErrorCode: 0x%08x).", inPath, converter.getErrorMessage(), converter.getErrorCode()));
16
17// Close output file
18if (!converter.close())
19    throw new Exception(String.format("Output file %s cannot be closed. %s (ErrorCode: 0x%08x).",
20        outPath, converter.getErrorMessage(), converter.getErrorCode()));
21
Download code samples for
1// Create the converter
2pConverter = Img2PdfCreateObject();
3
4// Set PDF Compliance level
5Img2PdfSetCompliance(pConverter, eCompliance);
6
7// Create output file
8if (!Img2PdfCreate(pConverter, szOutPath, _T(""), _T(""), ePermNoEncryption))
9{
10    _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
11    iReturnValue = 1;
12    goto cleanup;
13}
14
15// Convert pages from input image to PDF
16if (!Img2PdfCreatePageFromImage(pConverter, szInPath))
17{
18    _tprintf(_T("Pages cannot be created from image input %s. %s (ErrorCode: 0x%08x).\n"), szInPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
19    iReturnValue = 1;
20    goto cleanup;
21}
22
23// Close output file
24if (!Img2PdfClose(pConverter))
25{
26    _tprintf(_T("Output file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szOutPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
27    iReturnValue = 1;
28    goto cleanup;
29}
30
Download code samples for

Convert Image to encrypted PDF

1// Create the converter
2using (Img2Pdf converter = new Img2Pdf())
3{
4    // Create encrypted output file
5    if (!converter.Create(outPath, userPW, ownerPW, PDFPermission.ePermDigitalPrint | 
6        PDFPermission.ePermPrint))
7        throw new Exception(String.Format("Output file {0} cannot be created. {1} " + 
8            "(ErrorCode: 0x{2:x})", outPath, converter.ErrorMessage, converter.ErrorCode));
9
10    // Convert pages from input image to PDF
11    if (!converter.CreatePageFromImage(inPath))
12        throw new Exception(String.Format("Pages cannot be created from image input {0}. " + 
13            "{1} (ErrorCode: 0x{2:x})", inPath, converter.ErrorMessage, converter.ErrorCode));
14
15    // Close output file
16    if (!converter.Close())
17        throw new Exception(String.Format("Output file {0} cannot be closed. {1} " + 
18            "(ErrorCode: 0x{2:x})", outPath, converter.ErrorMessage, converter.ErrorCode));
19}
20
Download code samples for
1// Create the converter
2converter = new Img2Pdf();
3
4// Create encrypted output file
5if (!converter.create(outPath, userPW, ownerPW, NativeLibrary.PERMISSION.ePermPrint | 
6    NativeLibrary.PERMISSION.ePermDigitalPrint))
7    throw new Exception(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
8        outPath, converter.getErrorMessage(), converter.getErrorCode()));
9
10// Convert pages from input image to PDF 
11if (!converter.createPageFromImage(inPath))
12    throw new Exception(String.format("Pages cannot be created from image input %s. %s " + 
13        "(ErrorCode: 0x%08x).", inPath, converter.getErrorMessage(), converter.getErrorCode()));
14
15// Close output file
16if (!converter.close())
17    throw new Exception(String.format("Output file %s cannot be closed. %s (ErrorCode: 0x%08x).",
18        outPath, converter.getErrorMessage(), converter.getErrorCode()));
19
Download code samples for
1pConverter = Img2PdfCreateObject();
2
3// Create encrypted output file
4if (!Img2PdfCreate(pConverter, szOutPath, szUserPW, szOwnerPW, ePermDigitalPrint | ePermPrint))
5{
6    _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
7    iReturnValue = 1;
8    goto cleanup;
9}
10
11// Convert pages from input image to PDF
12if (!Img2PdfCreatePageFromImage(pConverter, szInPath))
13{
14    _tprintf(_T("Pages cannot be created from image input %s. %s (ErrorCode: 0x%08x).\n"), szInPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
15    iReturnValue = 1;
16    goto cleanup;
17}
18
19// Close output file
20if (!Img2PdfClose(pConverter))
21{
22    _tprintf(_T("Output file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szOutPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
23    iReturnValue = 1;
24    goto cleanup;
25}
26
Download code samples for

Convert Image to PDF and set InfoDictionary

1// Create the converter
2using (Img2Pdf converter = new Img2Pdf())
3{
4    // Create output file
5    if (!converter.Create(outPath, "", "", PDFPermission.ePermNoEncryption))
6        throw new Exception(String.Format("Output file {0} cannot be created. {1}" + 
7            "(ErrorCode: 0x{2:x})", outPath, converter.ErrorMessage, converter.ErrorCode));
8
9    // Set info dictionary
10    converter.SetInfoEntry("Title", "Your Title");
11    converter.SetInfoEntry("Author", "Your Author");
12    converter.SetInfoEntry("Creator", "Your Creator");
13    converter.SetInfoEntry("Subject", "Your Subject");
14    converter.SetInfoEntry("Producer", "Your Producer");
15
16    // Convert pages from input image to PDF
17    if (!converter.CreatePageFromImage(inPath))
18        throw new Exception(String.Format("Pages cannot be created from image input {0}. {1}" + 
19            "(ErrorCode: 0x{2:x})", inPath, converter.ErrorMessage, converter.ErrorCode));
20
21    // Close output file
22    if (!converter.Close())
23        throw new Exception(String.Format("Output file {0} cannot be closed. {1} " + 
24            "(ErrorCode: 0x{2:x})", outPath, converter.ErrorMessage, converter.ErrorCode));
25}
26
Download code samples for
1// Create the converter
2converter = new Img2Pdf();
3
4// Create output file
5if (!converter.create(outPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
6    throw new Exception(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
7        outPath, converter.getErrorMessage(), converter.getErrorCode()));
8
9// Set info dictionary
10converter.setInfoEntry("Title", "Your Title");
11converter.setInfoEntry("Author", "Your Author");
12converter.setInfoEntry("Creator", "Your Creator");
13converter.setInfoEntry("Subject", "Your Subject");
14converter.setInfoEntry("Producer", "Your Producer");
15
16// Convert pages from input image to PDF
17if (!converter.createPageFromImage(inPath))
18    throw new Exception(String.format("Pages cannot be created from image input %s. %s " + 
19        "(ErrorCode: 0x%08x).", inPath, converter.getErrorMessage(), converter.getErrorCode()));
20
21// Close output file
22if (!converter.close())
23    throw new Exception(String.format("Output file %s cannot be closed. %s (ErrorCode: 0x%08x).",
24        outPath, converter.getErrorMessage(), converter.getErrorCode()));
25
Download code samples for
1// Create the converter
2pConverter = Img2PdfCreateObject();
3
4// Create output file
5if (!Img2PdfCreate(pConverter, szOutPath, _T(""), _T(""), ePermNoEncryption))
6{
7    _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
8    iReturnValue = 1;
9    goto cleanup;
10}
11
12// Set info dictionary
13Img2PdfSetInfoEntry(pConverter, _T("Title"), _T("Your Title"));
14Img2PdfSetInfoEntry(pConverter, _T("Author"), _T("Your Author"));
15Img2PdfSetInfoEntry(pConverter, _T("Creator"), _T("Your Creator"));
16Img2PdfSetInfoEntry(pConverter, _T("Subject"), _T("Your Subject"));
17Img2PdfSetInfoEntry(pConverter, _T("Producer"), _T("Your Producer"));
18
19// Convert pages from input image to PDF
20if (!Img2PdfCreatePageFromImage(pConverter, szInPath))
21{
22    _tprintf(_T("Pages cannot be created from image input %s. %s (ErrorCode: 0x%08x).\n"), szInPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
23    iReturnValue = 1;
24    goto cleanup;
25}
26
27// Close output file
28if (!Img2PdfClose(pConverter))
29{
30    _tprintf(_T("Output file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szOutPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
31    iReturnValue = 1;
32    goto cleanup;
33}
34
Download code samples for

Convert Image to linearized PDF

1// Create the converter
2using (Img2Pdf converter = new Img2Pdf())
3{
4    // Set linearization
5    converter.Linearize = true;
6
7    // Create output file
8    if (!converter.Create(outPath, "", "", PDFPermission.ePermNoEncryption))
9        throw new Exception(String.Format("Output file {0} cannot be created. {1} " + 
10            "(ErrorCode: 0x{2:x})", outPath, converter.ErrorMessage, converter.ErrorCode));
11
12    // Convert pages from input image to PDF
13    if (!converter.CreatePageFromImage(inPath))
14        throw new Exception(String.Format("Pages cannot be created from image input {0}. {1}" + 
15            "(ErrorCode: 0x{2:x})", inPath, converter.ErrorMessage, converter.ErrorCode));
16
17    // Close output file
18    if (!converter.Close())
19        throw new Exception(String.Format("Output file {0} cannot be closed. {1} " + 
20            "(ErrorCode: 0x{2:x})", outPath, converter.ErrorMessage, converter.ErrorCode));
21}
22
Download code samples for
1// Create the converter
2converter = new Img2Pdf();
3
4// Set linearization
5converter.setLinearize(true);
6
7// Create output file
8if (!converter.create(outPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
9    throw new Exception(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
10        outPath, converter.getErrorMessage(), converter.getErrorCode()));
11
12// Convert pages from input image to PDF 
13if (!converter.createPageFromImage(inPath))
14    throw new Exception(String.format("Pages cannot be created from image input %s. %s " + 
15        "(ErrorCode: 0x%08x).", inPath, converter.getErrorMessage(), converter.getErrorCode()));
16
17// Close output file
18if (!converter.close())
19    throw new Exception(String.format("Output file %s cannot be closed. %s (ErrorCode: 0x%08x).",
20        outPath, converter.getErrorMessage(), converter.getErrorCode()));
21
Download code samples for
1// Create the converter
2pConverter = Img2PdfCreateObject();
3
4// Set linearization
5Img2PdfSetLinearize(pConverter, true);
6
7// Create output file
8if (!Img2PdfCreate(pConverter, szOutPath, _T(""), _T(""), ePermNoEncryption))
9{
10    _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
11    iReturnValue = 1;
12    goto cleanup;
13}
14
15// Convert pages from input image to PDF
16if (!Img2PdfCreatePageFromImage(pConverter, szInPath))
17{
18    _tprintf(_T("Pages cannot be created from image input %s. %s (ErrorCode: 0x%08x).\n"), szInPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
19    iReturnValue = 1;
20    goto cleanup;
21}
22
23// Close output file
24if (!Img2PdfClose(pConverter))
25{
26    _tprintf(_T("Output file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szOutPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
27    iReturnValue = 1;
28    goto cleanup;
29}
30
Download code samples for

Convert Image to PDF with a specific page size

1// Create the converter
2using (Img2Pdf converter = new Img2Pdf())
3{
4    // Set size of output page
5    converter.SetPageSize(width, height);
6
7    // Create output file
8    if (!converter.Create(outPath, "", "", PDFPermission.ePermNoEncryption))
9        throw new Exception(String.Format("Output file {0} cannot be created. {1} " + 
10            "(ErrorCode: 0x{2:x})", outPath, converter.ErrorMessage, converter.ErrorCode));
11
12    // Convert pages from input image to PDF
13    if (!converter.CreatePageFromImage(inPath))
14        throw new Exception(String.Format("Pages cannot be created from image input {0}. " + 
15            "{1} (ErrorCode: 0x{2:x})", inPath, converter.ErrorMessage, converter.ErrorCode));
16
17    // Close output file
18    if (!converter.Close())
19        throw new Exception(String.Format("Output file {0} cannot be closed. {1} " + 
20            "(ErrorCode: 0x{2:x})", outPath, converter.ErrorMessage, converter.ErrorCode));
21}
22
Download code samples for
1// Create the converter
2converter = new Img2Pdf();
3
4// Set size of output page
5converter.setPageSize(width, height);
6
7// Create output file
8if (!converter.create(outPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
9    throw new Exception(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
10        outPath, converter.getErrorMessage(), converter.getErrorCode()));
11
12// Convert pages from input image to PDF
13if (!converter.createPageFromImage(inPath))
14    throw new Exception(String.format("Pages cannot be created from image input %s. %s " + 
15        "(ErrorCode: 0x%08x).", inPath, converter.getErrorMessage(), converter.getErrorCode()));
16
17// Close output file
18if (!converter.close())
19    throw new Exception(String.format("Output file %s cannot be closed. %s (ErrorCode: 0x%08x).",
20        outPath, converter.getErrorMessage(), converter.getErrorCode()));
21
Download code samples for
1// Create the converter
2pConverter = Img2PdfCreateObject();
3
4// Set page size
5Img2PdfSetPageSize(pConverter, fWidth, fHeight);
6
7// Create output file
8if (!Img2PdfCreate(pConverter, szOutPath, _T(""), _T(""), ePermNoEncryption))
9{
10    _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
11    iReturnValue = 1;
12    goto cleanup;
13}
14
15// Convert pages from input image to PDF
16if (!Img2PdfCreatePageFromImage(pConverter, szInPath))
17{
18    _tprintf(_T("Pages cannot be created from image input %s. %s (ErrorCode: 0x%08x).\n"), szInPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
19    iReturnValue = 1;
20    goto cleanup;
21}
22
23// Close output file
24if (!Img2PdfClose(pConverter))
25{
26    _tprintf(_T("Output file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szOutPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
27    iReturnValue = 1;
28    goto cleanup;
29}
30
Download code samples for

Document Assembly

Merge multiple images to one PDF

1// Create the converter
2using (Img2Pdf converter = new Img2Pdf())
3{
4    // Set page size
5    converter.SetPageSize(595, 842);
6
7    // Create output file
8    if (!converter.Create(outPath, "", "", PDFPermission.ePermNoEncryption))
9        throw new Exception(String.Format("Output file {0} cannot be created. {1} " + 
10            "(ErrorCode: 0x{2:x})", outPath, converter.ErrorMessage, converter.ErrorCode));
11
12    // Loop through all input files
13    for (int i = 0; i < args.Length - 1; i++)
14    {
15        // Add pages from input image
16        if (!converter.CreatePageFromImage(inPath[i]))
17            throw new Exception(String.Format("Pages cannot be created from image input {0}. {1}" +
18                " (ErrorCode: 0x{2:x})", inPath[i], converter.ErrorMessage, converter.ErrorCode));
19    }
20
21    // Close output file
22    if (!converter.Close())
23        throw new Exception(String.Format("Output file {0} cannot be closed. {1} " + 
24            "(ErrorCode: 0x{2:x})", outPath, converter.ErrorMessage, converter.ErrorCode));
25}
26
Download code samples for
1// Create the converter
2converter = new Img2Pdf();
3
4// Set page size
5converter.setPageSize(595, 842);
6
7// Create output file 
8if (!converter.create(outPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
9    throw new Exception(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
10        outPath, converter.getErrorMessage(), converter.getErrorCode()));
11
12// Loop through all input files
13for (int i = 0; i < args.length - 1; i++)
14{
15    // Add pages from input image
16    if (!converter.createPageFromImage(inPath[i]))
17        throw new Exception(String.format("Pages cannot be created from image input %s. %s " + 
18            "(ErrorCode: 0x%08x).", inPath[i], converter.getErrorMessage(),
19            converter.getErrorCode()));
20}
21
22// Close output file
23if (!converter.close())
24    throw new Exception(String.format("Output file %s cannot be closed. %s (ErrorCode: 0x%08x).",
25        outPath, converter.getErrorMessage(), converter.getErrorCode()));
26
Download code samples for
1// Create the converter
2pConverter = Img2PdfCreateObject();
3
4// Set page size
5Img2PdfSetPageSize(pConverter, 595.0, 842.0);
6
7// Create output file
8if (!Img2PdfCreate(pConverter, szOutPath, _T(""), _T(""), ePermNoEncryption))
9{
10    _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
11    iReturnValue = 1;
12    goto cleanup;
13}
14
15// Loop through all input files
16for (int i = 0; i < argc - 2; i++)
17{
18    // Convert pages from input image to PDF
19    if (!Img2PdfCreatePageFromImage(pConverter, szInPath[i]))
20    {
21        _tprintf(_T("Pages cannot be created from image input %s. %s (ErrorCode: 0x%08x).\n"), szInPath[i], Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
22        iReturnValue = 1;
23        goto cleanup;
24    }
25}
26
27// Close output file
28if (!Img2PdfClose(pConverter))
29{
30    _tprintf(_T("Output file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szOutPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
31    iReturnValue = 1;
32    goto cleanup;
33}
34
Download code samples for

Split multipage TIFF to single-page PDF

1// Create the converter
2using (Img2Pdf converter = new Img2Pdf())
3{
4    // Create codec for input
5    using (PdfCodec inCodec = new PdfCodec())
6    {
7        // Open input image
8        if (!inCodec.Open(inPath))
9            throw new Exception(String.Format("Input image {0} cannot be opened. {1} " + 
10                "(ErrorCode: 0x{2:x})", inPath, inCodec.ErrorMessage, inCodec.ErrorCode));
11
12        // Loop over all pages of image file and create each time a new PDF
13        for (int iPage = 1; iPage <= inCodec.PageCount; iPage++)
14        {
15            inCodec.PageNo = iPage;
16
17            string docPath = Path.Combine(Path.GetDirectoryName(outPath), 
18                Path.ChangeExtension(Path.GetFileNameWithoutExtension(outPath) + iPage.ToString(),
19                "pdf"));
20
21            // Create output file
22            if (!converter.Create(docPath, "", "", PDFPermission.ePermNoEncryption))
23                throw new Exception(String.Format("Output file {0} cannot be created. {1} " + 
24                    "(ErrorCode: 0x{2:x})", outPath, converter.ErrorMessage, converter.ErrorCode));
25
26            // Create pages from image codec
27            if (!converter.CreatePageFromCodec(inCodec))
28                throw new Exception(String.Format("Page {0} of the image could not be converted " + 
29                    "to PDF {1}. {2} (ErrorCode: 0x{3:x})", iPage, outPath, converter.ErrorMessage,
30                    converter.ErrorCode));
31        }
32        // Close output file
33        if (!converter.Close())
34            throw new Exception(String.Format("Output file {0} cannot be closed. {1} " + 
35                "(ErrorCode: 0x{2:x})", outPath, converter.ErrorMessage, converter.ErrorCode));
36    }
37}
38
Download code samples for
1// Create the converter
2converter = new Img2Pdf();
3
4// Create codec for input
5inCodec = new Codec();
6
7// Open input image
8if (!inCodec.open(inPath))
9    throw new Exception(String.format("Input image %s cannot be opened. %s (ErrorCode: 0x%08x).",
10        inPath, inCodec.getErrorMessage(), inCodec.getErrorCode()));
11
12// Loop over all pages of image file and create each time a new PDF
13for (int iPage = 1; iPage <= inCodec.getPageCount(); iPage++)
14{
15    inCodec.setPageNo(iPage);
16
17    File file = new File(outPath);
18    String docPath = file.getName();
19
20    docPath = docPath.substring(0, docPath.lastIndexOf(".")) + iPage + ".pdf";
21
22    // Create output file
23    if (!converter.create(docPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
24        throw new Exception(String.format("Output file %s cannot be created. %s " + 
25            "(ErrorCode: 0x%08x).", outPath, converter.getErrorMessage(),
26            converter.getErrorCode()));
27
28    // Create pages from image codec
29    if (!converter.createPageFromCodec(inCodec))
30        throw new Exception(String.format("Page %d of the image could not be converted to " + 
31            "PDF %s. %s (ErrorCode: 0x%08x).", iPage, outPath, converter.getErrorMessage(),
32            converter.getErrorCode()));
33}
34// Close output file
35if (!converter.close())
36    throw new Exception(String.format("Output file %s cannot be closed. %s (ErrorCode: 0x%08x).",
37        outPath, converter.getErrorMessage(), converter.getErrorCode()));
38
Download code samples for
1// Create the converter
2pConverter = Img2PdfCreateObject();
3
4// Create codec for input
5pInCodec = PdfCodecCreateObject();
6
7// Open input image
8if (!PdfCodecOpen(pInCodec, szInPath))
9{
10    _tprintf(_T("Input image %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInPath, PdfCodecGetErrorMessageA(pInCodec), PdfCodecGetErrorCode(pInCodec));
11    iReturnValue = 1;
12    goto cleanup;
13}
14
15// Loop over all pages of image file and create each time a new PDF
16for (int iPage = 1; iPage <= PdfCodecGetPageCount(pInCodec); iPage++)
17{
18    PdfCodecSetPageNo(pInCodec, iPage);
19
20    // Change extension
21    _tcscpy(szDocPath, szOutPath);
22    _tcstok(szDocPath, _T("."));
23    _stprintf(szBuffer, "%d", iPage);
24    _tcscat(szDocPath, szBuffer);
25    _tcscat(szDocPath, _T(".pdf"));
26
27    // Create output file
28    if (!Img2PdfCreate(pConverter, szDocPath, _T(""), _T(""), ePermNoEncryption))
29    {
30        _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
31        iReturnValue = 1;
32        goto cleanup;
33    }
34
35    // Create pages from image codec
36    if (!Img2PdfCreatePageFromCodec(pConverter, pInCodec))
37    {
38        _tprintf(_T("Page %d of the image could not be converted to PDF %s. %s (ErrorCode: 0x%08x).\n"), iPage, szOutPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
39        iReturnValue = 1;
40        goto cleanup;
41    }
42}
43// Close output file
44if (!Img2PdfClose(pConverter))
45{
46    _tprintf(_T("Output file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szOutPath, Img2PdfGetErrorMessage(pConverter), Img2PdfGetErrorCode(pConverter));
47    iReturnValue = 1;
48    goto cleanup;
49}
50
Download code samples for

In Memory

Create PDF from Image in memory

1// Create the converter
2using (Img2Pdf converter = new Img2Pdf())
3{
4    // Create PDF in memory
5    if (!converter.CreateInMemory())
6        throw new Exception(String.Format("Output file {0} cannot be created. {1} " + 
7            "(ErrorCode: 0x{2:x})", outImage, converter.ErrorMessage, converter.ErrorCode));
8
9    // Create codec for input
10    using (PdfCodec inCodec = new PdfCodec())
11    {
12        // Open image from memory
13        if (!inCodec.OpenMem(inBuffer))
14            throw new Exception(String.Format("Codec cannot be opened. {0} (ErrorCode: 0x{1:x})",
15                inCodec.ErrorMessage, inCodec.ErrorCode));
16
17        // Convert all pages
18        for (int iPage = 1; iPage <= inCodec.PageCount; iPage++)
19        {
20            inCodec.PageNo = iPage;
21
22            // Create pages from image codec
23            if (!converter.CreatePageFromCodec(inCodec))
24                throw new Exception(String.Format("Page {0} of the image could not be converted" + 
25                    " to PDF {1}. {2} (ErrorCode: 0x{3:x}) ", iPage, outImage, 
26                    converter.ErrorMessage, converter.ErrorCode));
27        }
28        // Close output file
29        if (!converter.Close())
30            throw new Exception(String.Format("Output file {0} cannot be closed. {1} " + 
31                "(ErrorCode: 0x{2:x})", outImage, converter.ErrorMessage, converter.ErrorCode));
32    }
33
34    // Get PDF in buffer
35    outBuffer = converter.GetPdf();
36    if (outBuffer == null)
37        throw new Exception(String.Format("Getting buffer of output PDF failed. {0} " + 
38            "(ErrorCode: 0x{1:x})", converter.ErrorMessage, converter.ErrorCode));
39
40    // Write bytes to output file
41    File.WriteAllBytes(outImage, outBuffer);
42}
43
Download code samples for
1// Create the converter
2converter = new Img2Pdf();
3
4// Create PDF in memory
5if (!converter.createInMemory())
6    throw new Exception(String.format("Output file %s cannot be created in memory. %s" + 
7        "(ErrorCode: 0x%08x).", outPath, converter.getErrorMessage(), converter.getErrorCode()));
8
9// Create codec for input
10inCodec = new Codec();
11
12// Open image from memory
13if (!inCodec.openMem(inBuffer))
14    throw new Exception(String.format("Input cannot be opened from memory. %s (ErrorCode: 0x%08x).",
15        inCodec.getErrorMessage(), inCodec.getErrorCode()));
16
17// Convert all pages
18for (int iPage = 1; iPage <= inCodec.getPageCount(); iPage++)
19{
20    inCodec.setPageNo(iPage);
21
22    // Create pages from image codec
23    if (!converter.createPageFromCodec(inCodec))
24        throw new Exception(String.format("Page %d of the image could not be converted to PDF " + 
25            "%s. %s (ErrorCode: 0x%08x).", iPage, outPath, converter.getErrorMessage(),
26            converter.getErrorCode()));
27}
28// Close output file
29if (!converter.close())
30    throw new Exception(String.format("Output file %s cannot be closed. %s (ErrorCode: 0x%08x).",
31        outPath, converter.getErrorMessage(), converter.getErrorCode()));
32
33// Get PDF in buffer
34outBuffer = converter.getPdf();
35if (outBuffer == null)
36    throw new Exception(String.format("Getting buffer of output PDF failed. %s " + 
37        "(ErrorCode: 0x%08x).", converter.getErrorMessage(), converter.getErrorCode()));
38
39// Write bytes to output file
40Files.write(Paths.get(outPath), outBuffer, StandardOpenOption.CREATE_NEW);
41
Download code samples for