1
1

BasicImageAndText.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // standard library includes
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. // end standard library includes
  6. // pdfwriter library includes
  7. #include "PDFWriter.h"
  8. #include "PDFPage.h"
  9. #include "PageContentContext.h"
  10. #include "PDFFormXObject.h"
  11. #include "ResourcesDictionary.h"
  12. // end pdfwriter library includes
  13. using namespace PDFHummus;
  14. int main(int argc, char* argv[])
  15. {
  16. PDFWriter pdfWriter;
  17. EStatusCode status;
  18. do
  19. {
  20. status = pdfWriter.StartPDF(".\\BasicImageAndText.pdf",ePDFVersion13);
  21. if(status != eSuccess)
  22. break;
  23. // Create a new page
  24. PDFPage* pdfPage = new PDFPage();
  25. pdfPage->SetMediaBox(PDFRectangle(0,0,595,842));
  26. // Create an image object from image
  27. PDFFormXObject* image = pdfWriter.CreateFormXObjectFromJPGFile("C:\\Users\\bad-p\\Desktop\\Work Folder\\VS CODE\\SanAntonioPass.jpg");
  28. if(!image)
  29. {
  30. status = eFailure;
  31. break;
  32. }
  33. // Create a content context for the page
  34. PageContentContext* pageContentContext = pdfWriter.StartPageContentContext(pdfPage);
  35. // Place the image in the center of the page
  36. pageContentContext->q();
  37. pageContentContext->cm(0.4,0,0,0.4,57.5,241);
  38. pageContentContext->Do(pdfPage->GetResourcesDictionary().AddFormXObjectMapping(image->GetObjectID()));
  39. pageContentContext->Q();
  40. // Image object can be deleted right after i was used
  41. delete image;
  42. // Create a title text over the image
  43. PDFUsedFont* arialTTF = pdfWriter.GetFontForFile("C:\\Windows\\Fonts\\arial.ttf");
  44. if(!arialTTF)
  45. {
  46. status = eFailure;
  47. break;
  48. }
  49. pageContentContext->BT();
  50. pageContentContext->k(0,0,0,1);
  51. pageContentContext->Tf(arialTTF,20);
  52. pageContentContext->Tm(1,0,0,1,90,610);
  53. pageContentContext->Tj("San Antonio Pass, Cordillera Huayhuash, Peru");
  54. pageContentContext->ET();
  55. // End content context, and write the page
  56. status = pdfWriter.EndPageContentContext(pageContentContext);
  57. if(status != eSuccess)
  58. break;
  59. status = pdfWriter.WritePageAndRelease(pdfPage);
  60. if(status != eSuccess)
  61. break;
  62. status = pdfWriter.EndPDF();
  63. }while(false);
  64. if(eSuccess == status)
  65. cout<<"Succeeded in creating PDF file\n";
  66. else
  67. cout<<"Failed in creating PDF file\n";
  68. return 0;
  69. }