GdPicture.NET如何转换多页TIFF图片为PDF文档

作者:控件中国网   出处:控件中国网   2016-09-20 10:07:06   阅读:8

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());
Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat