Convert image formats to PDF in C++

Convert image files into PDF documents with Aspose.PDF for C++. These examples place images on PDF pages or load vector formats with dedicated options. They also cover SVG page size adjustment and page dimensions derived from a JPEG bitmap.

Convert BMP to PDF

Place a BMP image on a page in a new PDF document.

  1. Create an empty Document and add a page with Add().
  2. Create an Image and set its File property to the input image path.
  3. Add the image to the page’s Paragraphs collection.
  4. Save the output file with Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Generator/Image.h>
#include <Aspose.PDF.Cpp/Generator/Paragraphs.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Text;

namespace ConvertingExamples
{
//Convert BMP to PDF
void ConvertBMPtoPDF(String path_infile, String path_outfile) {
	// Open document
	auto document = MakeObject<Document>();

	// Add empty page in empty document
	auto page = document->get_Pages()->Add();
	auto image = MakeObject<Aspose::Pdf::Image>();
	image->set_File(path_infile);

	// Add image on a page
	page->get_Paragraphs()->Add(image);

	// Save output document
	document->Save(path_outfile);
}
}

Convert JPEG to PDF

Place a JPEG image on a page in a new PDF document.

  1. Create an empty Document and add a page with Add().
  2. Create an Image and set its File property to the input image path.
  3. Add the image to the page’s Paragraphs collection.
  4. Save the output file with Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Generator/Image.h>
#include <Aspose.PDF.Cpp/Generator/Paragraphs.h>

namespace ConvertingExamples
{
	using namespace System;
	using namespace Aspose::Pdf;
	using namespace Aspose::Pdf::Text;

	void ConvertJPEGtoPDF(String path_infile, String path_outfile)
	{
		// Open document
		auto document = MakeObject<Document>();

		// Add empty page in empty document
		auto page = document->get_Pages()->Add();
		auto image = MakeObject<Aspose::Pdf::Image>();
		image->set_File(path_infile);

		// Add image on a page
		page->get_Paragraphs()->Add(image);

		// Save output document
		document->Save(path_outfile);
	}
}

Set PDF page dimensions from a JPEG

Use a bitmap to obtain the JPEG dimensions and configure the PDF page size and margins.

The source adds the same image object twice; it is retained below rather than silently corrected. Review the output if you need a single image placement. Bitmap pixel dimensions are assigned directly to PDF page dimensions; the example does not calculate the page size from image DPI.

  1. Create an empty Document and add a page with Add().
  2. Open the image file and create a System::Drawing::Bitmap.
  3. Create an Image and add it to the page’s Paragraphs collection.
  4. Set the PageInfo height and width from the bitmap dimensions.
  5. Set all page margins to zero.
  6. Add the image to the paragraphs collection again, as shown in the supplied source.
  7. Save the output file with Save().
#include <system/io/file.h>
#include <drawing/bitmap.h>

#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Generator/PageInfo.h>
#include <Aspose.PDF.Cpp/Generator/MarginInfo.h>
#include <Aspose.PDF.Cpp/Generator/Image.h>
#include <Aspose.PDF.Cpp/Generator/Paragraphs.h>


namespace ConvertingExamples
{
	using namespace System;
	using namespace Aspose::Pdf;
	using namespace Aspose::Pdf::Text;

	void ConvertJPGtoPDF_fitsize(String path_infile, String path_outfile)
	{
		// Open document
		auto document = MakeObject<Document>();

		// Add empty page in empty document
		auto page = document->get_Pages()->Add();
		auto fileStream = System::IO::File::OpenRead(path_infile);
		auto bitMap = MakeObject<System::Drawing::Bitmap>(fileStream);

		auto image = MakeObject<Aspose::Pdf::Image>();
		image->set_File(path_infile);

		// Add image on a page
		page->get_Paragraphs()->Add(image);

		// Set page dimensions and margins
		page->get_PageInfo()->set_Height(bitMap->get_Height());
		page->get_PageInfo()->set_Width(bitMap->get_Width());
		page->get_PageInfo()->get_Margin()->set_Bottom(0);
		page->get_PageInfo()->get_Margin()->set_Top(0);
		page->get_PageInfo()->get_Margin()->set_Right(0);
		page->get_PageInfo()->get_Margin()->set_Left(0);
		page->get_Paragraphs()->Add(image);

		// Save output document
		document->Save(path_outfile);
	}
}

Convert PNG to PDF

Place a PNG image on a page in a new PDF document.

  1. Create an empty Document and add a page with Add().
  2. Create an Image and set its File property to the input image path.
  3. Add the image to the page’s Paragraphs collection.
  4. Save the output file with Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Generator/Image.h>
#include <Aspose.PDF.Cpp/Generator/Paragraphs.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Text;

namespace ConvertingExamples
{
void ConvertPNGtoPDF(String path_infile, String path_outfile)
{
	auto document = MakeObject<Document>();
	auto page = document->get_Pages()->Add();
	auto image = MakeObject<Aspose::Pdf::Image>();
	image->set_File(path_infile);
	page->get_Paragraphs()->Add(image);
	document->Save(path_outfile);
}
}

Convert EMF to PDF

Place an EMF image on a page in a new PDF document.

  1. Create an empty Document and add a page with Add().
  2. Create an Image and set its File property to the input image path.
  3. Add the image to the page’s Paragraphs collection.
  4. Save the output file with Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>

#include <Aspose.PDF.Cpp/Generator/Image.h>
#include <Aspose.PDF.Cpp/Generator/ImageFileType.h>
#include <Aspose.PDF.Cpp/Generator/Paragraphs.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Text;

