cppbuilder (CppBuilderUnit.cpp) – a similar “Hello World!” example, but this time for a C++ Builder, which also uses the plain C++ interface. One difference is that it doesn't draw to an HDC, but to a TCanvas.

    1 /*
    2  * (c) 2013-2016 http://www.litePDF.cz
    3  * (c) 2017 zyx [@:] zyx gmx [dot] us
    4  *
    5  * This software is provided 'as-is', without any express or implied
    6  * warranty.  In no event will the authors be held liable for any damages
    7  * arising from the use of this software.
    8  *
    9  * Permission is granted to anyone to use this software for any purpose,
   10  * including commercial applications, and to alter it and redistribute it
   11  * freely, subject to the following restrictions:
   12  *
   13  * 1. The origin of this software must not be misrepresented; you must not
   14  *    claim that you wrote the original software. If you use this software
   15  *    in a product, an acknowledgment in the product documentation would be
   16  *    appreciated but is not required.
   17  * 2. Altered source versions must be plainly marked as such, and must not be
   18  *    misrepresented as being the original software.
   19  * 3. This notice may not be removed or altered from any source distribution.
   20  */ 
   21 
   22 #include <vcl.h>
   23 #pragma hdrstop
   24 
   25 #include "CppBuilderUnit.h"
   26 #include "litePDF.h"
   27 //---------------------------------------------------------------------------
   28 #pragma package(smart_init)
   29 #pragma resource "*.dfm"
   30 TCppBuilderFrm *CppBuilderFrm;
   31 //---------------------------------------------------------------------------
   32 __fastcall TCppBuilderFrm::TCppBuilderFrm(TComponent* Owner)
   33    : TForm(Owner)
   34 {
   35 }
   36 //---------------------------------------------------------------------------
   37 
   38 void __fastcall TCppBuilderFrm::CreateBtnClick(TObject *Sender)
   39 {
   40    try {
   41       // define a LitePDF instance
   42       TLitePDF lpdf;
   43 
   44       // create a new PDF document
   45       lpdf.CreateMemDocument();
   46 
   47       // create a canvas to draw to
   48       Graphics::TCanvas *canvas = new Graphics::TCanvas();
   49       if (!canvas) {
   50          throw Exception("Low memory!");
   51       }
   52 
   53       HDC hdc = NULL;
   54 
   55       try {
   56          // add a page, with large-enough pixel scale
   57          hdc = lpdf.AddPage(lpdf.MMToUnit(210), lpdf.MMToUnit(297), 2100, 2970, LitePDFDrawFlag_None);
   58 
   59          // initialize the canvas
   60          canvas->Handle = hdc;
   61 
   62          // prepare text print
   63          canvas->Font->Name = "Arial";
   64          canvas->Font->Size = -240;
   65          canvas->Font->Color = clNavy;
   66 
   67          // print text
   68          canvas->TextOut(100, 100, "Hello World!");
   69 
   70          canvas->Font->Size = -100;
   71          canvas->Font->Color = clBlack;
   72          canvas->TextOut(100, 450, "from C++ Builder");
   73 
   74          // prepare a pen
   75          canvas->Pen->Width = 10;
   76          canvas->Pen->Style = psSolid;
   77 
   78          // draw three lines
   79          canvas->Pen->Color = clRed;
   80          canvas->MoveTo(1800, 100);
   81          canvas->LineTo(1800, 550);
   82 
   83          canvas->Pen->Color = clGreen;
   84          canvas->MoveTo(1810, 100);
   85          canvas->LineTo(1810, 550);
   86 
   87          canvas->Pen->Color = clBlue;
   88          canvas->MoveTo(1820, 100);
   89          canvas->LineTo(1820, 550);
   90       } __finally {
   91          delete canvas;
   92       }
   93 
   94       if (hdc) {
   95          // finish drawing
   96          lpdf.FinishPage(hdc);
   97 
   98          // save the document
   99          lpdf.SaveToFile("cppbuilder-1.pdf");
  100       }
  101 
  102       // close the document
  103       lpdf.Close();
  104    } catch (TLitePDFException &ex) {
  105       throw Exception(ex.getMessage());
  106    }
  107 }
  108 //---------------------------------------------------------------------------
  109 
  110 void __fastcall TCppBuilderFrm::OpenBtnClick(TObject *Sender)
  111 {
  112    int errCode = (int) ShellExecuteA(NULL, "open", "cppbuilder-1.pdf",
  113                                      NULL, NULL, SW_SHOW);
  114    if (errCode < 32) {
  115       throw Exception("Failed to open PDF file, error: " + AnsiString(errCode));
  116    }
  117 }
  118 //---------------------------------------------------------------------------
  119