Bonjour,

je voudrais imprimer en recto-verso, pour cela j'utilise :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <windows.h>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
#include <fstream>
 
LPDEVMODE GetLandscapeDevMode(HANDLE hPrinter, LPTSTR pDevice)
{
    LPDEVMODE pDevMode;
    DWORD dwNeeded, dwRet;
 
    /*
    * Step 1:
    * Allocate a buffer of the correct size.
    */ 
    dwNeeded = DocumentProperties(NULL,
    hPrinter, /* Handle to our printer. */ 
    pDevice, /* Name of the printer. */ 
    NULL, /* Asking for size, so */ 
    NULL, /* these are not used. */ 
    0); /* Zero returns buffer size. */ 
    pDevMode = (LPDEVMODE)malloc(dwNeeded);
 
    /*
    * Step 2:
    * Get the default DevMode for the printer and
    * modify it for your needs.
    */ 
    dwRet = DocumentProperties(NULL,
    hPrinter,
    pDevice,
    pDevMode, /* The address of the buffer to fill. */ 
    NULL, /* Not using the input buffer. */ 
    DM_OUT_BUFFER); /* Have the output buffer filled. */ 
    if (dwRet != IDOK)
    {
        /* If failure, cleanup and return failure. */ 
        free(pDevMode);
        ClosePrinter(hPrinter);
        return NULL;
    }
 
    /*
    * Make changes to the DevMode which are supported.
    */ 
    if (pDevMode->dmFields & DM_ORIENTATION)
    {
        /* If the printer supports paper orientation, set it.*/ 
        pDevMode->dmOrientation = DMORIENT_LANDSCAPE;
    }
 
    if (pDevMode->dmFields & DM_DUPLEX)
    {
        /* If it supports duplex printing, use it. */ 
        pDevMode->dmDuplex = DMDUP_HORIZONTAL;
        std::wcerr << L"supports duplex printing" << std::endl;
    }
 
    /*
    * Step 3:
    * Merge the new settings with the old.
    * This gives the driver an opportunity to update any private
    * portions of the DevMode structure.
    */ 
    dwRet = DocumentProperties(NULL,
    hPrinter,
    pDevice,
    pDevMode, /* Reuse our buffer for output. */ 
    pDevMode, /* Pass the driver our changes. */ 
    DM_IN_BUFFER | /* Commands to Merge our changes and */ 
    DM_OUT_BUFFER); /* write the result. */ 
 
    /* Finished with the printer */ 
    //ClosePrinter(hPrinter);
 
    if (dwRet != IDOK)
    {
        /* If failure, cleanup and return failure. */ 
        free(pDevMode);
        return NULL;
    }
 
    /* Return the modified DevMode structure. */ 
    return pDevMode;
}
 
void PrintPDF(LPTSTR printerName, char* bytes, int numBytes) {
    HANDLE hPrinter;
    PRINTER_DEFAULTS pd = { NULL, NULL, PRINTER_ACCESS_USE };
    if (!OpenPrinter(printerName, &hPrinter, &pd)) {
        std::wcerr << L"Failed to open printer: " << printerName << std::endl;
        return;
    }
 
    LPDEVMODE pDevMode = NULL;
    pDevMode = GetLandscapeDevMode(hPrinter, printerName);
    if (!pDevMode) {
        std::cerr << "Failed to set printer settings." << std::endl;
        ClosePrinter(hPrinter);
        return;
    }
 
    std::wcout << L"SetPrinter ok\n";
 
    // Create a DOC_INFO_1 structure
    DOC_INFO_1 docInfo;
    docInfo.pDocName = (LPTSTR)L"PDF Document";
    docInfo.pOutputFile = NULL;
    docInfo.pDatatype = (LPTSTR)L"RAW";
 
    // Start a print job
    DWORD dwJob = StartDocPrinter(hPrinter, 1, (LPBYTE)&docInfo);
    if (dwJob == 0) {
        std::cerr << "Failed to start print job." << std::endl;
        ClosePrinter(hPrinter);
        return;
    }
 
    // Start a page
    if (StartPagePrinter(hPrinter) == 0) {
        std::cerr << "Failed to start page." << std::endl;
        EndDocPrinter(hPrinter);
        ClosePrinter(hPrinter);
        return;
    }
 
    // Write the PDF data to the printer
    DWORD bytesWritten;
    if (WritePrinter(hPrinter, bytes, numBytes, &bytesWritten) == 0) {
        std::cerr << "Failed to write to printer." << std::endl;
        EndPagePrinter(hPrinter);
        EndDocPrinter(hPrinter);
        ClosePrinter(hPrinter);
        return;
    }
 
    // End the page
    if (EndPagePrinter(hPrinter) == 0) {
        std::cerr << "Failed to end page." << std::endl;
    }
 
    // End the print job
    if (EndDocPrinter(hPrinter) == 0) {
        std::cerr << "Failed to end print job." << std::endl;
    }
 
    std::cerr << "Impression envoyée." << std::endl;
    // Close the printer
    ClosePrinter(hPrinter);
}
 
std::vector<char> readPDFToByteArray(const std::string& filePath) {
    // Open the file in binary mode
    std::ifstream file(filePath, std::ios::binary | std::ios::ate);
    if (!file.is_open()) {
        throw std::runtime_error("Could not open file");
    }
 
    // Get the size of the file
    std::streamsize fileSize = file.tellg();
    file.seekg(0, std::ios::beg);
 
    // Read the file contents into a byte array
    std::vector<char> buffer(fileSize);
    if (!file.read(buffer.data(), fileSize)) {
        throw std::runtime_error("Error reading file");
    }
 
    return buffer;
}
 
int main()
{
    const wchar_t* printerName = L"my printer name";
 
    std::vector<char> pdfBytes;
    try {
        std::string filePath = "C:\\path\\doc.pdf";
        pdfBytes = readPDFToByteArray(filePath);
        std::cout << "PDF file read successfully. Size: " << pdfBytes.size() << " bytes" << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }
 
    PrintPDF((LPTSTR)printerName, pdfBytes.data(), (int)pdfBytes.size());
 
    return 0;
}
mais mon imprimante HP imprime page par page
ce qui est étrange, StartDocPrinter() n'utilise même pas les paramètres par défaut que j'ai modifié dans Windows (recto-verso bord long).

Merci