namespace ConvertingExamples
{
void ConvertEMFtoPDF(String path_infile, String path_outfile)
{
	// Open document
	auto document = MakeObject<Document>();

	// Add empty page in empty document
	auto page = document->get_Pages()->Add();
	auto image = MakeObject<Aspose::Pdf::Image>();
	image->set_File(path_infile);

	// Add image on a page
	page->get_Paragraphs()->Add(image);

	// Save output document
	document->Save(path_outfile);
}
}

Convert DICOM to PDF

Place a DICOM image on a page in a new PDF document.

  1. Create an empty Document and add a page with Add().
  2. Create an Image and set its File property to the DICOM path.
  3. Set FileType to ImageFileType::Dicom.
  4. Add the image to the page’s Paragraphs collection.
  5. Save the output file with Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Generator/Image.h>
#include <Aspose.PDF.Cpp/Generator/ImageFileType.h>
#include <Aspose.PDF.Cpp/Generator/Paragraphs.h>

using namespace System;
using namespace Aspose::Pdf;

namespace ConvertingExamples{void ConvertDICOMtoPDF(String path_infile, String path_outfile)
{
	// Instantiate Document Object
	auto document = MakeObject<Document>();

	// Add a page to pages collection of document
	auto page = document->get_Pages()->Add();

	auto image = MakeObject<Aspose::Pdf::Image>();
	image->set_File(path_infile);
	image->set_FileType(Aspose::Pdf::ImageFileType::Dicom);

	page->get_Paragraphs()->Add(image);

	// Save output as PDF format
	document->Save(path_outfile);
}
}

Convert TIFF to PDF through a stream

Load a TIFF bitmap and pass its image data to a PDF image through a memory stream.

This example creates one PDF page and does not iterate over TIFF frames. It is not an example of exporting every frame of a multipage TIFF.

  1. Create an empty Document and add a page with Add().
  2. Open the input TIFF as a readable stream and construct a System::Drawing::Bitmap.
  3. Create an Image and a System::IO::MemoryStream.
  4. Save the bitmap to the memory stream using TIFF encoding.
  5. Set the image’s ImageStream and add it to the page’s Paragraphs collection.
  6. Save the output file with Save().
#include <system/io/file.h>
#include <system/io/memory_stream.h>
#include <drawing/bitmap.h>
#include <drawing/imaging/image_format.h>

#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>

#include <Aspose.PDF.Cpp/Generator/Image.h>
#include <Aspose.PDF.Cpp/Generator/ImageFileType.h>
#include <Aspose.PDF.Cpp/Generator/Paragraphs.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Text;

namespace ConvertingExamples
{
	void ConvertTIFFtoPDF(String path_infile, String path_outfile)
	{
		auto document = MakeObject<Document>();
		auto page = document->get_Pages()->Add();

		auto fileStream = System::IO::File::OpenRead(path_infile);
		auto pdfImage = MakeObject<Aspose::Pdf::Image>();
		auto bitmap = MakeObject<System::Drawing::Bitmap>(fileStream);
		auto imgStream = MakeObject<System::IO::MemoryStream>();
		auto imageStream = System::ExplicitCast<System::IO::Stream>(imgStream);
		bitmap->Save(imageStream, System::Drawing::Imaging::ImageFormat::get_Tiff());
		pdfImage->set_ImageStream(imageStream);
		page->get_Paragraphs()->Add(pdfImage);
		document->Save(path_outfile);
	}
}

Convert CGM to PDF

Load the CGM source with format-specific options and save it as PDF.

  1. Create CgmLoadOptions.
  2. Construct a Document with the input file path and the load options.
  3. Save the output file with Save().
#include <system/console.h>
#include <system/io/file.h>

#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>

#include <Aspose.PDF.Cpp/CgmLoadOptions.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Text;

namespace ConvertingExamples
{
void ConvertCGMtoPDF(String path_infile, String path_outfile)
{
	auto options = MakeObject<CgmLoadOptions>();
	auto document = MakeObject<Document>(path_infile, options);
	document->Save(path_outfile);
}
}

Convert SVG to PDF

Load the SVG source with format-specific options and save it as PDF.

  1. Create SvgLoadOptions.
  2. Construct a Document with the input file path and the load options.
  3. Save the output file with Save().
#include <Aspose.PDF.Cpp/Document.h>

#include <Aspose.PDF.Cpp/SvgLoadOptions.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Text;

namespace ConvertingExamples
{
void ConvertSVGtoPDF(String path_infile, String path_outfile)
{
	auto options = MakeObject<SvgLoadOptions>();
	auto document = MakeObject<Document>(path_infile, options);
	document->Save(path_outfile);
}
}

Convert SVG with page size adjustment

Enable automatic page size adjustment and select the SVG conversion engine used by the supplied example.

  1. Create SvgLoadOptions.
  2. Set AdjustPageSize to true.
  3. Set ConversionEngine to SvgLoadOptions::ConversionEngines::NewEngine.
  4. Construct a Document from the SVG path and options.
  5. Save the output file with Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/SvgLoadOptions.h>

using namespace System;
using namespace Aspose::Pdf;

namespace ConvertingExamples{void ConvertSVGtoPDF_Advanced(String path_infile, String path_outfile)
{
	auto options = MakeObject<SvgLoadOptions>();
	options->set_AdjustPageSize(true);
	options->ConversionEngine = SvgLoadOptions::ConversionEngines::NewEngine;
	auto document = MakeObject<Document>(path_infile, options);
	document->Save(path_outfile);
}
}