Bontoomester
Bontoomester

Reputation: 21

C# Print word document page by page to get orientation and create one print spool

I would like to get some help with the following:

I am trying to print a word document which has different page orientations. (Usually first is portrait and the rest of them landscape, but could be vice-versa or all pages landscape or all portraits.)

I managed to get the pages printed but I only can set the default page orientation and it would print either landscape or portrait all pages.

I can print the document page by page and set the printer orientation which works, but it creates multiple print spools. (depending how many pages the document has)

Here is the simplified code I have so far:

System.Drawing.Image img;

public void Print()
{          
    try
    {
        //open word application and document
        object missing = System.Reflection.Missing.Value;
        string file = @"filepath";
        wordApp = new Word.Application(); 
        wordApp.Visible = false; 
        doc = wordApp.Documents.Open(file, refs); 

        //go through each document pages
        foreach (Word.Window window in doc.Windows)
        {
            foreach (Word.Pane pane in window.Panes)
            {                       
                for (var i = 1; i <= pane.Pages.Count; i++)
                {
                    //convert page to bits
                    var pagebits = pane.Pages[i].EnhMetaFileBits;
                    try
                    {
                        //create memorystream
                        using (var ms = new System.IO.MemoryStream((byte[])(pagebits)))
                        {
                            //store image
                            img = System.Drawing.Image.FromStream(ms);                                   
                            //get page with and height to check if it is portrait or landscape
                            int height = pane.Pages[i].Height;
                            int width = pane.Pages[i].Width;
                            //set printer orientation depending on the height with result
                            if (height > width) { pd.DefaultPageSettings.Landscape = false; }
                            else { pd.DefaultPageSettings.Landscape = true; }
                            //send page to printer
                            pd.PrintPage += doc_PrintPage;
                            System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
                            pd.Print();
                        }
                    }
                    catch (System.Exception ex)
                    { }
                }                     
            }
        }
        //close word application and document
        doc.Close(SaveChanges: false);
        doc = null;
        wordApp.Quit(SaveChanges: false); //kill word process the right way
        wordApp = null; //reset to null
    }
    catch (Exception ex)
    {}
}

 //The Print Event handeler
 private void doc_PrintPage(object sender, PrintPageEventArgs e)
 {
     try
     {
        Rectangle m = e.PageBounds;
        e.Graphics.Clear(System.Drawing.Color.White);                
        e.Graphics.DrawImage(img, m);
        e.HasMorePages = false;
    }
     catch (Exception)
     {}
 }

I also tried to create a list and store all pages there and set e.HasMorePages = true; until it goes through the list and e.HasMorePages = false; when it gets the last page. It prints all pages to 1 spool, but then I cannot change the printing orientation by page. List<System.Drawing.Image> img = new List<System.Drawing.Image>();

Is there any way to print the document page by page to get the page orientation and set the default printer orientation the same and put them in the same spool?

Note I might also need to set the margins as the portrait printing looks ok, but the landscape printing increases the left margin. Is there any way to set the printing to "fit to page"?

Thanks for any help.

Upvotes: 0

Views: 56

Answers (0)

Related Questions