Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed Dec 06, 2017 12:41 pm

Hello,
I'm evaluating Spire.PDF but is having a bit of a problem printing PDF files containing A3 pages. The problem is that it always comes out as A4.

I found some code (the commented part below) that is supposed to try and pick a usable size, but the last attempt ignores the size of the PDF and just tries to print it as an A3, and fails.

But whatever I do, I only get A4 printed. I have tried it on multiple A3-capable printes both new and old.

The following code is an excerpt of what I am using in the test environment today.
Code: Select all
                Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
                doc.LoadFromStream(stream);
                doc.PrintSettings.PrinterName = printerName;

                if (pages != null && pages.Length > 0)
                    doc.PrintSettings.SelectSomePages(pages);

                //SizeF size = doc.Pages[0].Size;
                //PaperSize paper = new PaperSize("Custom", (int)size.Width, (int)size.Height);
                //paper.RawKind = (int)PaperKind.Custom;
                //doc.PrintDocument.DefaultPageSettings.PaperSize = paper;

                PaperSize paper = new PaperSize("A3", (int)PdfPageSize.A3.Width, (int)PdfPageSize.A3.Height);
                paper.RawKind = (int)PaperKind.A3;
                doc.PrintDocument.DefaultPageSettings.PaperSize = paper;

                doc.PageScaling = PdfPrintPageScaling.ActualSize;               
                     
                doc.Print();
                doc.Dispose();


There's nothing wrong with the PDF files. Printing them manually in Adobe Acrobat, or similar, works just fine.

Thankful for any help.

petsu
 
Posts: 4
Joined: Wed Dec 06, 2017 12:29 pm

Thu Dec 07, 2017 2:22 am

Hello,

Thanks for your inquiry. After an initial test with the most new version( Spire.PDF Pack(Hot Fix) Version:3.9.493), I was unable to reproduce the issue. And I found you were using the obsolete method, please use the below code and try again.
Code: Select all
            Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
            doc.LoadFromStream(stream);
            doc.PrintSettings.PrinterName = printerName;
            PaperSize paper = new PaperSize("A3", (int)PdfPageSize.A3.Width, (int)PdfPageSize.A3.Height);
            paper.RawKind = (int)PaperKind.A3;
           //doc.PrintDocument.DefaultPageSettings.PaperSize = paper;
            doc.PrintSettings.PaperSize = paper;

            //doc.PageScaling = PdfPrintPageScaling.ActualSize;
            doc.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.ActualSize);

            doc.Print();
            doc.Dispose();


Best regards,
Simon
E-iceblue support team
User avatar

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

Thu Dec 07, 2017 1:15 pm

Thanks, just switching to the non-obsolete way of setting paper and PageLayout solved the problem! :)

However, now I have another problem:

My code correctly identifies the page size (A4,A3, etc) and sets the correct format. Printing A3 on A3-printer and A4 on A4-printer works fine.

But printing an A3 paper on an A4 printer produces only upper left corner of the PDF file, and an extra page with a warning that says "PCL XL error Warning: IllegalMediaSize"

If the printer doesn't support a large paper formar I would like the printer to default to the largest paper size available (A4 on an A4-only printer for example)

Is this possible?

I tried to set different PrinterSetting PdfSinglePageScalingMode options, but none lets me print an A2 or A3 PDF scaled down to fit the A4 page on an A4-only printer.

Note that the code has not, and will not, be aware of the capabillities of the currently selected printer. This code is just told the name of the network printer.

Edit: Currently running Spire.PDF 3.9.511

petsu
 
Posts: 4
Joined: Wed Dec 06, 2017 12:29 pm

Fri Dec 08, 2017 7:34 am

Hello,

Thanks for your feedback.
After an initial test, I was unable to reproduce the issue on my side, the A3 page could fit to A4 paper without warning when using an A4 printer. To help us investigate further, would you please share us with your PDF file with A3 page?
On the other hand, what is the result like when printing in Adobe? Please have a try on your side. Below is the code I am using.
Code: Select all
            Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
            doc.LoadFromFile(@"document.pdf");
            doc.PrintSettings.PrinterName = @"\\192.168.1.104\HP LaserJet P1007";
            doc.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.FitSize);
            doc.Print();
            doc.Dispose();


