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.

Mon Mar 02, 2020 12:56 pm

Hello Team,

we are using your Spire.PDF product to convert PDF into images.
we are facing some issues in converting the image. our requirement is to convert the pdf into a tiff image with the below-mentioned image properties.

Horizontal resolution: 200 dpi
Vertical resolution: 200 dpi
compression: ?? output tiff image should be color
Bit depth: 24

Please suggest how can we use to convert this pdf to color tiff with above-defined properties.
I have attached the sample pdf which we are using for testing.

we are waiting for your response.

Thanks
Abhishek Kumar
Last edited by abhi805103 on Tue Mar 03, 2020 7:25 am, edited 1 time in total.

abhi805103
 
Posts: 2
Joined: Wed Oct 10, 2018 3:33 pm

Tue Mar 03, 2020 3:38 am

Hello,

Thanks for your inquiry.
Below is the code for you. If there is any question, please get it back to us.
Code: Select all
static void Main(string[] args)
{
    PdfDocument document = new PdfDocument();
    document.LoadFromFile("ColorTwoPage.pdf");
    JoinTiffImages(SaveAsImage(document), "result-ColorTwoPage.tiff", EncoderValue.CompressionLZW);
}
private static Image[] SaveAsImage(PdfDocument document)
{
    Image[] images = new Image[document.Pages.Count];
    for (int i = 0; i < document.Pages.Count; i++)
    {
        //Save as image and set resolution
        images[i] = document.SaveAsImage(i, 200, 200);             
    }
    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(3);
    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
    ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder);
    //Set bit depth
    int dep = 24;
    System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.ColorDepth;
    EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, dep);
    ep.Param[2] = myEncoderParameter;

    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);
        }
        frame++;
    }
} 

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Tue Mar 03, 2020 7:25 am

Hello Team,

The below solution works for me.

Thanks for your response

Regards,
Abhishek Kumar

abhi805103
 
Posts: 2
Joined: Wed Oct 10, 2018 3:33 pm

Tue Mar 03, 2020 7:42 am

Hello,

Thanks for your feedback.
If you encounter any question using our products in the future, just feel free to contact us.
Have a nice day!

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.PDF

cron