myro
myro

Reputation: 1196

Page cropped and rotated incorrectly when printing with Ghostscript

I have been trying to print a one-page pdf file from command line (hostscript version 9.22), or .NET console app using ghostscript.net (version 1.2.1). The file (PDF version 1.7 - Acrobat 8.x) is A3 size and I want to print it on an A4. It looks like this:ORIGINAL The switches I am using

-empty -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -dNumCopies=1 -sDEVICE=mswinpr2 -sOutputFile=%printer%MSPrintToPDF -f d:\test.pdf

(different pdf (primopdf, dopdf8, microsoft pdf printer) and real printers)

Switches, different combinations of which, I have tried:

-sPaperSize=a4 -g2490x1000 -dFIXEDMEDIA -dFIXEDRESOLUTION -r300 -dAutoRotatePages -dfitpage -dPDFFitPage -dEPSFitPage -dDEVICEWIDTHPOINTS=3500 -dDEVICEHEIGHTPOINTS=2000 -dORIENT1=false -dORIENT1=true

Without this:

-dDEVICEWIDTHPOINTS=3500 -dDEVICEHEIGHTPOINTS=2000

the result is cropped and not rotated correctly:

enter image description here

Otherwise the result is landscape, though still cropped: enter image description here

How can print A3 size PDF on an A4 fitting the page?

Thank you

Upvotes: 0

Views: 463

Answers (2)

myro
myro

Reputation: 1196

My solution, suggested by @KenS:

    string printerName = "hp";
    string inputFile = @"d:\test.pdf";
    string tempFilePath = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".tmp.pdf";
    List<string> switches = new List<string>();
    switches.Add(string.Empty);
    switches.Add("-dNOPAUSE");
    switches.Add("-dBATCH");
    switches.Add("-sDEVICE=pdfwrite");
    switches.Add("-dSAFER");
    switches.Add("-dCompatibilityLevel=1.5");
    switches.Add("-dColorConversionStrategy=/LeaveColorUnchanged");
    switches.Add("-dSubsetFonts=true");
    switches.Add("-dEmbedAllFonts=true");
    switches.Add("-sDEFAULTPAPERSIZE=a4");
    switches.Add("-sPAPERSIZE=a4");
    switches.Add("-dAutoRotatePages=/PageByPage");
    switches.Add("-dFIXEDMEDIA");
    switches.Add("-dPDFFitPage");
    switches.Add($"-sOutputFile={tempFilePath}");
    switches.Add("-c");
    switches.Add("-f");
    switches.Add(inputFile);

    processor.StartProcessing(switches.ToArray(), null);
    switches = new List<string>();
    switches.Add("-dPrinted");
    switches.Add("-dBATCH");
    switches.Add("-dNOPAUSE");
    switches.Add("-dNOSAFER");
    switches.Add("-dNumCopies=1");
    switches.Add("-sDEVICE=mswinpr2");
    switches.Add("-sOutputFile=%printer%" + printerName);
    switches.Add("-sDEFAULTPAPERSIZE=a4");
    switches.Add("-sPAPERSIZE=a4");
    switches.Add("-dFIXEDMEDIA");
    switches.Add("-dPDFFitPage");
    switches.Add("-q");
    switches.Add("-f");
    switches.Add(tempFilePath);
    processor.StartProcessing(switches.ToArray(), null);

Upvotes: 0

KenS
KenS

Reputation: 31139

Most of the controls you are setting have no effect on a rendering device, or no effect on the mswinpr2 device.

The mswinpr2 device is controlled mainly by Windows. What happens is that the media size and resolution are determined by querying the default configuration of the printer, unless you allow the print dialog to pop up, in which case the configuration you enter there will be the one used.

Tha creates a Windows Printer Device Context. The input file is then rendered to a bitmap, and that bitmap is drawn to the device context. Finally the device context is told to print itself.

The upshot of all this is that, basically, waht you are trying to do simply won't work. The controls you are using are overridden by the configuration of the printer, and so no scaling takes place.

I wouldn't normally suggest this, but your best solution in this case will be to run the original file through the ps2write or pdfwrite device (or in the forthcoming 9.23 release one of the pdfimage devices). Get the size, orientation and scaling correct in that output file (you'll probably find PDF easiest to check. Then take that modified PDF file and print that through the mswinpr2 device.

Upvotes: 1

Related Questions