IdentifiantMot de passe
Loading...
Mot de passe oubli� ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les r�ponses en temps r�el, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++/CLI Discussion :

DocumentProperties() ne modifie pas les param�tres d'impression de StartDocPrinter()


Sujet :

C++/CLI

  1. #1
    Membre tr�s actif
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    427
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 427
    Par d�faut DocumentProperties() ne modifie pas les param�tres d'impression de StartDocPrinter()
    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

  2. #2
    Membre Expert
    Femme Profil pro
    ..
    Inscrit en
    D�cembre 2019
    Messages
    679
    D�tails du profil
    Informations personnelles :
    Sexe : Femme
    �ge : 95
    Localisation : Autre

    Informations professionnelles :
    Activit� : ..

    Informations forums :
    Inscription : D�cembre 2019
    Messages : 679
    Par d�faut
    Salut,

    Citation Envoy� par pol2095 Voir le message
    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).
    Le forum programmation syst�me -> windows est probablement plus adapt� pour ce genre de question.
    Cela dit, je suspecte le driver de ne pas traiter le document brut (RAW), autrement dit, ce dernier est suppos� contenir les param�tres d'impressions. Cette Hypoth�se est � v�rifier avec un document pr�mprim� dans un fichier, ou encore un document texte (TEXT).

  3. #3
    Membre tr�s actif
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    427
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 427
    Par d�faut
    Je pense aussi que le probl�me vient du driver de mon imprimante HP qui pourtant est r�cente et qui ne semble pas traiter le document RAW, existe-il un autre moyen en c++ d'imprimer un pdf sous Windows ?
    Qu'est-ce qu'un document pr�imprim� dans un fichier ? comment le cr�er ? merci

  4. #4
    Membre Expert
    Femme Profil pro
    ..
    Inscrit en
    D�cembre 2019
    Messages
    679
    D�tails du profil
    Informations personnelles :
    Sexe : Femme
    �ge : 95
    Localisation : Autre

    Informations professionnelles :
    Activit� : ..

    Informations forums :
    Inscription : D�cembre 2019
    Messages : 679
    Par d�faut
    Quand tu imprimes un document, �a ouvre une bo�te de dialogue o� tu choisis l'imprimante..Parmi les options il y a une case � cocher "imprimer dans un fichier". Elle permet d'enregistrer l'impression dans un fichier au lieu de la transmettre � l'imprimante (via le gestionnaire d'impression, spouleur).

    Ce fichier (dans son �tat brut) peut directement �tre transmis � l'imprimante qui �tait s�lectionn�e. Ce sera ton document RAW, document que le driver n'est pas cens� traiter (c'�tait le sens de mon pr�c�dent message).

  5. #5
    Membre tr�s actif
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    427
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 427
    Par d�faut
    Alors en utilisant ce document RAW, il n'y a pas moyen de transmettre des param�tres d'impression ?
    Existe-il un moyen d'imprimer en IPP sous windows en C++ ?

  6. #6
    Membre tr�s actif
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    427
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 427
    Par d�faut
    Finalement, j'appelle une DLL en C# qui utilise PdfiumViewer qui fonctionne correctement, je ne vois pas trop l'int�r�t de StartDocPrinter() en mode RAW en C++ si on ne peut pas modifier les param�tres d'impression...

+ R�pondre � la discussion
Cette discussion est r�solue.

Discussions similaires

  1. R�ponses: 0
    Dernier message: 24/07/2019, 23h22
  2. foreach ne modifie pas les valeurs d'un tableau
    Par tintin72 dans le forum Langage
    R�ponses: 11
    Dernier message: 07/05/2012, 09h15
  3. R�ponses: 0
    Dernier message: 19/02/2011, 01h30
  4. requ�te qui ne modifie pas les tables
    Par bellus33cg dans le forum S�curit�
    R�ponses: 2
    Dernier message: 24/12/2007, 11h39
  5. R�ponses: 2
    Dernier message: 01/06/2007, 11h40

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo