본문 바로가기
C++

[C++/WinRt] Windows Toolkit - Windows.Data.Pdf 를 이용한 PDF To Bitmap 변환 예제

by Hwoarang757 2024. 7. 29.

[C++/WinRt] Windows Toolkit - Windows.Data.Pdf 를 이용한 PDF To Bitmap 변환 예제 입니다.

 

Argument로 PDF 파일을 읽어들어 PDF 페이지별로 Bmp 파일로 Save하는 예제 입니다.

 

#include "pch.h"

#include <iostream>

using namespace winrt;
using namespace Windows::Foundation;

using namespace winrt::Windows::Storage;
using namespace Windows::Data::Pdf;

/*
*
* [0]. id
* [1]. source file fullpath 
* [2]. bmp files save path 
* 
* return value 
* 결과|PageCount|DESC
* 
*/

int wmain(int argc , wchar_t* argv[])
{
    if (argc != 4) { // 첫번째 매개변수는 실행되는 프로세스 명
        std::wcout << L"FAILED|0|매개변수가 3개가 전달 되지 않았습니다." << std::endl;
        return -1;
    }

    winrt::hstring Id;
    winrt::hstring sourcePath;
    winrt::hstring savePath;

    //std::wstring tempStr;
    //std::wstring replaceStr;
    for (int i = 0; i < argc; i++) {
        //tempStr = argv[i];
        //replaceStr = std::regex_replace(tempStr, std::wregex(L"\\\\"), L"/");

        if (i == 1) Id = winrt::hstring(argv[i]) ;
        if (i == 2) sourcePath = winrt::hstring(argv[i]);
        if (i == 3) savePath = winrt::hstring(argv[i]);
    }
    
    
    //std::wcout << Id.c_str() << "," << sourcePath.c_str() << "," << savePath.c_str() << std::endl;
    init_apartment();
    //Uri uri(L"http://aka.ms/cppwinrt");
    //printf("Hello, %ls!\n", uri.AbsoluteUri().c_str());
    
    try {
        StorageFile storagePdfFile = StorageFile::GetFileFromPathAsync(sourcePath).get();
        PdfDocument pdfDocument = PdfDocument::LoadFromFileAsync(storagePdfFile, L"").get();

        int nCount = pdfDocument.PageCount();
        int nPageIndex = 0;

        /*
        try {
            StorageFolder storageFolder = ApplicationData::Current().LocalFolder();
        }
        catch (hresult_error const& e) {
            wprintf_s(L"error 0x%08X - %ls\n", e.code() , e.message().c_str());
        }
        */

        StorageFolder storageFolder = StorageFolder::GetFolderFromPathAsync(savePath).get();

        while (nPageIndex < nCount) {
            PdfPage pdfPage = pdfDocument.GetPage(nPageIndex);

            winrt::Windows::Storage::Streams::IRandomAccessStream outputStream = winrt::Windows::Storage::Streams::InMemoryRandomAccessStream();
            pdfPage.RenderToStreamAsync(outputStream).get();

            uint64_t size64 = outputStream.Size();
            uint32_t size32 = static_cast<uint32_t>(size64);

            winrt::Windows::Storage::Streams::Buffer buffer = winrt::Windows::Storage::Streams::Buffer(size32);
            outputStream.ReadAsync(buffer, size32, winrt::Windows::Storage::Streams::InputStreamOptions::None);
            outputStream.Close();

            wchar_t szSavePath[_MAX_PATH];
            swprintf(szSavePath, L"%s_%d.bmp", Id.c_str(), nPageIndex);

            StorageFile saveFile = storageFolder.CreateFileAsync(szSavePath, CreationCollisionOption::ReplaceExisting).get();
            winrt::Windows::Storage::FileIO::WriteBufferAsync(saveFile, buffer).get();

            nPageIndex++;
        }

        std::wcout << "SUCCESS|" << nCount << "|SUCCESS" << std::endl;
    }
    catch (hresult_error const& e) {
        std::wcout << "FAIL|" << 0 << "|" << e.message().c_str() << std::endl;
    }

}