pdfHummusTest1.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Found that previous library was complied with the MSVC compiler and not compatible with GNU toolchain. Tried to use Msys with PDFHummus Cmake build tools and it was hard-coded
  2. * to MSVC. So building for GNU compiler will take some modification of Cmake.
  3. * 4.16.20 - after getting an image onto the pdf, we realized there was an error that was not getting logged/presented to us.
  4. * we came upon this error, by trying to run the pdfHummusTest1.exe and the pdf file was not being created (because it was still
  5. * open -> Windows locking files regardless of where they are open). This is the reason why we wanted to figure out if PDFHummus
  6. * had an error logging capability and then we had to try and figure out how to use it... to be continued for next time.
  7. * 19 April 2020
  8. * Gal's documentation for using the logging capabilitites was old, the LogConfiguration function takes 3 parameters.
  9. * Gal does not actually handle or log errors.
  10. */
  11. #include <stdlib.h>
  12. #include <iostream>
  13. #include "PDFWriter.h"
  14. #include "PDFPage.h"
  15. #include "PageContentContext.h"
  16. #include "PDFFormXObject.h"
  17. #include "Trace.h"
  18. #include <windows.h>
  19. #include <winnt.h>
  20. #include "PDFModifiedPage.h"
  21. #include "AbstractContentContext.h"
  22. int main(){
  23. //
  24. // Go to: "%LOCALAPPDATA%\VirtualStore" - because Windows does not write to the C:\ drive
  25. printf("if running on Windows check here for file(s): %%LOCALAPPDATA%%\\VirtualStore\n");
  26. PDFWriter pdfWriter;
  27. // PREPARE PDF DOCUMENT
  28. // ********************
  29. // get a PDFWriter object to interact with. set the PDF version, set log file
  30. EStatusCode e = pdfWriter.StartPDF("c:\\myFile.pdf",ePDFVersion13,LogConfiguration(true,true,".\\logMe"));
  31. if (e){
  32. /* 25APR20 - look through codebase and found these three reasons for nonrecoverable failure:
  33. * - user requested encryption, document doesn't support it
  34. * - it is unlikely, but for some reason GK wanted to call OutputFile::CloseFile(), it failed
  35. * - operating system was unsuccessful at giving a lock for the filename *
  36. *
  37. * * Windows 'gives' a lock, but doesn't protect others from writing to this filename
  38. */
  39. printf("Terminating (c:\\myFile.pdf): unsuccessful lock or couldn't close existing or requested encryption but not supported\n");
  40. TRACE_LOG("Terminating (c:\\myFile.pdf): unsuccessful lock or couldn't close existing or requested encryption but not supported");
  41. exit(1);
  42. }
  43. TRACE_LOG("Notified OS we might use c:\\myFile.pdf");
  44. PDFPage* pdfPage = new PDFPage();
  45. // set dimensions for an A4 sized page
  46. pdfPage->SetMediaBox(PDFRectangle(0,0,595,842));
  47. PageContentContext* pageContentContext = pdfWriter.StartPageContentContext(pdfPage);
  48. // include font file
  49. PDFUsedFont* arialTTF = pdfWriter.GetFontForFile("C:\\Windows\\Fonts\\arial.ttf");
  50. // include image file
  51. // ADD CONTENT
  52. // ********************
  53. // set font color to black
  54. pageContentContext->k(0,0,0,1);
  55. // set text object
  56. pageContentContext->BT();
  57. // set font
  58. pageContentContext->Tf(arialTTF,1);
  59. // set position of text
  60. pageContentContext->Tm(20,0,0,20,40,822);
  61. // insert text into PDF
  62. pageContentContext->Tj("Text placed and scaled with Tm");
  63. // end of text
  64. pageContentContext->ET();
  65. pdfWriter.EndPageContentContext(pageContentContext);
  66. pdfWriter.WritePageAndRelease(pdfPage);
  67. pdfWriter.EndPDF();
  68. // ****************Shutdown Usage************
  69. // ******************************************
  70. // ******************************************
  71. // pdfWriter.Shutdown("c:\\myFile.pdf.shutdown");
  72. /*PDFWriter pdfWriterB;
  73. pdfWriterB.ContinuePDF("c:\\myFile.pdf","myFile.pdf.shutdown","",LogConfiguration(true,true,".\\logMe"));
  74. PDFPage* pdfPageB = new PDFPage();
  75. // set dimensions for an A4 sized page
  76. pdfPageB->SetMediaBox(PDFRectangle(0,0,595,842));
  77. PageContentContext* pageContentContextB = pdfWriterB.StartPageContentContext(pdfPageB);
  78. PDFFormXObject* image = pdfWriterB.CreateFormXObjectFromJPGFile("C:\\Users\\bad-p\\Desktop\\Work Folder\\VS CODE\\SanAntonioPass.jpg");
  79. if (!image){
  80. TRACE_LOG("Image file not found, creating PDF without image.");
  81. printf("The image \"C:\\Users\\bad-p\\Desktop\\Work Folder\\VS CODE\\SanAntonioPass.jpg\" was not found.");
  82. }
  83. pageContentContextB->q();
  84. pageContentContextB->cm(0.4,0,0,0.4,57.5,241);
  85. pageContentContextB->Do(pdfPageB->GetResourcesDictionary().AddFormXObjectMapping(image->GetObjectID()));
  86. pageContentContextB->Q();
  87. delete image;
  88. // END PAGE, WRITE PDF.
  89. // ********************
  90. pdfWriterB.EndPageContentContext(pageContentContextB);
  91. EStatusCode f = pdfWriterB.WritePageAndRelease(pdfPageB);
  92. if (f){
  93. printf ("something interesting");
  94. }
  95. pdfWriterB.EndPDF();*/
  96. // PDFWriter pdfWriterB;
  97. //pdfWriterB.StartPDF("c:\\myFile.pdf",ePDFVersion13,LogConfiguration(true,true,".\\logMe"));
  98. //pdfWriterB.ContinuePDF("C:\\Users\\bad-p\\AppData\\Local\\VirtualStore\\myFile.pdf","C:\\Users\\bad-p\\AppData\\Local\\VirtualStore\\myFile.pdf.shutdown");
  99. // Create a new page
  100. pdfWriter.ModifyPDF("c:\\myFile.pdf",ePDFVersion13,"", LogConfiguration(true,true,".\\logMe"));
  101. PDFModifiedPage pdfPage2(&pdfWriter,0);
  102. AbstractContentContext* contentContext = pdfPage2.StartContentContext();
  103. PDFFormXObject* image = pdfWriter.CreateFormXObjectFromJPGFile("C:\\Users\\bad-p\\Desktop\\Work Folder\\VS CODE\\SanAntonioPass.jpg");
  104. if (!image){
  105. TRACE_LOG("Image file not found, creating PDF without image.");
  106. printf("The image \"C:\\Users\\bad-p\\Desktop\\Work Folder\\VS CODE\\SanAntonioPass.jpg\" was not found.");
  107. }
  108. contentContext->q();
  109. contentContext->cm(0.4,0,0,0.4,57.5,241);
  110. contentContext->Do("oooogAAABooogggAAABoogggaaa");
  111. //contentContext->Do(pdfPage2->GetResourcesDictionary().AddFormXObjectMapping(image->GetObjectID()));
  112. contentContext->Q();
  113. delete image;
  114. //AbstractContentContext::TextOptions opt(pdfWriter.GetFontForFile("C:\\Windows\\Fonts\\arial.ttf"),14,AbstractContentContext::eGray,0);
  115. //contentContext->WriteText(75,805,"Test Text",opt);
  116. pdfPage2.EndContentContext();
  117. pdfPage2.WritePage();
  118. /*pdfPage2->SetMediaBox(PDFRectangle(0,0,595,842));
  119. // Create a content context for the page
  120. PageContentContext* pageContentContext2 = pdfWriter.StartPageContentContext(pdfPage2);
  121. // End content context, and write the page
  122. pdfWriter.EndPageContentContext(pageContentContext2);
  123. pdfWriter.WritePageAndRelease(pdfPage2);*/
  124. pdfWriter.EndPDF();
  125. }