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-2017 http://www.litePDF.cz
    3  *
    4  * The example code is supplied "AS IS". It disclaims all warranties, expressed
    5  * or implied, including, without limitation, the warranties of merchantability
    6  * and of fitness for any purpose. It assumes no liability for direct, indirect,
    7  * incidental, special, exemplary, or consequential damages, which may result
    8  * from the use of the code, even if advised of the possibility of such damage.
    9  *
   10  * Permission is hereby granted to use, copy, modify, and distribute this
   11  * source code, or portions hereof, for any purpose, without fee.
   12  */ 
   13 
   14 #include <vcl.h>
   15 #pragma hdrstop
   16 
   17 #include "CppBuilderUnit.h"
   18 #include "litePDF.h"
   19 //---------------------------------------------------------------------------
   20 #pragma package(smart_init)
   21 #pragma resource "*.dfm"
   22 TCppBuilderFrm *CppBuilderFrm;
   23 //---------------------------------------------------------------------------
   24 __fastcall TCppBuilderFrm::TCppBuilderFrm(TComponent* Owner)
   25    : TForm(Owner)
   26 {
   27 }
   28 //---------------------------------------------------------------------------
   29 
   30 void __fastcall TCppBuilderFrm::CreateBtnClick(TObject *Sender)
   31 {
   32    try {
   33       // define a LitePDF instance
   34       TLitePDF lpdf;
   35 
   36       // create a new PDF document
   37       lpdf.CreateMemDocument();
   38 
   39       // create a canvas to draw to
   40       Graphics::TCanvas *canvas = new Graphics::TCanvas();
   41       if (!canvas) {
   42          throw Exception("Low memory!");
   43       }
   44 
   45       HDC hdc = NULL;
   46 
   47       try {
   48          // add a page, with large-enough pixel scale
   49          hdc = lpdf.AddPage(lpdf.MMToUnit(210), lpdf.MMToUnit(297), 2100, 2970, LitePDFDrawFlag_None);
   50 
   51          // initialize the canvas
   52          canvas->Handle = hdc;
   53 
   54          // prepare text print
   55          canvas->Font->Name = "Arial";
   56          canvas->Font->Size = -240;
   57          canvas->Font->Color = clNavy;
   58 
   59          // print text
   60          canvas->TextOut(100, 100, "Hello World!");
   61 
   62          canvas->Font->Size = -100;
   63          canvas->Font->Color = clBlack;
   64          canvas->TextOut(100, 450, "from C++ Builder");
   65 
   66          // prepare a pen
   67          canvas->Pen->Width = 10;
   68          canvas->Pen->Style = psSolid;
   69 
   70          // draw three lines
   71          canvas->Pen->Color = clRed;
   72          canvas->MoveTo(1800, 100);
   73          canvas->LineTo(1800, 550);
   74 
   75          canvas->Pen->Color = clGreen;
   76          canvas->MoveTo(1810, 100);
   77          canvas->LineTo(1810, 550);
   78 
   79          canvas->Pen->Color = clBlue;
   80          canvas->MoveTo(1820, 100);
   81          canvas->LineTo(1820, 550);
   82       } __finally {
   83          delete canvas;
   84       }
   85 
   86       if (hdc) {
   87          // finish drawing
   88          lpdf.FinishPage(hdc);
   89 
   90          // save the document
   91          lpdf.SaveToFile("cppbuilder-1.pdf");
   92       }
   93 
   94       // close the document
   95       lpdf.Close();
   96    } catch (TLitePDFException &ex) {
   97       throw Exception(ex.getMessage());
   98    }
   99 }
  100 //---------------------------------------------------------------------------
  101 
  102 void __fastcall TCppBuilderFrm::OpenBtnClick(TObject *Sender)
  103 {
  104    int errCode = (int) ShellExecuteA(NULL, "open", "cppbuilder-1.pdf",
  105                                      NULL, NULL, SW_SHOW);
  106    if (errCode < 32) {
  107       throw Exception("Failed to open PDF file, error: " + AnsiString(errCode));
  108    }
  109 }
  110 //---------------------------------------------------------------------------
  111 
TOPlist