Results 1 to 7 of 7

Thread: Creating PDF file from ThinBASIC

Threaded View

  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,083
    Blog Entries
    5
    Rep Power
    725

    Lightbulb Creating PDF file from ThinBASIC

    If you could use some PDF creation from script, please read on

    I converted the original Josť Roca's Haru Free PDF Library 2.1.0 headers to ThinBASIC. As the library requires callback for code handling I created high level wrapper, which makes the PDF creation a bit easier. Here sample code:
    ' 
    ' Test of PDF wrapper
    '       
    
    Uses "Console", "Math"
    
    #INCLUDE "fileformat_pdf.tBasicU"
    
    Function TBMain()
    
      ' -- Initialize library
      pdf_InitLibrary()        
      
      ' -- Create document                                                                                          
      String       sPDFFile  = APP_SourcePath + "Test.pdf"
      tPdfDocument hDocument = pdf_CreateDocument()
      
      ' -- Font used
      String       fArialName = pdf_GetFontNameFromTTF(hDocument, "C:\Windows\Fonts\arial.ttf", TRUE)
      tPdfFont     fArial     = pdf_CreateFont(hDocument, fArialName     , "CP1250")                                
      
      ' -- Create page and retrieve the dimensions  
      tPdfPage     hPage
      tPdfNumber   nHeight, nWidth        
        
      hPage = pdf_AddPage(hDocument)   
      pdf_GetPageSize(hPage, nWidth, nHeight)                                              
      
      ' -- Create content - the coordinates are as in math:
      ' -- 0,0 is lower left corner
      
      ' -- Draw rectangle, with different contour and content
      pdf_SetStrokeColor(hPage, Rgb(96,96,96))
      pdf_SetFillColor(hPage, Rgb(224,224,224))     
      pdf_DrawRectangle(hPage, 10, nHeight-10, nWidth-20, nHeight-20)
    
      ' -- Draw cross inside it, using 3px orange line                              
      pdf_SetStrokeColor(hPage, Rgb(255,128,64))
      pdf_SetLineWidth(hPage, 3)               
      pdf_DrawLine(hPage, 10, 10, nWidth-10, nHeight-10)
      pdf_DrawLine(hPage, 10, nHeight-10, nWidth-10, 10)
      
      ' -- Write text         
      Long i
      pdf_SetFillColor(hPage, Rgb(0, 0, 0))
      For i = 1 To 8
        pdf_SetFont (hPage, fArial, 10+i*2)
        pdf_PrintText(hPage, 225, nHeight-20-i*(10+i*2), "Hello ThinBASIC!")
      Next  
      
      ' -- Draw polygon                       
      Long radius
      
      For radius = 160 To 10 Step - 10
        pdf_SetColor(hPage, Rgb(Rnd(128,255), Rnd(128,255), Rnd(128,255)))
        pdf_BeginPoly(hPage, nWidth/2, nHeight/2)
      
          For i = 0 To 360 Step 60
              pdf_AddPoly(hPage, nWidth/2+Cos(DegToRad(i))*radius, nHeight/2+Sin(DegToRad(i))*radius)
          Next     
          
         pdf_EndPoly(hPage, TRUE)                              
      Next  
     
      
      ' -- Draw image
      pdf_DrawImage(hDocument, hPage, nWidth/2-64, nHeight/2-256, APP_SourcePath+"picture.png", 128, 128 )
      
      ' -- Save to disk
      pdf_SaveDocument(hDocument, sPDFFile)
    
      PrintL 
      PrintL "File saved as:"
      PrintL sPDFFile        
      PrintL                   
      PrintL "Press any key to quit..."
      WaitKey
      ' -- Release document from memory
      pdf_ReleaseDocument(hDocument)           
      
      ' -- Release library
      pdf_ReleaseLibrary()  
      
    End Function
    
    You can download the converted headers + my special wrapper + sample code from the attachement of this post. I hope you will find use for it.

    The wrapper offers these functions, each is documented in the fileFormat_PDF.tBasicU file:
    • pdf_InitLibrary, pdf_ReleaseLibrary
    • pdf_CreateDocument, pdf_SaveDocument, pdf_ReleaseDocument
    • pdf_AddPage, pdf_GetPageSize
    • pdf_GetFontNameFromTTF, pdf_CreateFont, pdf_SetFont, pdf_PrintText
    • pdf_SetStrokeColor, pdf_SetFillColor, pdf_SetColor
    • pdf_SetLineWidth, pdf_SetLineStyle
    • pdf_DrawRectangle, pdf_DrawLine, pdf_BeginPoly, pdf_AddPoly, pdf_EndPoly, pdf_DrawImage


    Of course, the original headers from Josť offer even more power.


    Petr
    Attached Files Attached Files
    Last edited by Petr Schreiber; 30-08-2020 at 14:55.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Similar Threads

  1. Creating thinBasic keywords lists
    By ErosOlmi in forum Tips and Tricks
    Replies: 13
    Last Post: 29-12-2012, 03:45
  2. Chapter 2, example 4: Creating a program from file
    By Petr Schreiber in forum OpenCL in Action by Matthew Scarpino
    Replies: 3
    Last Post: 02-10-2012, 02:08
  3. Creating .lib file from a Dll
    By MikeTrader in forum 3rd party tools
    Replies: 3
    Last Post: 22-10-2009, 12:57
  4. Replies: 12
    Last Post: 13-06-2009, 22:00
  5. Creating .exe from thinbasic scripts
    By Macros The Black in forum thinBundle
    Replies: 1
    Last Post: 02-06-2009, 09:36

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •