Wednesday, July 27, 2011

Split PDF into single page

** This process requires you to have acrobat pro
** The acrobat reader does not compatible with it


** Create an object to hold the Acrobat
Pd = CreateObject('acroexch.pddoc')
Pn = CreateObject('acroexch.pddoc')Pd.open('input.pdf')


** Get the actual page count from pdf
PC = pd.getnumpages


** First page is 0, so the last page is the PC - 1
For cp = 0 to PC - 1


  ** Create a new and empty PDF file
  Pn.create


  ** Insert a page from the original file
  Pn.insertpages(-1, pd, cp, 1, .f.)


  ** Save the new pdf with page number
  Pn.save(1, 'output_' + padl(cp + 1, 3, '0'))


  ** Close the new pdf file
  Pn.close


Next


** Close and release all PDF process
Pd.Close
Release pd
Release Pn
Close all

Tuesday, July 26, 2011

Print to IP printer at DOS Command

In the DOS command prompt, create a batch file and type the following code:




For /R %CD% %%I in (*.PRN) do (
  @echo printing %%I
  LPR -S 192.168.1.100 -P raw %%I
)




The 192.168.1.100 is your network printer's IP address. It should be changed accordingly.


This program reads all *.PRN and print them to the specified IP printer.

Tuesday, July 19, 2011

Ontario Public Holiday Program


Lparameters pDate
Private xY, xC, xN, xK, xI
Private xJ, xM, xD, xHoliday


xHoliday = ''
Do Case
  Case Month(pDate) =  1 And ;
       Day(pDate)   =  1
    xHoliday = "New year's day"

  Case Month(pDate) =  1 And ;
       Day(pDate)   =  2 And ;
       Dow(pDate)   =  2
    xHoliday = "Monday after new year"

  Case Month(pDate) =  2 And ;
       Dow(pDate)   =  2 And ;
       Day(pDate)   > 14 And ;
       Day(pDate)   < 22
    xHoliday = "Family Day"

  Case Month(pDate) =  5 And ;
       DOW(pDate)   =  2 And ;
       DAY(pDate)   > 21 And ;
       DAY(pDate)   < 29
    xHoliday = 'Victoria Day'

  Case Month(pDate) =  7 And ;
       Day(pDate)   =  1
    xHoliday = 'Canada Day'

  Case Month(pDate) =  7 And ;
       Day(pDate)   =  2 And ;
       Dow(pDate)   =  2
    xHoliday = 'Monday after Canada Day'

  Case Month(pDate) =  8 And ;
       Dow(pDate)   =  2 And ;
       Day(pDate)   <  8
    xHoliday = 'Civil Holiday'

  Case Month(pDate) =  9 And ;
       Dow(pDate)   =  2 And ;
       Day(pDate)   <  8
    xHoliday = 'Labour Day'

  Case Month(pDate) = 10 And ;
       Dow(pDate)   =  2 And ;
       Day(pDate)   >  7 And ;
       Day(pDate)   < 15
    xHoliday = 'Thanksgiving'

  Case Month(pDate) = 12 And ;
       Day(pDate)   = 25
    xHoliday = 'Christmas Day'

  Case Month(pDate) = 12 And ;
       Day(pDate)   = 26
    xHoliday = 'Boxing Day'

  Case Month(pDate) = 12 And ;
       Day(pDate)   = 27 And ;
       Dow(pDate)   = 2
    xHoliday = 'Monday after Christmas'

  Case Month(pDate) = 12 And ;
       Day(pDate)   = 28 And ;
       Dow(pDate)   = 2
    xHoliday = 'Monday after Christmas'

  Case Month(pDate) = 12 And ;
       Day(pDate)   = 27 And ;
       Dow(pDate)   = 3
    xHoliday = 'Tuesday after Christmas'

  Case Month(pDate) = 12 And ;
       Day(pDate)   = 28 And ;
       Dow(pDate)   = 3
    xHoliday = 'Tuesday after Christmas'

  Otherwise
    xY = Year(pDate)
    xC = Int(xY / 100)
    xN = xY - 19 * Int(xY / 19)
    xK = Int((xC - 17) / 25)
    xI = xC - Int(xC / 4) - ;
         Int((xC - xK) / 3) + 19 * xN + 15
    xI = xI - 30 * Int(xI / 30)
    xI = xI - Int(xI / 28) * (1 - Int(xI / 28) * ;
         Int(28 / (xI + 1)) * Int((21 - xN) / 11))
    xJ = xY + Int(xY / 4) + xI + 2 - xC + Int(xC / 4)
    xJ = xJ - 7 * Int(xJ / 7)
    xL = xI - xJ
    xM = 3 + Int((xL + 40) / 44)
    xD = xL + 28 - 31 * Int(xM / 4)
 
    Do Case
      Case pDate = Date(xY, xM, xD) - 2
        xHoliday = 'Good Friday'

      Case pDate = Date(xY, xM, xD) + 1
        xHoliday = 'Easter Monday'

      Case Dow(pDate) = 1
        xHoliday = 'Sunday'

      Case Dow(pDate) = 7
        xHoliday = 'Saturday'

    Endcase
