Tue Apr 13, 2021 12:29 pm
Thanks Marcia,
I had used the code you provided me the link. The only difference is instead of EncoderValue.CompressionLZW, I used CCITT 6. I will provide the details shortly.
.NET Framework 4.6.1, Windows 10, VS 2019, Regional Setting UTC -5.00 Eastern (US - CANADA)
using System;
using Spire.Pdf;
using System.Drawing;
using System.Drawing.Imaging;
namespace TestconvertPDFtoTiff
{
class Program
{
static void Main(string[] args)
{
PdfDocument pdfDoc = new PdfDocument();
pdfDoc.LoadFromFile("C:\\Test\\sample-Input.pdf");
JoinTiffImages(SaveAsImage(pdfDoc), "C:\\Test\\spireOutput\\tif-output.tif", EncoderValue.CompressionCCITT4);
}
private static Image[] SaveAsImage(PdfDocument document)
{
Image[] images = new Image[document.Pages.Count];
for (int i = 0; i < document.Pages.Count; i++)
{
//use the document.SaveAsImage() method save the pdf as image
images[i] = document.SaveAsImage(i,Spire.Pdf.Graphics.PdfImageType.Bitmap,400,400);
}
return images;
}
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");
}
public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder)
{
//use the save encoder
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;
//save the first frame
pages.Save(outFile, info, ep);
}
else
{
//save the intermediate frames
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
pages.SaveAdd(img, ep);
}
if (frame == images.Length - 1)
{
//flush and close.
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
image.Dispose();
frame++;
}
}
}
}