Print Page | Close Window

Creating a multi page PDF from a multipage TIFF

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: Sample Code
Forum Description: Share Debenu Quick PDF Library sample code with other forum members
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=2125
Printed Date: 22 Nov 24 at 6:57PM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: Creating a multi page PDF from a multipage TIFF
Posted By: AndrewC
Subject: Creating a multi page PDF from a multipage TIFF
Date Posted: 01 Feb 12 at 9:06AM

Here is some code that correctly imports and scales pages from a multipage TIFF image and creates a multipage PDF file.

NOTE : ImageWidth and ImageHeight return the size of the image in pixels, not points, so we must calculate the correct page size by extracting the DPI from the image.

Andrew.

        private void AddImageFromFile_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "TIFF Files|*.tif";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                QP.NewDocument();

                int pages = QP.GetImagePageCount(openFileDialog1.FileName);
                if (pages == 0)
                    pages = 1;

                for (int i=1;i<=pages;i++)
                {
                    if (i != 1)
                        QP.NewPage();

                    int id = QP.AddImageFromFile(openFileDialog1.FileName, i);

                    QP.SelectImage(id);

                    int dpix = QP.ImageHorizontalResolution();
                    int dpiy = QP.ImageVerticalResolution();

                    if (dpix == 0) dpix = 72;
                    if (dpiy == 0) dpiy = 72;

                    double ImageWidthInPoints = (double)QP.ImageWidth() / dpix * 72.0; // assumming dpi units
                    double ImageHeightInPoints = (double)QP.ImageHeight() / dpiy * 72.0;

                    QP.SetPageDimensions(ImageWidthInPoints, ImageHeightInPoints);
                    QP.SetOrigin(1);
                    QP.DrawImage(0, 0, ImageWidthInPoints, ImageHeightInPoints);

                }

                QP.SaveToFile("out.pdf");

                QP.RemoveDocument(QP.SelectedDocument());
            }
        }




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