Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Built statically with cmake under VS Community 2019
- //https://github.com/kiyolee/zlib-win-build
- //http://download.savannah.gnu.org/releases/freetype/freetype-2.10.2.tar.gz
- //
- //https://sourceforge.net/projects/podofo/files/latest/download
- //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
- //
- //Own PDF Merge App VS configuration :
- //Include :
- //C:\Temp\podofo-static\src
- //WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)
- //Link:
- //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
- //podofo.lib;freetype.lib;libz.lib;%(AdditionalDependencies)
- //Windows (/SUBSYSTEM:WINDOWS)
- ///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
- #include <windows.h>
- #include <string>
- #pragma comment(lib, "Ws2_32.lib")
- #include <podofo/podofo.h>
- #include "resource.h"
- using namespace std;
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- HANDLE hwndButton;
- void CenterWindow(HWND);
- LPWSTR stringToLPWSTR(const string& instr);
- void mergePDF(vector<char*> inputfiles, char* outputfile);
- vector<string> split(string s, string delimiter);
- int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
- {
- MSG msg;
- WNDCLASSW wc = { 0 };
- wc.lpszClassName = L"MPDF";
- wc.hInstance = hInstance;
- wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
- wc.lpfnWndProc = WndProc;
- wc.hCursor = LoadCursor(0, IDC_ARROW);
- wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
- RegisterClassW(&wc);
- CreateWindowW(wc.lpszClassName, L"MPDF", WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX | WS_VISIBLE, 260, 134, 220, 220, 0, 0, hInstance, 0);
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (int)msg.wParam;
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch (msg)
- {
- case WM_KEYDOWN:
- if (wParam == VK_ESCAPE) {
- int ret = MessageBoxW(hwnd, L"Are you sure you want to quit?",
- L"Message", MB_OKCANCEL);
- if (ret == IDOK) {
- SendMessage(hwnd, WM_CLOSE, 0, 0);
- }
- }
- break;
- case WM_CREATE:
- CenterWindow(hwnd);
- hwndButton = CreateWindowW(L"Button", L"Click to Merge PDFs", WS_VISIBLE | WS_CHILD, 0, 0, 210, 190, hwnd, (HMENU)ID_BUTTON, NULL, NULL);
- break;
- case WM_COMMAND:
- if (LOWORD(wParam) == ID_BUTTON)
- {
- string path(MAX_PATH, '\0');
- OPENFILENAME ofn;
- TCHAR szFile[MAX_PATH];
- ZeroMemory(&ofn, sizeof(ofn));
- ofn.lStructSize = sizeof(ofn);
- ofn.lpstrFile = (LPWSTR)&path[0];
- ofn.lpstrFile[0] = '\0';
- ofn.hwndOwner = NULL;
- ofn.nMaxFile = sizeof(szFile);
- ofn.lpstrFilter = TEXT("Pdf Files\0*.pdf\0Any File\0*.*\0");
- ofn.nFilterIndex = 1;
- ofn.lpstrTitle = TEXT("Select PDF files");
- ofn.lpstrInitialDir = L"";
- ofn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT;
- if (GetOpenFileName(&ofn))
- {
- OutputDebugString(ofn.lpstrFile);
- int cSize = WideCharToMultiByte(CP_ACP, 0, ofn.lpstrFile, wcslen(ofn.lpstrFile), NULL, 0, NULL, NULL);
- string output(static_cast<size_t>(cSize), '\0');
- WideCharToMultiByte(CP_ACP, 0, ofn.lpstrFile, wcslen(ofn.lpstrFile), reinterpret_cast<char*>(&output[0]), cSize, NULL, NULL);
- string delimiter = " ";
- vector<string> v = split(output, delimiter);
- vector<char*> inputfiles;
- for (unsigned int i = 1; i < v.size();i++) {
- string output = v[0] + v[i];
- char* theFilePath = new char[100];
- strcpy(theFilePath, output.c_str());
- inputfiles.emplace_back(theFilePath);
- }
- PoDoFo::PdfError::EnableDebug(false);
- char* outputfile = new char[100];
- WCHAR path[MAX_PATH];
- GetCurrentDirectory(MAX_PATH, path);
- std::wstring fileName;
- fileName = L"\\output.pdf";
- wcscat(path, fileName.c_str());
- sprintf(outputfile, "%ws", path);
- try {
- mergePDF(inputfiles, outputfile);
- }
- catch (const PoDoFo::PdfError& e) {
- OutputDebugStringA("Error occured during pdf merge!");
- e.PrintErrorMsg();
- return e.GetError();
- }
- OutputDebugString(ofn.lpstrFile);
- }
- }
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- }
- return DefWindowProcW(hwnd, msg, wParam, lParam);
- }
- void CenterWindow(HWND hwnd)
- {
- RECT rc = { 0 };
- GetWindowRect(hwnd, &rc);
- int win_w = rc.right - rc.left;
- int win_h = rc.bottom - rc.top;
- int screen_w = GetSystemMetrics(SM_CXSCREEN);
- int screen_h = GetSystemMetrics(SM_CYSCREEN);
- SetWindowPos(hwnd, HWND_TOP, (screen_w - win_w) / 2, (screen_h - win_h) / 2, 0, 0, SWP_NOSIZE);
- }
- LPWSTR stringToLPWSTR(const string& instr)
- {
- int bufferlen = ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), NULL, 0);
- LPWSTR widestr = new WCHAR[bufferlen + 1];
- ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), widestr, bufferlen);
- widestr[bufferlen] = 0;
- return widestr;
- }
- void mergePDF(vector<char*> inputfiles, char* outputfile) {
- try {
- PoDoFo::PdfMemDocument firstDoc;
- firstDoc.Load(inputfiles[0]);
- for (int k = 1; k < inputfiles.size(); k++) {
- PoDoFo::PdfMemDocument doc;
- doc.Load(inputfiles[k]);
- firstDoc.Append(doc);
- }
- firstDoc.Write(outputfile);
- }
- catch (const PoDoFo::PdfError& e) {
- throw e;
- }
- }
- vector<string> split(string s, string delimiter) {
- size_t pos_start = 0, pos_end, delim_len = delimiter.length();
- string token;
- vector<string> res;
- while ((pos_end = s.find(delimiter, pos_start)) != string::npos) {
- token = s.substr(pos_start, pos_end - pos_start);
- pos_start = pos_end + delim_len;
- res.push_back(token);
- }
- res.push_back(s.substr(pos_start));
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement