Win32 |
Author |
Message |
delovski
Joined: 14 Jun 2006 Posts: 3524 Location: Zagreb
|
Posted: Mon May 13, 2019 4:48 pm Post subject: Printing |
|
|
ms - Retrieve a Printer Device Context
"You can obtain the exact printer name string to pass to CreateDC by calling
the EnumPrinters function. The following code example shows how to call
EnumPrinters and get the names of the local and locally connected printers." |
|
Back to top |
|
 |
delovski
Joined: 14 Jun 2006 Posts: 3524 Location: Zagreb
|
Posted: Thu May 30, 2019 7:50 pm Post subject: |
|
|
How to Change Default Printer Settings in an MFC Applicat
"To change the default printer settings in an MFC application, you must
retrieve the system default settings in a CWinApp derived object and modify
those defaults before a print job is invoked." |
|
Back to top |
|
 |
delovski
Joined: 14 Jun 2006 Posts: 3524 Location: Zagreb
|
Posted: Mon Jun 03, 2019 3:49 pm Post subject: |
|
|
ms - How To Modify Printer Settings by Using SetPrinter
"SetPrinter is a new API for Windows 95, Windows NT, Windows 2000, and
Windows XP that allows applications to change various printer attributes.
However, as the code in this article demonstrates, a certain amount of
preparation is necessary in order to call SetPrinter correctly." |
|
Back to top |
|
 |
delovski
Joined: 14 Jun 2006 Posts: 3524 Location: Zagreb
|
|
Back to top |
|
 |
delovski
Joined: 14 Jun 2006 Posts: 3524 Location: Zagreb
|
Posted: Mon Jun 03, 2019 4:01 pm Post subject: |
|
|
cp - Configuring Printer Settings Programmatically
"The usage of DocumentProperties and SetPrinter API functions gives an
opportunity to change the printer settings. But, when using them, I could not
have a stable method of changing settings on all Windows OS versions. Below,
all possible methods of changing printer parameters will be described and also
the solution of how to obtain the stabler behavior for different Windows OS
versions." |
|
Back to top |
|
 |
delovski
Joined: 14 Jun 2006 Posts: 3524 Location: Zagreb
|
Posted: Mon Jun 03, 2019 4:11 pm Post subject: |
|
|
ms - SetDefaultPrinter function
"The SetDefaultPrinter() function sets the printer name of the default printer
for the current user on the local computer" |
|
Back to top |
|
 |
Ike Kapetan
Joined: 17 Jun 2006 Posts: 3263 Location: Europe
|
Posted: Mon Jun 03, 2019 4:41 pm Post subject: |
|
|
cboard - How can I set windows printer configuration in c?
"I can't get certain options set which are in the DEVMODE structure.
I set the values in the in the DEVMODE object but my Brother HL-1850 printer
completely ignores them.
I've verified the constants that are used and they are all correct.
I checked the value of the option settings and they are all correct.
I tried calling the VALID_OPTIONS option (code below) after setting all the
options but it made no difference.
It uses Windows functions: DocumentProperties() and SetPrinter()." |
|
Back to top |
|
 |
delovski
Joined: 14 Jun 2006 Posts: 3524 Location: Zagreb
|
Posted: Mon Jun 03, 2019 4:48 pm Post subject: |
|
|
so - Printing on the FILE port with a given output path still shows the 'save as' dialog
"My application uses GDI printing to create postscript files then converted to
PDF. My printer use Ghostscript PDF driver and the FILE port (By the way why
it is named "PDF driver" if it cannot output pdf directly) I specify a file output
path with the DOCINFO struct docInfo.lpszOutput = L"c:\temp_path\{guid}.ps";
Everything works fine. But sporadically the driver prompt the 'save as'
dialog, despite I provided an output filename." |
|
Back to top |
|
 |
delovski
Joined: 14 Jun 2006 Posts: 3524 Location: Zagreb
|
Posted: Mon Jun 03, 2019 4:56 pm Post subject: |
|
|
git - windows-graphics-programming-src/Chapt_17/Printer/Printer.cpp
// "Windows Graphics Programming: Win32 GDI and DirectDraw
// ISBN 0-13-086985-6 //
// //
// Written by Yuan, Feng www.fengyuan.com //
// Copyright (c) 2000 by Hewlett-Packard Company www.hp.com //
// Published by Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com //
// //
// FileName : printer.cpp //
// Description: Printer demo program, Chapter 17 //
// Version : 1.00.000, May 31, 2000 " |
|
Back to top |
|
 |
delovski
Joined: 14 Jun 2006 Posts: 3524 Location: Zagreb
|
|
Back to top |
|
 |
delovski
Joined: 14 Jun 2006 Posts: 3524 Location: Zagreb
|
|
Back to top |
|
 |
Ike Kapetan
Joined: 17 Jun 2006 Posts: 3263 Location: Europe
|
Posted: Sat Mar 13, 2021 11:24 am Post subject: |
|
|
msdn - CreateDCA function (wingdi.h)
"When you call CreateDC to create the HDC for a printer device, the printer
driver validates the DEVMODE. If the printer driver determines that the
DEVMODE is invalid (that is, printer driver can't convert or consume the
DEVMODE), the printer driver provides a default DEVMODE to create the
HDC for the printer device." |
|
Back to top |
|
 |
Ike Kapetan
Joined: 17 Jun 2006 Posts: 3263 Location: Europe
|
Posted: Wed Apr 23, 2025 9:34 am Post subject: |
|
|
ChatGPT:
"Now you want to send that to Microsoft Print to PDF and save the result to
a specific path without asking the user.
This means you need to:
Open the Microsoft Print to PDF printer.
Configure its DEVMODE to specify the output file path.
Start a GDI print job as usual."
Code: | #include <windows.h>
#include <winspool.h> // For OpenPrinter, DocumentProperties
#include <stdio.h>
BOOL PrintPDFSilently(const wchar_t* outputFilePath) {
HANDLE hPrinter = NULL;
PRINTER_DEFAULTS defaults = {0};
defaults.DesiredAccess = PRINTER_ACCESS_USE;
if (!OpenPrinterW(L"Microsoft Print to PDF", &hPrinter, &defaults)) {
wprintf(L"Failed to open printer.\n");
return FALSE;
}
DWORD dwNeeded = DocumentPropertiesW(NULL, hPrinter, L"Microsoft Print to PDF", NULL, NULL, 0);
PDEVMODEW pDevMode = (PDEVMODEW)GlobalAlloc(GPTR, dwNeeded);
if (DocumentPropertiesW(NULL, hPrinter, L"Microsoft Print to PDF", pDevMode, NULL, DM_OUT_BUFFER) != IDOK) {
wprintf(L"Failed to get printer DEVMODE.\n");
ClosePrinter(hPrinter);
return FALSE;
}
// Set output file path - this is the key!
wcscpy_s((LPWSTR)((BYTE*)pDevMode + pDevMode->dmDriverExtra), dwNeeded - sizeof(DEVMODEW), outputFilePath);
pDevMode->dmFields |= DM_ORIENTATION; // Set anything to force update
// Create DC with modified devmode
HDC hDC = CreateDCW(L"Microsoft Print to PDF", NULL, NULL, pDevMode);
if (!hDC) {
wprintf(L"Failed to create DC.\n");
ClosePrinter(hPrinter);
GlobalFree(pDevMode);
return FALSE;
}
DOCINFOW docInfo = {0};
docInfo.cbSize = sizeof(DOCINFOW);
docInfo.lpszDocName = L"My Silent PDF Print Job";
if (StartDocW(hDC, &docInfo) <= 0) {
wprintf(L"StartDoc failed.\n");
DeleteDC(hDC);
ClosePrinter(hPrinter);
GlobalFree(pDevMode);
return FALSE;
}
StartPage(hDC);
// --- DRAW HERE ---
TextOutW(hDC, 100, 100, L"Hello PDF!", 10);
EndPage(hDC);
EndDoc(hDC);
DeleteDC(hDC);
GlobalFree(pDevMode);
ClosePrinter(hPrinter);
return TRUE;
}
|
"You need to write the file path to the output inside the dmDriverExtra section
of the DEVMODE structure. This only works if your app has permission to
access that printer and path.
The memory for the DEVMODE structure must be properly allocated.
This method is undocumented behavior for Microsoft Print to PDF and may
not be 100% future-proof." |
|
Back to top |
|
 |
|