Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Apr 13, 2021 2:41 am

Hi,
I have a project where I get driving license copy as a PDF. When I am converting the PDF to a tiff image with compression ccitt t.6 the image loses its quality. The photo of the person looks like a caricature and the address and license numbers are cut off. This is a client requirement. The existing application used by some old Adobe library generates the license as a grayscale image.

I have tried the greyscale sample code provided, however I was getting a memory exception. Spire.office satisfied all the other necessary conditions for our client requirement, and this functionality is important for them to purchase the product.

Thanks,
Nami

namivenus
 
Posts: 32
Joined: Tue Apr 13, 2021 2:26 am

Tue Apr 13, 2021 8:33 am

Hello,

Thanks for your inquiry!

First, I recommend that you convert your PDF Document as tiff image following this article (Save PDF Document as tiff image), and free the image memory at the bottom of the JoinTiffImages function using the following code with the latest Spire.PDF V7.3.3.
Code: Select all
            foreach (Image image in images)
            {
                image.Dispose();
            }


If the image is still in lower quality or the memory exception is still exists, to help us investigate your issue. Please provide us with your input file, target framework, system information (E.g. Win7, 64 bit) and region setting (E.g. China, Chinese) for reference. Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

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++;

}

}


}
}

namivenus
 
Posts: 32
Joined: Tue Apr 13, 2021 2:26 am

Wed Apr 14, 2021 5:44 am

Hello Nami,

Thanks for sharing more information!

I did notice that the generated tiff image is in the lower quality when using the EncoderValue.CompressionCCITT4, and I have logged it in our issue tracking system with the ticket SPIREPDF-4193 for further investigation.

We will let you know if there is any update. Sorry for the inconvenience caused.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Wed Apr 14, 2021 12:26 pm

Hi Marcia,

Thanks for checking it out. Looking forward to hear from you.

Thanks,
Nami

namivenus
 
Posts: 32
Joined: Tue Apr 13, 2021 2:26 am

Thu Apr 15, 2021 1:29 am

Hello Nami,

Once there is any progress regarding the issue SPIREPDF-4193, we will notify you immediately.

Thanks for your patience!

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Mon Apr 19, 2021 7:06 am

Hello Nami,

Thanks for your patience!

After further investigate, we found that when converting the PDF to tiff, our Spire.PDF can accurately restore all the pixels, but after the CCITT4 compression, the gray pixels are lost. This will causes the image quality to be too low.

However, according to Wikipedia, the compressed image of CCITT4 is a black and white image. So the loss of some gray pixels after compression using CCITT4 in .Net has nothing to do with our product. This is the specification of CCITT4 itself.

I recommend that you can use other compression formats to convert PDF to tiff images, such as EncoderValue.LZW or EncoderValue.Rle, to make sure that the quality of the generated images is better.

Hope you can understand.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Mon Apr 26, 2021 2:49 am

Hello,

Greetings from E-iceblue.

Has the issue been solved with other compression formats? Could you please give us some feedback at your convenience?

Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Return to Spire.PDF