TPdf

Fuente: source/classes/tpdf.prg

Standalone class (uses libharu / hbhpdf.lib)

TPdf provides PDF document generation using the Haru Free PDF Library (libharu). It supports creating multi-page PDF documents with text, lines, rectangles, images, and fonts. Documents can be password-protected with configurable permissions.

Key DATA Members

DATATypeDescription
hPdfHandlelibharu PDF document handle
hPageHandleCurrent page handle
aPagesArrayArray of all page handles in the document
nPageNumericCurrent page number
nPageSizeNumericPage size constant (default: HPDF_PAGE_SIZE_A4)
nOrientationNumericHPDF_PAGE_PORTRAIT or HPDF_PAGE_LANDSCAPE
nHeightNumericCurrent page height in points
nWidthNumericCurrent page width in points
cFileNameCharacterOutput PDF file name
cPasswordCharacterUser password for PDF encryption
cOwnerPasswordCharacterOwner password for PDF permissions
nPermissionNumericPDF permission flags
lPreviewLogicalIf .T., open PDF after save
hImageListHashLoaded image cache
LoadedFontsArrayArray of loaded font handles

Methods

MethodDescription
New( cFileName, cPassword, cOwnerPassword, nPermission, lPreview )Create a new PDF document
SetPage( nPageSize )Set page size (e.g., HPDF_PAGE_SIZE_A4, HPDF_PAGE_SIZE_LETTER)
SetLandscape()Switch page orientation to landscape
SetPortrait()Switch page orientation to portrait
SetCompression( cMode )Set PDF compression mode
StartPage()Start a new page (adds to document)
EndPage()Finalize the current page
Say( nRow, nCol, cText, oFont, nWidth, nClrText, nBkMode, nPad )Draw text at position in points
CmSay( nRow, nCol, cText, oFont, nWidth, nClrText, nBkMode, nPad, lO2A )Draw text at position in centimeters
SayRotate( nTop, nLeft, cTxt, oFont, nClrText )Draw rotated text
DefineFont( cFontName, nSize, lEmbed )Define and load a TrueType font
AddFont( cFontName, cTtfFile )Register a TrueType font for embedding
Line( nTop, nLeft, nBottom, nRight, oPen )Draw a line in points
CmLine( nTop, nLeft, nBottom, nRight, oPen )Draw a line in centimeters
Rect( nTop, nLeft, nBottom, nRight, oPen, nColor )Draw a rectangle in points
CmRect( nTop, nLeft, nBottom, nRight, oPen, nColor )Draw a rectangle in centimeters
RoundBox( nTop, nLeft, nBottom, nRight, nWidth, nHeight, oPen, nColor, nBackColor, lFondo )Draw a rounded rectangle
DashLine( nTop, nLeft, nBottom, nRight, oPen )Draw a dashed line
SayBitmap( nRow, nCol, xBitmap, nWidth, nHeight )Place a bitmap image on the page
CmSayBitmap( nRow, nCol, xBitmap, nWidth, nHeight, nRaster, lStrech )Place a bitmap using centimeter coordinates
GetImageFromFile( cImageFile )Load an image from file for use on a page
GetTextWidth( cText, oFont )Measure text width in points
Save( cFileName )Save the PDF document to file
End()Close and release PDF resources

Example: Create a PDF with Text and Shapes

#include "FiveWin.ch"
#include "harupdf.ch"

function Main()

   local oPdf, oFont

   oPdf := TPdf():New( "output.pdf" )

   // Set up the first page
   oPdf:SetPage( HPDF_PAGE_SIZE_A4 )
   oPdf:StartPage()

   // Define a font
   oFont := oPdf:DefineFont( "Courier", 12 )

   // Draw text
   oPdf:Say( 100, 100, "Hello, PDF World!", oFont )

   // Draw a rectangle
   oPdf:Rect( 80, 80, 300, 150, , CLR_RED )

   // Draw a line
   oPdf:Line( 80, 80, 300, 150 )

   oPdf:EndPage()

   // Second page with landscape orientation
   oPdf:SetPage( HPDF_PAGE_SIZE_A4 )
   oPdf:SetLandscape()
   oPdf:StartPage()
   oPdf:Say( 100, 100, "This is landscape", oFont )
   oPdf:EndPage()

   oPdf:Save()
   oPdf:End()

return nil

Notes

Ver También