Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri Apr 06, 2018 5:17 pm

Hi,

I already have a Word document with a legal size.

When I try to send it directly to the printer, the document is always considered like a letter size; the part at the end of each page which the size is greater than a letter size is truncated.

What can I do to send the document with the correct size?

Here is my code:

Code: Select all
Document myDoc = new Document();

myDoc.LoadFromFile("myFile.doc");

         PrintDialog dialog = new PrintDialog
         {
            AllowPrintToFile = true,
            AllowCurrentPage = true,
            AllowSomePages = true,
            UseEXDialog = true
         };

         myDoc.PrintDialog = dialog;

if (dialog .ShowDialog() == DialogResult.Ok)
         myDoc.PrintDocument.Print();
Last edited by mystcreater on Mon Apr 09, 2018 12:27 pm, edited 1 time in total.

mystcreater
 
Posts: 18
Joined: Tue Sep 29, 2015 12:10 am

Mon Apr 09, 2018 2:21 am

Hello,

Thanks for your post. You need to set the pager size before printing. Please refer to the below code snippet.
Code: Select all
            Document myDoc = new Document();
            myDoc.LoadFromFile(@"test.docx");

            //set paper size to Legal
            PaperSize paperSize = new PaperSize();
            paperSize.RawKind = (int)PaperKind.Legal;
            myDoc.PrintDocument.DefaultPageSettings.PaperSize = paperSize;

            PrintDialog dialog = new PrintDialog
            {
                AllowPrintToFile = true,
                AllowCurrentPage = true,
                AllowSomePages = true,
                UseEXDialog = true
            };
            myDoc.PrintDialog = dialog;
            if (dialog.ShowDialog() == DialogResult.OK)
                myDoc.PrintDocument.Print();


Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Mon Apr 09, 2018 1:13 pm

Again, it solves my problem.

Great help! :)

Would it be better in include this automation automatically in the print method?

I mean, would it be better to get the PageSize in the section of the document is only one section exist and then assigning the PaperKind automatically like that:

paperSize = Document.Sections[0].PageSetup.PageSize
paperSize.RawKind = GetPaperKindFromPaperSize()
PrintDocument.DefaultPageSettings.PaperSize = paperSize

I know in some situations where multiple sections exist, we cannot define a paper size automatically but, I think, in the vast majority of cases, only one section exist and the process of defining the paper kind could be more simple if he would be in the print method directly. Just a suggestion... because in my case, I lost 1 day trying to figure out why the legal document of my customer was printing in letter format even if the customer is changing the format to legal in the print dialog.

In anyways, thanks again! :)

mystcreater
 
Posts: 18
Joined: Tue Sep 29, 2015 12:10 am

Tue Apr 10, 2018 8:16 am

Hello,

Thanks for your kind suggestion. I wrote a demo according to your requirement. But it doesn't include all the page sizes. I hope it's helpful to you. Please refer to the below code.

Code: Select all
           Document myDoc = new Document();
            myDoc.LoadFromFile(@"test.docx");

            //set paper size according to the page size of first section
            PaperSize paperSize = new PaperSize();
            paperSize.RawKind = getPageSizeFromSection(myDoc.Sections[0]);
            myDoc.PrintDocument.DefaultPageSettings.PaperSize = paperSize;
 
            PrintDialog dialog = new PrintDialog
            {
                AllowPrintToFile = true,
                AllowCurrentPage = true,
                AllowSomePages = true,
                UseEXDialog = true
            };
            myDoc.PrintDialog = dialog;
            if (dialog.ShowDialog() == DialogResult.OK)
                myDoc.PrintDocument.Print();
        static int getPageSizeFromSection(Section section0)
        {
            var ps = section0.PageSetup.PageSize;
            var pss = typeof(PageSize);
            var fields = pss.GetFields();

            var name = "";
            foreach (FieldInfo fi in fields)
            {
                if (ps.Equals(fi.GetValue(null)))
                {
                    name = fi.Name;
                }
            }

            switch (name)
            {
                case "A3":
                    return 8;
                case "A4":
                    return 9;

                case "A5":
                    return 11;
                case "A6":
                    return 70;
                case "B4":
                    return 12;
                case "B5":
                    return 13;


                case "EnvelopeDL":
                    return 27;
                case "Executive":
                    return 7;


                case "Ledger":
                    return 4;
                case "Legal":
                    return 5;
                case "Letter":
                    return 1;


                case "Note":
                    return 18;
                case "Quarto":
                    return 15;
                case "Statement":
                    return 6;
                case "Tabloid":
                    return 3;
            }
            return 9;
        }


Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Thu Apr 12, 2018 8:29 am

Hello,

Is the solution I provided helpful to you?
Please give us some feedback at your convenience.

Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Return to Spire.Doc