pdfHummusTest1.cpp 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. int main(){
  19. printf("if running on Windows check here for file(s): %%AppData%%\\Local\\VirtualStore\n");
  20. PDFWriter pdfWriter;
  21. // PREPARE PDF DOCUMENT
  22. // ********************
  23. // get a PDFWriter object to interact with. set the PDF version, set log file
  24. EStatusCode e = pdfWriter.StartPDF("c:\\myFile.pdf",ePDFVersion13,LogConfiguration(true,true,".\\logMe"));
  25. if (e){
  26. /* 25APR20 - look through codebase and found these three reasons for nonrecoverable failure:
  27. * - user requested encryption, document doesn't support it
  28. * - it is unlikely, but for some reason GK wanted to call OutputFile::CloseFile(), it failed
  29. * - operating system was unsuccessful at giving a lock for the filename *
  30. *
  31. * * Windows 'gives' a lock, but doesn't protect others from writing to this filename
  32. */
  33. printf("Terminating (c:\\myFile.pdf): unsuccessful lock or couldn't close existing or requested encryption but not supported\n");
  34. TRACE_LOG("Terminating (c:\\myFile.pdf): unsuccessful lock or couldn't close existing or requested encryption but not supported");
  35. exit(1);
  36. }
  37. TRACE_LOG("Notified OS we might use c:\\myFile.pdf");
  38. PDFPage* pdfPage = new PDFPage();
  39. // set dimensions for an A4 sized page
  40. pdfPage->SetMediaBox(PDFRectangle(0,0,595,842));
  41. PageContentContext* pageContentContext = pdfWriter.StartPageContentContext(pdfPage);
  42. // include font file
  43. PDFUsedFont* arialTTF = pdfWriter.GetFontForFile("C:\\Windows\\Fonts\\arial.ttf");
  44. // ADD CONTENT
  45. // ********************
  46. PDFFormXObject* image = pdfWriter.CreateFormXObjectFromJPGFile("C:\\Users\\bad-p\\Desktop\\Work Folder\\VS CODE\\SanAntonioPass.jpg");
  47. if (!image){
  48. printf ("hiiii");
  49. }
  50. pageContentContext->q();
  51. pageContentContext->cm(1,0,0,1,0,0);
  52. //pageContentContext->cm(0.4,0,0,0.4,57.5,241);
  53. pageContentContext->Do(pdfPage->GetResourcesDictionary().AddFormXObjectMapping(image->GetObjectID()));
  54. pageContentContext->Q();
  55. delete image;
  56. // set font color to black
  57. pageContentContext->k(0,0,0,1);
  58. // set text object
  59. pageContentContext->BT();
  60. // set font
  61. pageContentContext->Tf(arialTTF,1);
  62. // set position of text
  63. pageContentContext->Tm(20,0,0,20,40,822);
  64. // insert text into PDF
  65. pageContentContext->Tj("Text placed and scaled with Tm");
  66. // end of text
  67. pageContentContext->ET();
  68. // END PAGE, WRITE PDF.
  69. // ********************
  70. pdfWriter.EndPageContentContext(pageContentContext);
  71. EStatusCode f = pdfWriter.WritePageAndRelease(pdfPage);
  72. if (f){
  73. printf ("something interesting");
  74. }
  75. pdfWriter.EndPDF();
  76. }