1
0

clip2pdf.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #define CUSTOM_FORMAT RegisterClipboardFormatA("Portable Document Format")
  4. int main(int argc, char* argv[])
  5. {
  6. // 要保存的PDF文件路径
  7. const char* outputFile = "output.pdf";
  8. if(2 == argc)
  9. outputFile = argv[1];
  10. // 打开剪贴板
  11. if (!OpenClipboard(NULL)) {
  12. printf("Failed to open clipboard.\n");
  13. return 1;
  14. }
  15. // 获取剪贴板中的PDF数据
  16. HANDLE hData = GetClipboardData(CUSTOM_FORMAT);
  17. if (!hData) {
  18. printf("Failed to get clipboard data.\n");
  19. CloseClipboard();
  20. return 1;
  21. }
  22. // 锁定内存并获取指针
  23. void* pdfData = GlobalLock(hData);
  24. if (!pdfData) {
  25. printf("Failed to lock global memory.\n");
  26. CloseClipboard();
  27. return 1;
  28. }
  29. // 获取PDF数据的大小
  30. size_t fileSize = GlobalSize(hData);
  31. // 将PDF数据写入文件
  32. FILE* file = fopen(outputFile, "wb");
  33. if (!file) {
  34. printf("Failed to open output file.\n");
  35. GlobalUnlock(hData);
  36. CloseClipboard();
  37. return 1;
  38. }
  39. fwrite(pdfData, 1, fileSize, file);
  40. fclose(file);
  41. // 解锁内存并关闭剪贴板
  42. GlobalUnlock(hData);
  43. CloseClipboard();
  44. printf("PDF binary data from clipboard saved to file: %s\n", outputFile);
  45. return 0;
  46. }