sales@e-iceblue.com    |    028-81705109    |    2181348176    |    微信扫一扫    |    EN-US   |    论坛

C# 转换 PDF 为 Tiff,转 Tiff 为 PDF

本篇文章将介绍通过 Spire.PDF for .NET 实现 PDF 文档与 .tiff 图片文档相互转换的方法,包括:

PDF转Tiff

static void Main(string[] args)
        {
            //创建PdfDocument对象,加载PDF文档
            PdfDocument document = new PdfDocument();
            document.LoadFromFile("sample.pdf");

            //保存文档为.tiff格式的文件
            JoinTiffImages(SaveAsImage(document), "result.tiff", EncoderValue.CompressionLZW);
         }
        //自定义方法SaveAsImage()
        private static Image[] SaveAsImage(PdfDocument document)
        {
            Image[] images = new Image[document.Pages.Count];
            for (int i = 0; i < document.Pages.Count; i++)
            {
                images[i] = document.SaveAsImage(i);
            }
            return images;
        }
        //自定义方法GetEncoderInfo()
        private static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
            for (int j = 0; j < encoders.Length; j++)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
        }
        //自定义方法JoinTiffImages()
        public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder)
        {
            Encoder enc = Encoder.SaveFlag;
            EncoderParameters ep = new EncoderParameters(2);
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
            ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder);
            Image pages = images[0];
            int frame = 0;
            ImageCodecInfo info = GetEncoderInfo("image/tiff");
            foreach (Image img in images)
            {
                if (frame == 0)
                {
                    pages = img;
                    pages.Save(outFile, info, ep);
                }

                else
                {
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);

                    pages.SaveAdd(img, ep);
                }
                if (frame == images.Length - 1)
                {
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
                    pages.SaveAdd(ep);
                }
                frame++;
            }
        }

转换结果:

C# 转换 PDF 为 Tiff,转 Tiff 为 PDF

Tiff转PDF

static void Main(string[] args)
        {
            //创建PdfDocument对象
            PdfDocument pdfDocument = new PdfDocument();
            //加载tiff图片
            Image tiffImage = Image.FromFile("test.tiff");

            //调用方法拆分tiff图片
            Image[] images = SplitTIFFImage(tiffImage);
             for (int i = 0; i < images.Length; i++)
              {
                PdfImage pdfImg = PdfImage.FromImage(images[i]);//获取图片
                PdfPageBase page = pdfDocument.Pages.Add();//添加PDF页面
                //指定图片在PDF中的高度、宽度及位置
                float width = pdfImg.Width * 0.5f;
                float height = pdfImg.Height * 0.5f;
                float x = (page.Canvas.ClientSize.Width - width) / 2;
                //将图片绘制到PDF页面 
                page.Canvas.DrawImage(pdfImg, x, 0, width, height);
              }
            //保存文档
            pdfDocument.SaveToFile("result.pdf");
        }
        //自定义方法SplitTIFFImage()
        public static Image[] SplitTIFFImage(Image tiffImage)
        {
            int frameCount = tiffImage.GetFrameCount(FrameDimension.Page);
            Image[] images = new Image[frameCount];
            Guid objGuid = tiffImage.FrameDimensionsList[0];
            FrameDimension objDimension = new FrameDimension(objGuid); 
            for (int i = 0; i < frameCount; i++)
            {
                tiffImage.SelectActiveFrame(objDimension, i);
                using (MemoryStream ms = new MemoryStream())
                {
                    tiffImage.Save(ms, ImageFormat.Tiff);
                    images[i] = Image.FromStream(ms);
                }
            }
            return images;
        }

转换结果:

C# 转换 PDF 为 Tiff,转 Tiff 为 PDF