Add Image to PDF - C++ Sample Code

Sample code to use Apryse SDK for programmatically inserting various raster image formats (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) into a PDF document. Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Learn more about our Server SDK and PDF Editing & Manipulation Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#include <PDF/PDFNet.h>
7#include <PDF/PDFDoc.h>
8#include <PDF/ElementBuilder.h>
9#include <PDF/ElementWriter.h>
10#include <PDF/ElementReader.h>
11#include <PDF/Image.h>
12#include <Filters/MappedFile.h>
13#include <Filters/FilterReader.h>
14#include <SDF/ObjSet.h>
15#include "../../LicenseKey/CPP/LicenseKey.h"
16
17#include <iostream>
18
19using namespace std;
20
21using namespace pdftron;
22using namespace Common;
23using namespace SDF;
24using namespace PDF;
25
26//-----------------------------------------------------------------------------------
27// This sample illustrates how to embed various raster image formats
28// (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
29//
30// Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to
31// be present in the system path.
32//-----------------------------------------------------------------------------------
33
34int main(int argc, char *argv[])
35{
36 int ret = 0;
37 PDFNet::Initialize(LicenseKey);
38
39 // Relative path to the folder containing test files.
40 string input_path = "../../TestFiles/";
41 string output_path = "../../TestFiles/Output/";
42
43 try
44 {
45 PDFDoc doc;
46
47 ElementBuilder f; // Used to build new Element objects
48 ElementWriter writer; // Used to write Elements to the page
49
50 Page page = doc.PageCreate(); // Start a new page
51 writer.Begin(page); // Begin writing to this page
52
53 // ----------------------------------------------------------
54 // Add JPEG image to the output file
55 PDF::Image img = PDF::Image::Create(doc, (input_path + "peppers.jpg").c_str());
56 Element element = f.CreateImage(img, 50, 500, img.GetImageWidth()/2, img.GetImageHeight()/2);
57 writer.WritePlacedElement(element);
58
59 // ----------------------------------------------------------
60 // Add a PNG image to the output file
61 img = PDF::Image::Create(doc, (input_path + "butterfly.png").c_str());
62 element = f.CreateImage(img, Matrix2D(100, 0, 0, 100, 300, 500));
63 writer.WritePlacedElement(element);
64
65 // ----------------------------------------------------------
66 // Add a GIF image to the output file
67 img = PDF::Image::Create(doc, (input_path + "pdfnet.gif").c_str());
68 element = f.CreateImage(img, Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 50, 350));
69 writer.WritePlacedElement(element);
70
71 // ----------------------------------------------------------
72 // Add a TIFF image to the output file
73
74 img = PDF::Image::Create(doc, (input_path + "grayscale.tif").c_str());
75 element = f.CreateImage(img, Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 10, 50));
76 writer.WritePlacedElement(element);
77
78 writer.End(); // Save the page
79 doc.PagePushBack(page); // Add the page to the document page sequence
80
81 // ----------------------------------------------------------
82 // Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter.
83
84 page = doc.PageCreate(PDF::Rect(0, 0, 612, 794));
85 writer.Begin(page); // begin writing to this page
86
87 // Note: encoder hints can be used to select between different compression methods.
88 // For example to instruct PDFNet to compress a monochrome image using JBIG2 compression.
89 ObjSet hint_set;
90 Obj enc=hint_set.CreateArray(); // Initialize encoder 'hint' parameter
91 enc.PushBackName("JBIG2");
92 enc.PushBackName("Lossy");
93
94 img = PDF::Image::Create(doc, (input_path + "multipage.tif").c_str(), enc);
95 element = f.CreateImage(img, Matrix2D(612, 0, 0, 794, 0, 0));
96 writer.WritePlacedElement(element);
97
98 writer.End(); // Save the page
99 doc.PagePushBack(page); // Add the page to the document page sequence
100
101 // ----------------------------------------------------------
102 // Add a JPEG2000 (JP2) image to the output file
103
104 // Create a new page
105 page = doc.PageCreate();
106 writer.Begin(page); // Begin writing to the page
107
108 // Embed the image.
109 img = Image::Create(doc, (input_path + "palm.jp2").c_str());
110
111 // Position the image on the page.
112 element = f.CreateImage(img, Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 96, 80));
113 writer.WritePlacedElement(element);
114
115 // Write 'JPEG2000 Sample' text string under the image.
116 writer.WriteElement(f.CreateTextBegin(Font::Create(doc, Font::e_times_roman), 32));
117 element = f.CreateTextRun("JPEG2000 Sample");
118 element.SetTextMatrix(1, 0, 0, 1, 190, 30);
119 writer.WriteElement(element);
120 writer.WriteElement(f.CreateTextEnd());
121
122 writer.End(); // Finish writing to the page
123 doc.PagePushBack(page);
124
125 // ----------------------------------------------------------
126
127 doc.Save((output_path + "addimage.pdf").c_str(), SDFDoc::e_linearized, 0);
128 cout << "Done. Result saved in addimage.pdf..." << endl;
129 }
130 catch(Common::Exception& e)
131 {
132 cout << e << endl;
133 ret = 1;
134 }
135 catch(...)
136 {
137 cout << "Unknown Exception" << endl;
138 ret = 1;
139 }
140
141 PDFNet::Terminate();
142 return ret;
143}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales