Convert an image to a PDF document
Convert one or more TIFF, JPEG, BMP, GIF, PNG, JBIG2, or JPEG2000 images into an output PDF document. Content for long-term archiving and document accessibility can be added during the conversion process. The formatting and conformance level of the output PDF document can be adjusted to suit the requirements of the document workflow.
Download the full sample now in C# and Java.
Interested in C or other language samples? Let us know on the contact page and we'll add it to our samples backlog.
Steps to convert a document
- Reading the input image
- Selecting a conversion profile
- Adjusting the conversion profile
- Converting the image to a PDF document
- Full example
You need to initialize the library.
Reading the input image
First you need to read the image you want to convert. To do this, load the input image from the file system into a read-only image Document
.
This Document
can then later be passed to the Converter
.
- .NET
- Java
// Open image document
using var inStr = File.OpenRead(inPath);
using var inDoc = Document.Open(inStr);
// Open input document
FileStream inStr = new FileStream(inPath, FileStream.Mode.READ_ONLY);
com.pdftools.image.Document inDoc = com.pdftools.image.Document.open(inStr);
Selecting a conversion profile
You start the image conversion process by creating a conversion Profile
object to determine the conversion profile.
The Profile
object defines the parameters that are applied to the conversion process.
For more information about image to PDF conversion profiles, see Conversion profiles.
This example uses the Default
profile, which is designed for document workflows that need to convert images to PDF documents, but do not have long-term archiving or accessibility requirements.
- .NET
- Java
// Create the profile that defines the conversion parameters.
// The Default profile converts images to PDF documents.
var profile = new Profiles.Default();
// Create the profile that defines the conversion parameters.
// The Default profile converts images to PDF documents.
Default profile = new Default();
Adjusting the conversion profile
If the images must be added to the PDF document in a specific way (for example, if the image size should be preserved or a specific page orientation should be used), this can be achieved by changing the properties of the conversion profile. The PDF conformance level may also be adjusted in this way.
For more information about image to PDF conversion profiles, see Conversion profiles.
Converting the image to a PDF document
After creating the Profile
object and adjusting for any workflow-specific properties, the final step is to instantiate a Converter
object and call its Convert
method.
The output PDF document is created at the specified path.
- .NET
- Java
// Create output stream
using var outStr = File.Create(outPath);
// Convert the image to a PDF document
using var outDoc = new Converter().Convert(inDoc, outStr, profile);
// Create output stream
FileStream outStream = new FileStream(outPath, FileStream.Mode.READ_WRITE_NEW);
// Convert the image to a PDF document
Document outDoc = new Converter().convert(inDoc, outStream, profile));
Full example
- .NET
- Java
// Open image document
using var inStr = File.OpenRead(inPath);
using var inDoc = Document.Open(inStr);
// Create the profile that defines the conversion parameters.
// The Default profile converts images to PDF documents.
var profile = new Profiles.Default();
// Optionally, the profile's parameters can be changed according to the
// requirements of your conversion process.
// Create output stream
using var outStr = File.Create(outPath);
// Convert the image to a PDF document
using var outDoc = new Converter().Convert(inDoc, outStr, profile);
// Create the profile that defines the conversion parameters.
// The Default profile converts images to PDF documents.
Default profile = new Default();
// Optionally, the profile's parameters can be changed according to the
// requirements of your conversion process.
try (
// Open input document
FileStream inStr = new FileStream(inPath, FileStream.Mode.READ_ONLY);
com.pdftools.image.Document inDoc = com.pdftools.image.Document.open(inStr);
// Create output stream
FileStream outStream = new FileStream(outPath, FileStream.Mode.READ_WRITE_NEW);
// Convert the image to a PDF document
com.pdftools.pdf.Document outDoc = new Converter().convert(inDoc, outStream, profile))
{
}