Endcase
Return xHoliday


Monday, July 18, 2011

Change default printer

Declare Long GetLastError In WIN32API
Declare Long SetDefaultPrinter In WINSPOOL.DRV String pPrinterName


xPrinterName = "New Printer"
xDefaultPrinter = Set("printer", 2)
If SerDefaultPrinter(xPrinterName) = 0
  MessageBox("Unable to set the " + ;
             xPrinterName         + ;
             " as the default printer", 0 + 16)
Else


  && insert your code here...


EndIf


SerDefaultPrinter(xDefaultPeinter)

Saturday, July 16, 2011

Treeview Recursive Program

It is a simple way to implement a Treeview in Foxpro.


Create a catalog database
Create Table Catalog (ID N(10), Parent N(10), Name C(50))
The ID field must be an unique number with start from 1 to 9999999999. Parent is the number pointing to the parent node (Previous level). In this program, 0 (zero) is the root node (first node) of the tree.

Create a form
Add a TreeView control into a form. Name it as tvwCatalog. This is what I usually do. I use txtSomething to indicate the object is a text box. tvw is stand for TreeView. Although it is not quite necessary, but with a good practice is always a good idea. In the Form1.Init, add Do Load_Catalog with this, 0

Create a recursive program (Load_Catalog)
Here is the code of the Load_Catalog.Prg
LPARAMETERS pTree, pNode

Private xQ, xParent, xID, xName, xCheck

***** Add the root node when the tree is empty
IF pTree.Nodes.Count = 0
pTree.Nodes.Add (,, 'C0', 'Root')
pTree.Nodes.Item('C0').Expanded = .T.
pTree.Nodes.Item('C0').Checked  = .T.
ENDIF


***** SYS(2015) returns a temporary file name
xQ = SYS(2015)


***** Read the nodes with specified parent node number
Select * from Catalog ;
 WHERE Parent = pNode ;
 ORDER BY Name        ;
 INTO CURSOR &xQ


***** If this parent has child node
IF RECCOUNT() > 0
  SCAN
    xParent = 'C' + ALLTRIM(STR(&xQ..Parent))
    xID     = 'C' + ALLTRIM(STR(&xQ..ID))
    xName   =       ALLTRIM(&xQ..Name)
    pTree.Nodes.Add (xParent, 4, xID, xName)
    DO Load_Catalog WITH pTree, &xQ..ID
  ENDSCAN
ENDIF
Select &xQ
USE




Test the code

Since the Catalog database has no record. You should only see the "Root" in the tree. Later I added an "ADD" button. Here is the code:


PRIVATE xName, xID, xParent, xName


xName = ALLTRIM(INPUTBOX('Please enter a name', ;
                         'Add New Catalog', ''))


IF NOT EMPTY(xName)
  SELECT MAX(ID + 1) as NewID FROM Catalog INTO CURSOR Q1
  IF RECCOUNT() = 0
    xID  = 1
  ELSE
    xID  = IIF(RECCOUNT() = 0, 1, Q1.NewID)
  ENDIF
  xParent = VAL(SUBSTR(thisform.tvwCatalog.SelectedItem.Key, 2))

  INSERT INTO Catalog (ID,  Parent,  Name) ;
               VALUES (xID, xParent, xName)
thisform.tvwCatalog.Init
ENDIF