cdrPDF2Clip.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "cdrapp.h"
  2. #include <stdio.h>
  3. #include <windows.h>
  4. #include <thread>
  5. #include <chrono>
  6. #define CUSTOM_FORMAT RegisterClipboardFormatA("Portable Document Format")
  7. bool pdf_to_clipboard(const char *pdffile)
  8. {
  9. // 打开剪贴板
  10. if (!OpenClipboard(NULL)) {
  11. printf("Failed to open clipboard.\n");
  12. return false;
  13. }
  14. // 清空剪贴板
  15. EmptyClipboard();
  16. // 读取PDF文件到内存
  17. FILE *file = fopen(pdffile, "rb");
  18. if (!file) {
  19. printf("Failed to open file.\n");
  20. CloseClipboard();
  21. return false;
  22. }
  23. // 获取文件大小
  24. fseek(file, 0, SEEK_END);
  25. size_t fileSize = ftell(file);
  26. fseek(file, 0, SEEK_SET);
  27. // 分配内存并读取文件内容
  28. void *pdfData = malloc(fileSize);
  29. if (!pdfData) {
  30. printf("Failed to allocate memory.\n");
  31. fclose(file);
  32. CloseClipboard();
  33. return false;
  34. }
  35. fread(pdfData, 1, fileSize, file);
  36. fclose(file);
  37. // 将二进制数据写入剪贴板
  38. HGLOBAL hGlobal = GlobalAlloc(GHND, fileSize);
  39. if (!hGlobal) {
  40. printf("Failed to allocate global memory.\n");
  41. free(pdfData);
  42. CloseClipboard();
  43. return false;
  44. }
  45. memcpy(GlobalLock(hGlobal), pdfData, fileSize);
  46. GlobalUnlock(hGlobal);
  47. if (!SetClipboardData(CUSTOM_FORMAT, hGlobal)) {
  48. printf("Failed to set clipboard data.\n");
  49. GlobalFree(hGlobal);
  50. free(pdfData);
  51. CloseClipboard();
  52. return false;
  53. }
  54. // 关闭剪贴板
  55. CloseClipboard();
  56. printf("PDF binary data copied to clipboard using custom format.\n");
  57. // 不要忘记释放内存
  58. free(pdfData);
  59. return true;
  60. }
  61. bool clipboard_to_pdf(const char *outputFile)
  62. {
  63. // 打开剪贴板
  64. if (!OpenClipboard(NULL)) {
  65. printf("Failed to open clipboard.\n");
  66. return false;
  67. }
  68. // 获取剪贴板中的PDF数据
  69. HANDLE hData = GetClipboardData(CUSTOM_FORMAT);
  70. if (!hData) {
  71. printf("Failed to get clipboard data.\n");
  72. CloseClipboard();
  73. return false;
  74. }
  75. // 锁定内存并获取指针
  76. void *pdfData = GlobalLock(hData);
  77. if (!pdfData) {
  78. printf("Failed to lock global memory.\n");
  79. CloseClipboard();
  80. return false;
  81. }
  82. // 获取PDF数据的大小
  83. size_t fileSize = GlobalSize(hData);
  84. // 将PDF数据写入文件
  85. FILE *file = fopen(outputFile, "wb");
  86. if (!file) {
  87. printf("Failed to open output file.\n");
  88. GlobalUnlock(hData);
  89. CloseClipboard();
  90. return false;
  91. }
  92. fwrite(pdfData, 1, fileSize, file);
  93. fclose(file);
  94. // 解锁内存并关闭剪贴板
  95. GlobalUnlock(hData);
  96. CloseClipboard();
  97. printf("PDF binary data from clipboard saved to file: %s\n", outputFile);
  98. return true;
  99. }
  100. bool cdr_savepdf(corel *cdr, const char *outputFile)
  101. {
  102. DeleteFile(_bstr_t(outputFile));
  103. auto pdfst = cdr->ActiveDocument->PDFSettings;
  104. pdfst->BitmapCompression = pdfLZW;
  105. pdfst->ColorMode = pdfCMYK;
  106. pdfst->EmbedBaseFonts = cdrFalse;
  107. pdfst->EmbedFonts = cdrFalse;
  108. pdfst->FileInformation = cdrFalse;
  109. pdfst->Hyperlinks = cdrFalse;
  110. pdfst->IncludeBleed = cdrFalse;
  111. pdfst->Linearize = cdrTrue;
  112. pdfst->MaintainOPILinks = cdrTrue;
  113. pdfst->Overprints = cdrTrue;
  114. pdfst->PutpdfVersion(pdfVersion14); //'pdfVersion14 : pdfVersion13;
  115. pdfst->PublishRange = pdfSelection;
  116. pdfst->RegistrationMarks = cdrFalse;
  117. pdfst->SpotColors = cdrTrue;
  118. pdfst->Startup = pdfPageOnly;
  119. pdfst->SubsetFonts = cdrFalse;
  120. pdfst->TextAsCurves = cdrFalse;
  121. pdfst->Thumbnails = cdrFalse;
  122. cdr->ActiveDocument->PublishToPDF(_bstr_t(outputFile));
  123. return true;
  124. }
  125. void CdrCopy_to_AdobeAI(corel *cdr)
  126. {
  127. char path[MAX_PATH] = {0};
  128. GetTempPath(MAX_PATH, path);
  129. char *f = strcat(path, "CDR2AI.pdf");
  130. if (cdr_savepdf(cdr, f))
  131. pdf_to_clipboard(f);
  132. }
  133. bool pdf_ImportCdr(corel *cdr, const char *pdffile)
  134. {
  135. auto si = cdr->CreateStructImportOptions();
  136. si->MaintainLayers = true;
  137. auto impflt = cdr->ActiveLayer->ImportEx(_bstr_t(pdffile), cdrAutoSense , si);
  138. impflt->Finish();
  139. return true;
  140. }
  141. void AdobeAI_Copy_ImportCdr(corel *cdr)
  142. {
  143. char path[MAX_PATH] = {0};
  144. GetTempPath(MAX_PATH, path);
  145. char *f = strcat(path, "CDR2AI.pdf");
  146. if (clipboard_to_pdf(f)){
  147. // 延时 0.5 秒
  148. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  149. pdf_ImportCdr(cdr, f);
  150. }
  151. }