taji01
taji01

Reputation: 2615

Using Print Page Range

I’m using WinForms. In my form I have a pictureBox and (a From: textbox and a To: Textbox). These textboxes are used to print certain page ranges from a multipage Tif document. The problem is that the application doesn’t print the page ranges. Another problem is that the print preview doesn’t show the correct page, for example If I type number 2 in the From textbox I expect the print preview dialog to show page number 2 from the Tif document, it doesn't show that it shows the wrong page which is page 1.

Note: I’ve been printing to PDF for my test cases

Below is a sample Tif Document for Testing

http://www.filedropper.com/tifbordernumberpage

Printing Application

   using System.Drawing.Printing;
   using System.Drawing.Imaging;

    private int currentPrintPage;

    private void Form1_Load(object sender, EventArgs e)
    {
        //using (var dialog = new OpenFileDialog())
        //{
        //    if (dialog.ShowDialog(this) == DialogResult.OK)
        //    {
        //        string filename = dialog.FileName;
        //        pictureBox1.Load(filename);
        //    }
        //}
        pictureBox1.Load("C:\\image\\Tif_Document.tif");

    }

    private void Print_button_Click(object sender, EventArgs e)
    {
        print_preview_Settings();

       // if (printDialog1.ShowDialog() == DialogResult.OK)
       // {
       //     currentPrintPage = Convert.ToInt32(From_Pg_txtBox.Text) - 1; 
       //     printDocument1.Print();
       // }
    }

    private void print_preview_Settings()
    {

        printPreviewDialog1.Document = printDocument1;

        printDocument1.DefaultPageSettings.Margins.Top = 100;
        printDocument1.DefaultPageSettings.Margins.Left = 200;
        printDocument1.DefaultPageSettings.Margins.Right = 0;
        printDocument1.DefaultPageSettings.Margins.Bottom = 0;

        currentPrintPage = Convert.ToInt32(From_Pg_txtBox.Text) - 1;
        printPreviewDialog1.ShowDialog();
    }

    private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
        Image i;

        i = Image.FromFile("C:\\image\\Tif_Document.tif");


        i.SelectActiveFrame(FrameDimension.Page, currentPrintPage);

        //Print while maintating aspect ratio of the image
        var img_width = e.PageBounds.Width - e.MarginBounds.Left - Math.Abs(e.MarginBounds.Right - e.PageBounds.Width);
        var img_height = e.PageBounds.Height - e.MarginBounds.Top - Math.Abs(e.MarginBounds.Bottom - e.PageBounds.Height);


        var img = ResizeAcordingToImage(i, img_width, img_height);

        e.Graphics.DrawImage(i,
            e.MarginBounds.Left, e.MarginBounds.Top, img.Width, img.Height);

        currentPrintPage++; //increment page from the Tif doc

        if (currentPrintPage < Convert.ToInt32(to_Pg_txtBox.Text))
        {
            e.HasMorePages = true;

        }
        else
        {
            e.HasMorePages = false;
        }
    }

    private Image ResizeAcordingToImage(Image Source, int boxWidth, int boxHeight)
    {
        Image resizedImage;
        double dbl = (double)Source.Width / (double)Source.Height;
        //set height of image to boxHeight and check if resulting width is less than boxWidth, 
        //else set width of image to boxWidth and calculate new height
        if ((int)((double)boxHeight * dbl) <= boxWidth)
        {
            resizedImage = new Bitmap(Source, (int)((double)boxHeight * dbl), boxHeight);
        }
        else
        {
            resizedImage = new Bitmap(Source, boxWidth, (int)((double)boxWidth / dbl));
        }

        return resizedImage;
    }

Upvotes: 1

Views: 1041

Answers (1)

LarsTech
LarsTech

Reputation: 81610

For some reason, the PrintPage seems to interfere with the active frame of the TIF, so try extracting the image that needs to be displayed:

Image i = Image.FromFile("C:\\image\\Tif_Document.tif");
i.SelectActiveFrame(FrameDimension.Page, currentPrintPage);

using (MemoryStream ms = new MemoryStream()) {
  i.Save(ms, ImageFormat.Tiff);
  using (Image pageImage = Image.FromStream(ms)) {
    var img = ResizeAcordingToImage(pageImage, img_width, img_height);
    e.Graphics.DrawImage(pageImage, e.MarginBounds.Left, e.MarginBounds.Top,
                                    img.Width, img.Height);
  }
}

Upvotes: 1

Related Questions