Best regards,
Simon
E-iceblue support team
User avatar

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

Mon Dec 11, 2017 7:05 am

Hi,

I'm unable to share a PDF since it's propiretary CAD drawings on them.

I have been experimenting a bit with PdfSinglePageScalingMode but is unable to find a way where I can print A3 on A3 printer as A3, and the same A3 document on A4 printer as A4, without telling it that the document is an A3 when printing on an A3 printer, and not telling it the paper size when printing on A4 printer.

If I set the paper size to A3 when printing on A4 printer, I only get a corner of the drawing.

To sumarize:
Without knowing if the printer is capable of Printing A3 or bigger sizes, or using a GUI dialog, can I print documents with different sizes (A4 up to A2) and have it select and scale to the largest paper size the printer supports?
Or is this more of a "this is how printers work" kind of problem?



The print code:
Code: Select all
                Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
                doc.LoadFromStream(stream);

                SizeF size = doc.Pages[0].Size;

                PaperSize paper = GetPaperFromSize(size);

                doc.PrintSettings.PaperSize = paper;
                doc.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.FitSize);
                doc.PrintSettings.PrinterName = printerName;

                if (pages != null && pages.Length > 0)
                    doc.PrintSettings.SelectSomePages(pages);

                doc.Print();
                doc.Dispose();


The helper methods:
Code: Select all
        private PaperSize GetPaperFromSize(SizeF size)
        {
            PaperSize paper;
           
            if( SizeMatches(size, PdfPageSize.A2))
            {
                paper = new PaperSize("A2", (int)PdfPageSize.A2.Width, (int)PdfPageSize.A2.Height);
                paper.RawKind = (int)PaperKind.A2;
            }
            else if (SizeMatches(size, PdfPageSize.A3))
            {
                paper = new PaperSize("A3", (int)PdfPageSize.A3.Width, (int)PdfPageSize.A3.Height);
                paper.RawKind = (int)PaperKind.A3;
            }
            else
            {
                paper = new PaperSize("A4", (int)PdfPageSize.A4.Width, (int)PdfPageSize.A4.Height);
                paper.RawKind = (int)PaperKind.A4;
            }

            return paper;
        }

        private bool SizeMatches(SizeF pageSize, SizeF paperFormat)
        {
            if (Math.Abs(pageSize.Width - paperFormat.Width) < 1 && Math.Abs(pageSize.Height - paperFormat.Height) < 1)
                return true;

            if (Math.Abs(pageSize.Width - paperFormat.Height) < 1 && Math.Abs(pageSize.Height - paperFormat.Width) < 1)
                return true;

            return false;
        }

petsu
 
Posts: 4
Joined: Wed Dec 06, 2017 12:29 pm

Mon Dec 11, 2017 10:04 am

Hello,

Thanks for your feedback. I have confirmed that both A3 and A5 PDF can fit to A4 paper when printing on A4 printer(HP LaserJet P1007) with simply setting PdfSinglePageScalingMode.FitSize. The issue may be caused by your printer, if possible, your could have a try with other printers.
On the other hand, it will be helpful if you could send us a sample PDF that can reproduce the same issue on your side.

Code: Select all
          Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
            doc.LoadFromFile(@"document.pdf");
            doc.PrintSettings.PrinterName = @"\\192.168.1.104\HP LaserJet P1007";
            doc.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.FitSize);
            doc.Print();
            doc.Dispose();


Best regards,
Simon
E-iceblue support team
User avatar

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

Tue Dec 12, 2017 7:58 am

Hello,

Hope your are doing fine.
How is the issue now? 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

Tue Dec 12, 2017 3:48 pm

Hi,

No, the issue is still there. As you said, there could be something wrong with our small A4 printers, because the issue is reproduceable on all of them.

I am experimenting with this from time to time when I have some time over, so I don't have any new information for the moment.

petsu
 
Posts: 4
Joined: Wed Dec 06, 2017 12:29 pm

Wed Dec 13, 2017 2:35 am

Hello,

Thanks for your reply. I am looking forward to your further update.

Best regards,
Simon
E-iceblue support team
User avatar

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

Return to Spire.PDF

cron