Advertisement
Combreal

pdfMerge.cpp

Sep 29th, 2022 (edited)
1,171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.05 KB | Source Code | 0 0
  1. //Built statically with cmake under VS Community 2019
  2. //https://github.com/kiyolee/zlib-win-build
  3. //http://download.savannah.gnu.org/releases/freetype/freetype-2.10.2.tar.gz
  4. //
  5. //https://sourceforge.net/projects/podofo/files/latest/download
  6. //cmake .. -DZLIB_INCLUDE_DIR=C:\Temp\podofo-static\build\zlib -DZLIB_LIBRARY_RELEASE=C:\Temp\zlib-win-build-main\zlib-win-build-main\build-VS2019\Release\libz.lib -A Win32 -DPODOFO_BUILD_STATIC:BOOL=TRUE -DCMAKE_BUILD_TYPE=Release -DFREETYPE_INCLUDE_DIR=C:\Temp\podofo-static\build\freetype\include -DFREETYPE_LIBRARY_RELEASE=C:\Temp\podofo-static\build\freetype\objs\Win32\Release Static\freetype.lib -DPODOFO_BUILD_SHARED=FALSE
  7. //
  8. //Own PDF Merge App VS configuration :
  9. //Include :
  10. //C:\Temp\podofo-static\src
  11. //WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)
  12. //Link:
  13. //C:\Temp\podofo-static;C:\Temp\zlib-win-build-main\zlib-win-build-main\build-VS2019\Release;C:\Temp\podofo-static\build\freetype\objs\Win32\Release Static
  14. //podofo.lib;freetype.lib;libz.lib;%(AdditionalDependencies)
  15. //Windows (/SUBSYSTEM:WINDOWS)
  16. ///libpath:C:/Temp/podofo-static/podofo.lib /libpath:C:\Temp\zlib-win-build-main\zlib-win-build-main\build-VS2019\Release\libz.lib /libpath:C:\Temp\podofo-static\build\freetype\objs\freetype.lib  
  17.  
  18.  
  19.  
  20. #include <windows.h>
  21. #include <string>
  22. #pragma comment(lib, "Ws2_32.lib")
  23. #include <podofo/podofo.h>
  24. #include "resource.h"
  25. using namespace std;
  26.  
  27. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  28. HANDLE hwndButton;
  29.  
  30. void CenterWindow(HWND);
  31. LPWSTR stringToLPWSTR(const string& instr);
  32. void mergePDF(vector<char*> inputfiles, char* outputfile);
  33. vector<string> split(string s, string delimiter);
  34.  
  35. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
  36. {
  37.     MSG msg;
  38.     WNDCLASSW wc = { 0 };
  39.     wc.lpszClassName = L"MPDF";
  40.     wc.hInstance = hInstance;
  41.     wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  42.     wc.lpfnWndProc = WndProc;
  43.     wc.hCursor = LoadCursor(0, IDC_ARROW);
  44.     wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
  45.     RegisterClassW(&wc);
  46.     CreateWindowW(wc.lpszClassName, L"MPDF", WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX | WS_VISIBLE, 260, 134, 220, 220, 0, 0, hInstance, 0);
  47.     while (GetMessage(&msg, NULL, 0, 0))
  48.     {
  49.         TranslateMessage(&msg);
  50.         DispatchMessage(&msg);
  51.     }
  52.     return (int)msg.wParam;
  53. }
  54.  
  55. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  56. {
  57.     switch (msg)
  58.     {
  59.     case WM_KEYDOWN:
  60.         if (wParam == VK_ESCAPE) {
  61.             int ret = MessageBoxW(hwnd, L"Are you sure you want to quit?",
  62.                 L"Message", MB_OKCANCEL);
  63.             if (ret == IDOK) {
  64.                 SendMessage(hwnd, WM_CLOSE, 0, 0);
  65.             }
  66.         }
  67.         break;
  68.     case WM_CREATE:
  69.         CenterWindow(hwnd);
  70.         hwndButton = CreateWindowW(L"Button", L"Click to Merge PDFs", WS_VISIBLE | WS_CHILD, 0, 0, 210, 190, hwnd, (HMENU)ID_BUTTON, NULL, NULL);
  71.         break;
  72.     case WM_COMMAND:
  73.         if (LOWORD(wParam) == ID_BUTTON)
  74.         {
  75.             string path(MAX_PATH, '\0');
  76.             OPENFILENAME ofn;
  77.             TCHAR szFile[MAX_PATH];
  78.             ZeroMemory(&ofn, sizeof(ofn));
  79.             ofn.lStructSize = sizeof(ofn);
  80.             ofn.lpstrFile = (LPWSTR)&path[0];
  81.             ofn.lpstrFile[0] = '\0';
  82.             ofn.hwndOwner = NULL;
  83.             ofn.nMaxFile = sizeof(szFile);
  84.             ofn.lpstrFilter = TEXT("Pdf Files\0*.pdf\0Any File\0*.*\0");
  85.             ofn.nFilterIndex = 1;
  86.             ofn.lpstrTitle = TEXT("Select PDF files");
  87.             ofn.lpstrInitialDir = L"";
  88.             ofn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT;
  89.             if (GetOpenFileName(&ofn))
  90.             {
  91.                 OutputDebugString(ofn.lpstrFile);
  92.                 int cSize = WideCharToMultiByte(CP_ACP, 0, ofn.lpstrFile, wcslen(ofn.lpstrFile), NULL, 0, NULL, NULL);
  93.                 string output(static_cast<size_t>(cSize), '\0');
  94.                 WideCharToMultiByte(CP_ACP, 0, ofn.lpstrFile, wcslen(ofn.lpstrFile), reinterpret_cast<char*>(&output[0]), cSize, NULL, NULL);
  95.                 string delimiter = " ";
  96.                 vector<string> v = split(output, delimiter);
  97.                 vector<char*> inputfiles;
  98.                 for (unsigned int i = 1; i < v.size();i++) {
  99.                     string output = v[0] + v[i];
  100.                     char* theFilePath = new char[100];
  101.                     strcpy(theFilePath, output.c_str());
  102.                     inputfiles.emplace_back(theFilePath);
  103.                 }
  104.                 PoDoFo::PdfError::EnableDebug(false);
  105.                 char* outputfile = new char[100];
  106.                 WCHAR path[MAX_PATH];
  107.                 GetCurrentDirectory(MAX_PATH, path);
  108.                 std::wstring fileName;
  109.                 fileName = L"\\output.pdf";
  110.                 wcscat(path, fileName.c_str());
  111.  
  112.                 sprintf(outputfile, "%ws", path);
  113.                 try {
  114.                     mergePDF(inputfiles, outputfile);
  115.                 }
  116.                 catch (const PoDoFo::PdfError& e) {
  117.                     OutputDebugStringA("Error occured during pdf merge!");
  118.                     e.PrintErrorMsg();
  119.                     return e.GetError();
  120.                 }
  121.                 OutputDebugString(ofn.lpstrFile);
  122.             }
  123.         }
  124.         break;
  125.     case WM_DESTROY:
  126.         PostQuitMessage(0);
  127.         break;
  128.     }
  129.     return DefWindowProcW(hwnd, msg, wParam, lParam);
  130. }
  131.  
  132. void CenterWindow(HWND hwnd)
  133. {
  134.     RECT rc = { 0 };
  135.     GetWindowRect(hwnd, &rc);
  136.     int win_w = rc.right - rc.left;
  137.     int win_h = rc.bottom - rc.top;
  138.     int screen_w = GetSystemMetrics(SM_CXSCREEN);
  139.     int screen_h = GetSystemMetrics(SM_CYSCREEN);
  140.     SetWindowPos(hwnd, HWND_TOP, (screen_w - win_w) / 2, (screen_h - win_h) / 2, 0, 0, SWP_NOSIZE);
  141. }
  142.  
  143. LPWSTR stringToLPWSTR(const string& instr)
  144. {
  145.     int bufferlen = ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), NULL, 0);
  146.     LPWSTR widestr = new WCHAR[bufferlen + 1];
  147.     ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), widestr, bufferlen);
  148.     widestr[bufferlen] = 0;
  149.     return widestr;
  150. }
  151.  
  152. void mergePDF(vector<char*> inputfiles, char* outputfile) {
  153.     try {
  154.         PoDoFo::PdfMemDocument firstDoc;
  155.         firstDoc.Load(inputfiles[0]);
  156.         for (int k = 1; k < inputfiles.size(); k++) {
  157.             PoDoFo::PdfMemDocument doc;
  158.             doc.Load(inputfiles[k]);
  159.             firstDoc.Append(doc);
  160.         }
  161.          firstDoc.Write(outputfile);
  162.     }
  163.     catch (const PoDoFo::PdfError& e) {
  164.         throw e;
  165.     }
  166. }
  167.  
  168. vector<string> split(string s, string delimiter) {
  169.     size_t pos_start = 0, pos_end, delim_len = delimiter.length();
  170.     string token;
  171.     vector<string> res;
  172.  
  173.     while ((pos_end = s.find(delimiter, pos_start)) != string::npos) {
  174.         token = s.substr(pos_start, pos_end - pos_start);
  175.         pos_start = pos_end + delim_len;
  176.         res.push_back(token);
  177.     }
  178.  
  179.     res.push_back(s.substr(pos_start));
  180.     return res;
  181. }
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement