PDF Toolkit - Save Image to PDF in C#

One way to save an image file to PDF format is by loading the image file into a DIB, and then saving the DIB as a PDF file. The Document Imaging Toolkit provides several ActiveX methods which load different types of image files into a DIB. For example: In BiImgFrm.ocx: LoadGIFIntoDIB and LoadJPEGIntoDIB methods, in

BiDib.ocx: LoadDIBFromFile method, in BiTiff.ocx: LoadTiffIntoDIB method. A DIB can be saved as a PDF file by using the SaveDIBAsColorPDFPage method (in BiPdf.ocx).
The following code shows the InpExample.TIFF file (this file must be created by the user) loaded into a DIB then the DIB is saved into the

OutpExample.PDF file (c:\OutpExample.PDF) in C#. TIFF is a multipage file format, and so more than one image could be in the InpExample.TIFF file. So the conversion is in a loop. BITiff1 is an instance of BiTiff.ocx, BIPDF1 is an instance of BiPdf.ocx. The ImageCount stores the number of images in the InpExample.TIFF file.

string TIFFFile, PDFFile, ErrorString;
long ImageCount, i, hDIB;
PDFFile = “c:\\OutpExample.PDF”;
TIFFFile = “c:\\InpExample.TIFF”;
ImageCount = 0;
ImageCount = BITiff1.GetNumberOfImagesInTiffFile(TIFFFile);
if (ImageCount == 0)
{
MessageBox.Show("Error loading the TIFF file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
BIPDF1.ReleasePDF();
return;
}
if (ImageCount == -1)
{
MessageBox.Show("The specified file is not a TIFF file!", "Warning",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
BIPDF1.ReleasePDF();
return;
}
if (BIPDF1.InitPDF() == false)
return;
if (BIPDF1.CreatePDF(PDFFile) == false)
{
MessageBox.Show("Error creating the PDF file.", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
BIPDF1.ReleasePDF();
return;
}
for (i = 0; i < ImageCount; i++)
{
hDIB = BITiff1.LoadTiffIntoDIB(TIFFFile, (short)i, false);
if (hDIB == 0)
{
ErrorString = "Error loading image" + Convert.ToString(i+1) + " from the TIFF file!";
MessageBox.Show(ErrorString, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
BIPDF1.ReleasePDF();
return;
}
if (BIPDF1.SaveDIBAsColorPDFPage((int) hDIB, "TIFF32.DLL", 512, (short) -1) == false)
{
MessageBox.Show("Error saving image into the PDF file.", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
BIPDF1.FinishPDF();
BIPDF1.ReleasePDF();
return;
}
}
if(BIPDF1.FinishPDF() == false)
MessageBox.Show("Error finishing the PDF file.", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
else
MessageBox.Show("PDF file created successfully.", "Information", MessageBoxButtons.OK,
MessageBoxIcon.Information);
BIPDF1.ReleasePDF();
Page 1 Page 2 Page 3