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.
- Create an empty Document and add a page with Add().
- Create an Image and set its File property to the input image path.
- Add the image to the page’s Paragraphs collection.
- 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.
- Create an empty Document and add a page with Add().
- Create an Image and set its File property to the input image path.
- Add the image to the page’s Paragraphs collection.
- 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.
- Create an empty Document and add a page with Add().
- Open the image file and create a System::Drawing::Bitmap.
- Create an Image and add it to the page’s Paragraphs collection.
- Set the PageInfo height and width from the bitmap dimensions.
- Set all page margins to zero.
- Add the image to the paragraphs collection again, as shown in the supplied source.
- 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.
- Create an empty Document and add a page with Add().
- Create an Image and set its File property to the input image path.
- Add the image to the page’s Paragraphs collection.
- 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.
- Create an empty Document and add a page with Add().
- Create an Image and set its File property to the input image path.
- Add the image to the page’s Paragraphs collection.
- 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.
- Create an empty Document and add a page with Add().
- Create an Image and set its File property to the DICOM path.
- Set FileType to ImageFileType::Dicom.
- Add the image to the page’s Paragraphs collection.
- 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.
- Create an empty Document and add a page with Add().
- Open the input TIFF as a readable stream and construct a System::Drawing::Bitmap.
- Create an Image and a System::IO::MemoryStream.
- Save the bitmap to the memory stream using TIFF encoding.
- Set the image’s ImageStream and add it to the page’s Paragraphs collection.
- 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.
- Create CgmLoadOptions.
- Construct a Document with the input file path and the load options.
- 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.
- Create SvgLoadOptions.
- Construct a Document with the input file path and the load options.
- 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.
- Create SvgLoadOptions.
- Set AdjustPageSize to true.
- Set ConversionEngine to SvgLoadOptions::ConversionEngines::NewEngine.
- Construct a Document from the SVG path and options.
- 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);
}
}