GdPicture.NET是一款综合性的图片和文档处理控件,不仅可以帮助开发人员对各种图片进行处理,还可以利用各种添加模块进行条码的生成与识别、图片的OCR和OMR识别,PDF文档的处理,文档的扫描和TIFF多页图片处理的,这篇文章主要介绍如何转换多页的TIFF图片为PDF文档,注意在使用该功能时要结合optional
GdPicture.NET PDF可添加模块一起使用,具体如下:
//We assume that GdPicture has been correctly installed and unlocked.
GdPictureImaging oGdPictureImaging = new GdPictureImaging();
//Making subsequently opened multipage TIFF images opened with read-only capabilities
oGdPictureImaging.TiffOpenMultiPageForWrite(false);
//Loading the Image from a file
int imageId = oGdPictureImaging.CreateGdPictureImageFromFile("C:\\Image.tif");
if (imageId != 0)
{
GdPicturePDF oGdPicturePDF = new GdPicturePDF();
oGdPicturePDF.NewPDF();
if (oGdPictureImaging.TiffIsMultiPage(imageId) == false) //One page TIFF image
{
//adding image as a resource and drawing it in a new page
oGdPicturePDF.AddImageFromGdPictureImage(imageId, false, true);
if (oGdPicturePDF.GetStat() == GdPictureStatus.OK)
{
//Save the new PDF to file
if (oGdPicturePDF.SaveToFile("C:\\output.pdf") != GdPictureStatus.OK)
{
//in case saving pdf file failed: display error
MessageBox.Show(oGdPicturePDF.GetStat().ToString());
}
}
else
{
//in case adding image to pdf failed: display error
MessageBox.Show(oGdPicturePDF.GetStat().ToString());
}
//Clear resources
oGdPicturePDF.CloseDocument();
oGdPictureImaging.ReleaseGdPictureImage(imageId);
}
else //multi page pdf image
{
int NumberOfPages = oGdPictureImaging.TiffGetPageCount(imageId);
bool savePDF = true;
//loop through pages
for (int i = 1; i <= NumberOfPages; i++)
{
//select each page in TIFF file
oGdPictureImaging.TiffSelectPage(imageId, i);
//add selected TIFF page as a resource to pdf file and draw it on a new page
oGdPicturePDF.AddImageFromGdPictureImage(imageId, false, true);
if (oGdPicturePDF.GetStat() != GdPictureStatus.OK)
{
//in case of error adding image to pdf raise: display error
MessageBox.Show(oGdPicturePDF.GetStat().ToString());
//raise flag to cancel pdf saving
savePDF = false;
break;
}
}
if (savePDF) //check whether error occured in adding any image to pdf
{
oGdPicturePDF.SaveToFile(("C:\\output.pdf"));
if (oGdPicturePDF.GetStat() != GdPictureStatus.OK)
{
MessageBox.Show(oGdPicturePDF.GetStat().ToString());
}
}
//clearing resources
oGdPicturePDF.CloseDocument();
oGdPictureImaging.ReleaseGdPictureImage(imageId);
}
}
else
{
MessageBox.Show(oGdPictureImaging.GetStat().ToString());
}