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 Jun 01, 2018 5:39 pm

Hi,

I have a Word document which is configured in Landcape mode.

When I try to print it, the document is printed in Portrait mode.

I tried to set the Landscape to true in the DefaultPageSettings without success.

The result is a document printed in the format Portrait with all what is greater than 612 in width truncated.

When I look the PageSettings just before printing the document, the printable areas is still in 612X1008 instead of 1008X612 but I don't have any idea what cause the problem.

Please, help me! :(

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

Mon Jun 04, 2018 3:12 am

Hello mystcreater,

Thank you for your post.
When setting the print orientation, you need to specify the page orientation directly because our product would judge and decide the print orientation according to that of page setting. Below is the code for your kind reference.

Code: Select all
Document doc = new Document();
doc.LoadFromFile(@"input.docx");
foreach(Section s in document.Sections)
{
   s.PageSetup.Orientation = Spire.Doc.Documents.PageOrientation.Landscape;
}
PrintDocument pdt = doc.PrintDocument;
pdt.Print();


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Mon Jun 04, 2018 1:28 pm

Hi,

No, it doesn't work.

Here is the function that I've created to print the document:

Code: Select all
      private PaperKind GetPaperKind()
      {
         SizeF pageSize = Document.Sections[0].PageSetup.PageSize;

         if (HasSameDimensions(pageSize, PageSize.Legal))
            return PaperKind.Legal;
         if (HasSameDimensions(pageSize, PageSize.Letter))
            return PaperKind.Letter;
         else
            throw new ArgumentOutOfRangeException("The paper kind required for this document is not supported.");
      }

      private bool IsPortrait()
      {
         SizeF pageSize = Document.Sections[0].PageSetup.PageSize;

         return pageSize.Height > pageSize.Width;
      }

      private bool HasSameDimensions(SizeF currentSize, SizeF searchSize)
      {
         return Math.Floor(currentSize.Height) == Math.Floor(searchSize.Height) && Math.Floor(currentSize.Width) == Math.Floor(searchSize.Width) ||
                Math.Floor(currentSize.Height) == Math.Floor(searchSize.Width) && Math.Floor(currentSize.Width) == Math.Floor(searchSize.Height);
      }

      public void Print(string printerName, int numberOfCopy = 1, int fromPage = 0, int toPage = 0)
      {
         PaperSize paperSize = new PaperSize
         {
            RawKind = (int)GetPaperKind()
         };

         bool landscape = !IsPortrait();

         PageOrientation pageOrientation = landscape ? PageOrientation.Landscape : PageOrientation.Portrait;

         foreach (Section s in Document.Sections)
         {
            s.PageSetup.Orientation = pageOrientation;
         }

         Document.PrintDocument.DefaultPageSettings.PaperSize = paperSize;

         var pageSettings = Document.PrintDocument.DefaultPageSettings;

         pageSettings.PrinterSettings.Copies = (short)numberOfCopy;
         pageSettings.PrinterSettings.PrinterName = printerName;

         if (fromPage > 0)
            pageSettings.PrinterSettings.FromPage = fromPage;

         if (toPage > 0)
            pageSettings.PrinterSettings.ToPage = toPage;

         Document.PrintDocument.PrintController = new StandardPrintController();

         Document.JPEGQuality = 100;

         Document.PrintDocument.Print();
      }


And as an attachment, the word document.

Thanks

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

Tue Jun 05, 2018 3:21 am

Hello,

Thank you for your feedback.
I have noticed that the manipulation of paper size was incorrect in your code.
Below is the modified code for the "Print" method.
Code: Select all
public void Print(string printerName, int numberOfCopy = 1, int fromPage = 0, int toPage = 0)
{
    PaperKind pk = GetPaperKind();
    PaperSize paperSize = null;
    foreach (PaperSize ps in pdt.PrinterSettings.PaperSizes)
    {
        if(ps.PaperName.Equals(pk.ToString()))
           {
                  p = ps;
                  break;
            }
    }
    bool landscape = !IsPortrait();
    PageOrientation pageOrientation = landscape ? PageOrientation.Landscape : PageOrientation.Portrait;

    foreach (Section s in Document.Sections)
    {
        s.PageSetup.Orientation = pageOrientation;
    }

    document.PrintDocument.DefaultPageSettings.PaperSize = paperSize;
    var pageSettings = Document.PrintDocument.DefaultPageSettings;

    pageSettings.PrinterSettings.Copies = (short)numberOfCopy;
    pageSettings.PrinterSettings.PrinterName = printerName;

    if (fromPage > 0)
        pageSettings.PrinterSettings.FromPage = fromPage;

    if (toPage > 0)
        pageSettings.PrinterSettings.ToPage = toPage;

    Document.PrintDocument.PrintController = new StandardPrintController();
    Document.JPEGQuality = 100;
    Document.PrintDocument.Print();
}


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Wed Jun 06, 2018 12:45 pm

In your suggestion, the variable paperSize is never assign a value.

Also, in a loop, p is assign to ps. What is p?

Have you tried your suggestion?

I'm starting to be upset about doing all these hacks just to print a simple document. :(

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

Wed Jun 06, 2018 1:12 pm

Hi,

I finally found what was the wrong variable name. p have to be changed to paperSize.

Also, I want to note that in a previous post, you gave me an answer that is not correct if I consider your current answer. I mean, I cannot just create a new papersize object with a rawkind but instead I have to find the papersize corresponding to the one of the current document.

https://www.e-iceblue.com/forum/how-to-print-a-document-in-legal-size-t7469.html

Again, it's weird that we have to do all these things just to print a document that is already defined with the Legal size.

Now, I have to create a library at my company with a Document object that wrap your Document object just to handle all these cases where the printing is not done correctly.

I would suggest you to add this code directly in the Print function of your library or a sample of code in the how to section just to help other like me. It could be a show stopper for a developer trying your library to evaluate if it's worth to buy it in case where a simple operation like printing a document in legal size have to be done with some weird hacking code.

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

Thu Jun 07, 2018 10:29 am

Hello mystcreater,

I apologize for my mistake on wrongly writing the variable "paperSize" to "p". Also sorry for the previous incorrect solution in the other post. Regarding your confusion of such hacking code and suggestion, our dev team would look into it and see if it is reasonable to implement it.
I'll keep you informed about the progress.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.Doc