Example code is for an older version of Toolkit, newer code is available.
# Copyright (c) 2021 ActivePDF, Inc.
# ActivePDF Toolkit 2017
# Example generated 01/25/21
require 'win32ole'
# Get current path
strPath = File.expand_path(File.dirname(__FILE__)) + "\\"
# Instantiate Object
oTK = WIN32OLE.new("APToolkit.Object")
# Create the new PDF file
intOpenOutputFile = oTK.OpenOutputFile(strPath + 'new.pdf')
if intOpenOutputFile != 0
puts "Error in OpenOutputFile: #{intOpenOutputFile}"
end
# Open the template PDF
intOpenInputFile = oTK.OpenInputFile(strPath + '8rotations.pdf')
if intOpenInputFile != 0
puts "Error in OpenInputFile: #{intOpenInputFile}"
end
# Font and Text variables
StampFontName = 'Helvetica'
StampFontSize = 12
StampeFontText = 'Confidential'
StampLocationX = 72
StampLocationY = 24
# Get the page count of the input file
TotalPages = oTK.NumPages('')
if TotalPages < 1
# Error getting page count of input file
puts "Error in NumPages: #{TotalPages}"
else
# Loop through all pages of the input file
CurrentPage = 1
for CurrentPage in CurrentPage..TotalPages
# Get the current page width, height and rotation
PageRotation = oTK.GetInputPageRotation(CurrentPage)
intGetBoundingBox = oTK.GetBoundingBox('', CurrentPage)
if intGetBoundingBox != 0
puts "Error in GetBoundingBox: #{intGetBoundingBox}"
end
PageHeight = oTK.BBHeight
PageWidth = oTK.BBWidth
# Set font properties and text rotation
oTK.SetFont(StampFontName, StampFontSize, CurrentPage)
oTK.SetTextColor(255, 0, 0, 0, CurrentPage)
oTK.SetTextRotation(PageRotation)
# Depending on the rotation of the page, adjust coordinates
# This only accounts for rotations of 0, 90, 180, 270
case PageRotation
when 0
oTK.PrintText(StampLocationX, StampLocationY, StampeFontText, CurrentPage)
when 90
oTK.PrintText(PageWidth - StampLocationY, StampLocationX, StampeFontText, CurrentPage)
when 180
oTK.PrintText(PageWidth - StampLocationX, PageHeight - StampLocationY, StampeFontText, CurrentPage)
when 270
oTK.PrintText(StampLocationY, PageHeight - StampLocationX, StampeFontText, CurrentPage)
else
puts "Error in Page Rotation: #{PageRotation}"
end
# Copy the current page to the output file
intCopyForm = oTK.CopyForm(CurrentPage, CurrentPage)
if intCopyForm != 1
puts "Error in CopyForm: #{intCopyForm}"
end
end
end
# Close the new file to complete PDF creation
oTK.CloseOutputFile()
# Release Object
oTK = ''
# Process Complete
puts "Done!"