Quantcast
Channel: プログラミング
Viewing all articles
Browse latest Browse all 11745

プリンタ出力 - sannpurazu’s diary

$
0
0

#include
#include

//#include <windows.h>
//#include
//
//void PrintToPrinter(const char* text) {
//    // プリンタに出力するための構造体を初期化します。
//    DOCINFO docInfo;
//    ZeroMemory(&docInfo, sizeof(DOCINFO));
//    docInfo.cbSize = sizeof(DOCINFO);
//    docInfo.lpszDocName = "Test Document";
//
//    // デフォルトプリンタのデバイスコンテキストを取得します。
//    HDC hdcPrinter = CreateDC(NULL, "WINSPOOL", NULL, NULL);
//    if (hdcPrinter == NULL) {
//        std::cerr << "Failed to get printer device context." << std::endl;
//        return;
//    }
//
//    // 印刷ジョブを開始します。
//    if (StartDoc(hdcPrinter, &docInfo) <= 0) {
//        std::cerr << "Failed to start document." << std::endl;
//        DeleteDC(hdcPrinter);
//        return;
//    }
//
//    // ページを開始します。
//    if (StartPage(hdcPrinter) <= 0) {
//        std::cerr << "Failed to start page." << std::endl;
//        EndDoc(hdcPrinter);
//        DeleteDC(hdcPrinter);
//        return;
//    }
//
//    // テキストをプリンタに出力します。
//    TextOut(hdcPrinter, 100, 100, text, strlen(text));
//
//    // ページを終了します。
//    if (EndPage(hdcPrinter) <= 0) {
//        std::cerr << "Failed to end page." << std::endl;
//        EndDoc(hdcPrinter);
//        DeleteDC(hdcPrinter);
//        return;
//    }
//
//    // 印刷ジョブを終了します。
//    if (EndDoc(hdcPrinter) <= 0) {
//        std::cerr << "Failed to end document." << std::endl;
//        DeleteDC(hdcPrinter);
//        return;
//    }
//
//    // デバイスコンテキストを削除します。
//    DeleteDC(hdcPrinter);
//}
//
//int main() {
//    const char* text = "Hello, Printer!";
//    PrintToPrinter(text);
//    return 0;
//}

///////////////////////////////////////////////////////

//void PrintToFile(const char* filePath) {
//
//    // ファイルを開く(書き込みモード)
//    FILE* file;
//    fopen_s(&file,filePath, "w");
//
//    if (!file) {
//        std::cerr << "Failed to open file." << std::endl;
//        return;
//    }
//
//    // 10行分のデータをファイルに出力します。
//    for (int i = 1; i <= 10; ++i) {
//        fprintf(file, "This is line number %d\n", i);
//    }
//
//    // ファイルを閉じる
//    fclose(file);
//}
//
//int main() {
//    const char* filePath = "C:\\Users\\sannp\\Desktop\\output1.txt";  // 出力先のファイルパスを指定します。
//    PrintToFile(filePath);
//    return 0;
//}

/////////////////////////////////////////////////////

#include <windows.h>
#include

void PrintToPDF(const char* outputPath) {
    DOCINFO di = { 0 };
    di.cbSize = sizeof(DOCINFO);
    di.lpszDocName = "Test Document";
    di.lpszOutput = outputPath;

    // "Microsoft Print to PDF" プリンタのデバイスコンテキストを取得します。
    HDC hdcPrinter = CreateDC(NULL, "Microsoft Print to PDF", NULL, NULL);
    if (hdcPrinter == NULL) {
        std::cerr << "Failed to get printer device context." << std::endl;
        return;
    }

    // 印刷ジョブを開始します。
    if (StartDoc(hdcPrinter, &di) <= 0) {
        std::cerr << "Failed to start document." << std::endl;
        DeleteDC(hdcPrinter);
        return;
    }

    // ページを開始します。
    if (StartPage(hdcPrinter) <= 0) {
        std::cerr << "Failed to start page." << std::endl;
        EndDoc(hdcPrinter);
        DeleteDC(hdcPrinter);
        return;
    }

    // 10行分のテキストを出力します。
    for (int i = 1; i <= 10; ++i) {
        std::string line = "This is line number 1";
        TextOut(hdcPrinter, 100, 100 + (i * 20), line.c_str(), line.length());
    }

    // ページを終了します。
    if (EndPage(hdcPrinter) <= 0) {
        std::cerr << "Failed to end page." << std::endl;
        EndDoc(hdcPrinter);
        DeleteDC(hdcPrinter);
        return;
    }

    // 印刷ジョブを終了します。
    if (EndDoc(hdcPrinter) <= 0) {
        std::cerr << "Failed to end document." << std::endl;
        DeleteDC(hdcPrinter);
        return;
    }

    // デバイスコンテキストを削除します。
    DeleteDC(hdcPrinter);
}

int main() {
    const char* outputPath = "C:\\Users\\sannp\\Desktop\\output.pdf";  // 出力先のファイルパスを指定します。
    PrintToPDF(outputPath);
    return 0;
}

 


Viewing all articles
Browse latest Browse all 11745

Trending Articles