Print Page | Close Window

Convert a TIFF image to a PDF -> Size Increased

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: General Discussion
Forum Description: Discussion board for Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=3860
Printed Date: 25 Apr 24 at 3:10AM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: Convert a TIFF image to a PDF -> Size Increased
Posted By: vinoth.prog
Subject: Convert a TIFF image to a PDF -> Size Increased
Date Posted: 02 Nov 20 at 9:33PM
Hi Team, Please see the code below.  When I try to convert tif image to pdf. it is creating pdf with increased Size. ie. 99 KB tif to 2221  KB. I have also tried compress method , but no use it is not compressing.

#region TIFF
            if (imageLocation.Split('.').Last().ToUpper() == "TIF" || imageLocation.Split('.').Last().ToUpper() == "TIFF")
            {
                
                if (isNewDoc)
                {
                    int MainDocInt = PDFLib.SelectedDocument();
                    double startHeight = PDFLib.PageHeight();
                    bool firstpage = true;
                    Image imageIn = Image.FromFile(imageLocation);
                    Guid objGuid = imageIn.FrameDimensionsList[0];
                    var objDimension = new FrameDimension(objGuid);

                    int pageCount = imageIn.GetFrameCount(FrameDimension.Page);
                    for (int i = 0; i < pageCount; i++)
                    {
                        

                        if (!firstpage)
                        {
                            int newDoc = PDFLib.NewDocument();
                            PDFLib.SelectDocument(newDoc);
                        }
                        if (String.IsNullOrEmpty(PageSize) || PageSize.ToUpper() == "DEFAULT")
                        {
                            imageIn.SelectActiveFrame(objDimension, i);
                            int ImageWidth = imageIn.Size.Width;
                            int ImageHeight = imageIn.Size.Height;

                            PDFLib.SetPageDimensions(ImageWidth, ImageHeight);

                            int imageInt = PDFLib.AddImageFromFile(imageLocation, i+1);
                            PDFLib.SelectImage(imageInt);
                            PDFLib.CompressImages(1);
                            PDFLib.DrawImage(0, PDFLib.PageHeight(), ImageWidth, ImageHeight);
                            PDFLib.CompressImages(1);
                        }
                        else
                        {
                            imageIn.SelectActiveFrame(objDimension, i);
                            PDFLib.SetPageSize(PageSize);
                            float ImageWidth = imageIn.Size.Width;
                            float ImageHeight = imageIn.Size.Height;
                            float PageWidth = Convert.ToInt32(PDFLib.PageWidth());
                            float PageHeight = Convert.ToInt32(PDFLib.PageHeight());
                            float heightScale =  PageHeight / ImageHeight;
                            float widthScale = PageWidth / ImageWidth;

                            float scale = 0;
                            if (heightScale > widthScale)
                                scale = widthScale;
                            else
                                scale = heightScale;


                            //PDFLib.SetPageDimensions(ImageWidth, ImageHeight);

                            int imageInt = PDFLib.AddImageFromFile(imageLocation, i+1);
                            PDFLib.SelectImage(imageInt);
                           
                            if (scale == 0)
                                PDFLib.DrawImage(0, PDFLib.PageHeight(), ImageWidth, ImageHeight);
                            else
                            {
                                PDFLib.DrawScaledImage(0, PDFLib.PageHeight(), scale);
                            }
                        }

                        if (!firstpage)
                        {
                            int thisDoc = PDFLib.SelectedDocument();
                            PDFLib.SelectDocument(MainDocInt);
                            PDFLib.MergeDocument(thisDoc);
                        }
                        if (firstpage)
                            firstpage = false;

                    }
                    isNewDoc = false;
                }
                else
                {
                    int MainDoc = PDFLib.SelectedDocument();
                    int NewDoc = PDFLib.NewDocument();
                    PDFLib.SelectDocument(NewDoc);

                    int MainDocInt = PDFLib.SelectedDocument();
                    double startHeight = PDFLib.PageHeight();
                    bool firstpage = true;
                    Image imageIn = Image.FromFile(imageLocation);
                    Guid objGuid = imageIn.FrameDimensionsList[0];
                    var objDimension = new FrameDimension(objGuid);

                    int pageCount = imageIn.GetFrameCount(FrameDimension.Page);
                    for (int i = 0; i < pageCount; i++)
                    {
                        if (!firstpage)
                        {
                            int newDoc = PDFLib.NewDocument();
                            PDFLib.SelectDocument(newDoc);
                        }
                        if (String.IsNullOrEmpty(PageSize))
                        {
                            imageIn.SelectActiveFrame(objDimension, i);
                            int ImageWidth = imageIn.Size.Width;
                            int ImageHeight = imageIn.Size.Height;

                            PDFLib.SetPageDimensions(ImageWidth, ImageHeight);

                            int imageInt = PDFLib.AddImageFromFile(imageLocation, i);
                            PDFLib.SelectImage(imageInt);
                            PDFLib.DrawImage(0, PDFLib.PageHeight(), ImageWidth, ImageHeight);
                        }
                        else
                        {
                            imageIn.SelectActiveFrame(objDimension, i);
                            PDFLib.SetPageSize(PageSize);
                            int ImageWidth = imageIn.Size.Width;
                            int ImageHeight = imageIn.Size.Height;
                            int PageWidth = Convert.ToInt32(PDFLib.PageWidth());
                            int PageHeight = Convert.ToInt32(PDFLib.PageHeight());
                            float heightScale = ImageHeight / PageHeight;
                            float widthScale = ImageWidth / PageWidth;

                            float scale = 0;
                            if (heightScale > widthScale)
                                scale = widthScale;
                            else
                                scale = heightScale;


                            //PDFLib.SetPageDimensions(ImageWidth, ImageHeight);

                            int imageInt = PDFLib.AddImageFromFile(imageLocation, 0);
                            PDFLib.SelectImage(imageInt);
                            if (scale == 0)
                                PDFLib.DrawImage(0, PDFLib.PageHeight(), ImageWidth, ImageHeight);
                            else
                            {
                                PDFLib.DrawScaledImage(0, PDFLib.PageHeight(), scale);
                            }
                        }

                        if (!firstpage)
                        {
                            int thisDoc = PDFLib.SelectedDocument();
                            PDFLib.SelectDocument(MainDocInt);
                            PDFLib.MergeDocument(thisDoc);
                        }
                        if (firstpage)
                            firstpage = false;
                    }

                    PDFLib.SelectDocument(MainDoc);
                    PDFLib.MergeDocument(NewDoc);
                }
            }
            #endregion



Replies:
Posted By: Ingo
Date Posted: 03 Nov 20 at 5:10PM
Hi Vinoth,

BTW: this here is a forum from users to users - it's not a publisher forum. Please keep this in mind.
To check your prob more deeper you should upload the tif anywhere to a free filehoster (like DropBox) and post the link here. So we can test with the tiff ourself for better advice.

Regarding CommpressImages: What's the returning value of this function? What does LastErrorCode say directly after this function?
Which version of QuickPDF you're using?

Cheers and welcome here,
Ingo



-------------
Cheers,
Ingo



Posted By: tfrost
Date Posted: 03 Nov 20 at 11:56PM
I agree with the suggestion to post the TIF image (and the PDF) so that we can look at it.  Everything depends on how the TIF file was compressed and what compression you are using for the image in the PDF.  With this information I hope the users here may be able to help in more detail.

The key thing you need to note is that you are actually not 'converting the image to PDF'.  PDF is not an image format, it is a document format in which you can embed images.  The process you are using will convert the (perhaps compressed) TIF image to a different (perhaps compressed) image format, and then you embed/draw the second image into a PDF file.  So the fact that your output file is a PDF is of much less importance than exactly how you are processing the images.  Also seeing the image would enable us to check how well this specific image will compress. 



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk