Advanced Imaging - Convert DICOM to PDF - C++ Sample Code

Sample code demonstrates how to use the Apryse Advanced Imaging module for direct, high-quality conversion from DICOM to PDF. Samples provided in Python, C++, C#, Java, Node.js (JavaScript), and VB. Learn more about our Server SDK and PDF Conversion 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/Convert.h>
9#include <PDF/AdvancedImagingModule.h>
10#include <PDF/AdvancedImagingConvertOptions.h>
11#include <string>
12#include <iostream>
13#include <stdio.h>
14#include "../../LicenseKey/CPP/LicenseKey.h"
15
16using namespace pdftron;
17using namespace pdftron::SDF;
18using namespace PDF;
19using namespace std;
20
21
22//---------------------------------------------------------------------------------------
23// The following sample illustrates how to convert AdvancedImaging documents to PDF format
24//
25// The AdvancedImaging module is an optional PDFNet Add-on that can be used to convert AdvancedImaging
26// documents into PDF documents
27//
28// The Apryse SDK AdvancedImaging module can be downloaded from http://www.pdftron.com/
29//---------------------------------------------------------------------------------------
30
31UString inputPath("../../TestFiles/AdvancedImaging/");
32UString outputPath("../../TestFiles/Output/");
33
34
35int main(int argc, char *argv[])
36{
37 int ret = 0;
38
39 try
40 {
41 // The first step in every application using PDFNet is to initialize the
42 // library and set the path to common PDF resources. The library is usually
43 // initialized only once, but calling Initialize() multiple times is also fine.
44 PDFNet::Initialize(LicenseKey);
45 PDFNet::AddResourceSearchPath("../../../Lib/");
46
47 if(!AdvancedImagingModule::IsModuleAvailable())
48 {
49 cout << endl;
50 cout << "Unable to run AdvancedImagingTest: Apryse SDK AdvancedImaging module not available." << endl;
51 cout << "---------------------------------------------------------------" << endl;
52 cout << "The AdvancedImaging module is an optional add-on, available for download" << endl;
53 cout << "at http://www.pdftron.com/. If you have already downloaded this" << endl;
54 cout << "module, ensure that the SDK is able to find the required files" << endl;
55 cout << "using the PDFNet::AddResourceSearchPath() function." << endl << endl;
56 return 0;
57 }
58
59 typedef struct
60 {
61 UString inputFile, outputFile;
62 } TestFile;
63
64 UString dicom_input_file, heic_input_file, psd_input_file;
65
66 dicom_input_file = "xray.dcm";
67 heic_input_file = "jasper.heic";
68 psd_input_file = "tiger.psd";
69
70 try
71 {
72 PDFDoc pdfdoc_dicom;
73 AdvancedImagingConvertOptions opts;
74 opts.SetDefaultDPI(72);
75 Convert::FromDICOM(pdfdoc_dicom, inputPath + dicom_input_file, &opts);
76 pdfdoc_dicom.Save(outputPath + dicom_input_file + UString(".pdf"), SDF::SDFDoc::e_linearized, NULL);
77 }
78 catch (Common::Exception& e)
79 {
80 cout << "Unable to convert DICOM test file" << endl;
81 cout << e << endl;
82 ret = 1;
83 }
84 catch (...)
85 {
86 cout << "Unknown Exception" << endl;
87 ret = 1;
88 }
89
90 try
91 {
92 PDFDoc pdfdoc_heic;
93 Convert::ToPdf(pdfdoc_heic, inputPath + heic_input_file);
94 pdfdoc_heic.Save(outputPath + heic_input_file + UString(".pdf"), SDF::SDFDoc::e_linearized, NULL);
95 }
96 catch (Common::Exception& e)
97 {
98 cout << "Unable to convert the HEIC test file" << endl;
99 cout << e << endl;
100 ret = 1;
101 }
102 catch (...)
103 {
104 cout << "Unknown Exception" << endl;
105 ret = 1;
106 }
107
108 try
109 {
110 PDFDoc pdfdoc_psd;
111 Convert::ToPdf(pdfdoc_psd, inputPath + psd_input_file);
112 pdfdoc_psd.Save(outputPath + psd_input_file + UString(".pdf"), SDF::SDFDoc::e_linearized, NULL);
113 }
114 catch (Common::Exception& e)
115 {
116 cout << "Unable to convert the PSD test file" << endl;
117 cout << e << endl;
118 ret = 1;
119 }
120 catch (...)
121 {
122 cout << "Unknown Exception" << endl;
123 ret = 1;
124 }
125
126
127 PDFNet::Terminate();
128 }
129 catch (Common::Exception& e)
130 {
131 cout << e << endl;
132 ret = 1;
133 }
134 catch (...)
135 {
136 cout << "Unknown Exception" << endl;
137 ret = 1;
138 }
139
140 return ret;
141}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales