Example code is for an older version of Toolkit, newer code is available.
// Copyright (c) 2021 ActivePDF, Inc.
// ActivePDF Toolkit 2017
// Example generated 03/02/21
using System;
// Make sure to add the ActivePDF product .NET DLL(s) to your application.
// .NET DLL(s) are typically found in the products 'bin' folder.
class Examples
{
public static void Example()
{
string strPath;
int intOpenOutputFile;
int intOpenInputFile;
string StampFontName;
float StampFontSize;
string StampeFontText;
float StampLocationX;
float StampLocationY;
int TotalPages;
int CurrentPage;
short PageRotation;
int intGetBoundingBox;
short PageHeight;
short PageWidth;
int intCopyForm;
strPath = System.AppDomain.CurrentDomain.BaseDirectory;
// Instantiate Object
APToolkitNET.Toolkit oTK = new APToolkitNET.Toolkit();
// Create the new PDF file
intOpenOutputFile = oTK.OpenOutputFile(strPath + "new.pdf");
if (intOpenOutputFile != 0)
{
ErrorHandler("OpenOutputFile", intOpenOutputFile);
}
// Open the template PDF
intOpenInputFile = oTK.OpenInputFile(strPath + "8rotations.pdf");
if (intOpenInputFile != 0)
{
ErrorHandler("OpenInputFile", intOpenInputFile);
}
// 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
ErrorHandler("NumPages", TotalPages);
} else {
// Loop through all pages of the input file
for (CurrentPage = 1; CurrentPage <= TotalPages; CurrentPage++)
{
// Get the current page width, height and rotation
PageRotation = oTK.GetInputPageRotation(CurrentPage);
intGetBoundingBox = oTK.GetBoundingBox("", CurrentPage);
if (intGetBoundingBox != 0)
{
ErrorHandler("GetBoundingBox", intGetBoundingBox);
}
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
switch (PageRotation)
{
case 0:
oTK.PrintText(StampLocationX, StampLocationY, StampeFontText, CurrentPage);
break;
case 90:
oTK.PrintText(PageWidth - StampLocationY, StampLocationX, StampeFontText, CurrentPage);
break;
case 180:
oTK.PrintText(PageWidth - StampLocationX, PageHeight - StampLocationY, StampeFontText, CurrentPage);
break;
case 270:
oTK.PrintText(StampLocationY, PageHeight - StampLocationX, StampeFontText, CurrentPage);
break;
default:
ErrorHandler("Page Rotation", PageRotation);
break;
}
// Copy the current page to the output file
intCopyForm = oTK.CopyForm(CurrentPage, CurrentPage);
if (intCopyForm != 1)
{
ErrorHandler("CopyForm", intCopyForm);
}
}
}
// Close the new file to complete PDF creation
oTK.CloseOutputFile();
// Release Object
oTK.Dispose();
// Process Complete
WriteResults("Done!");
}
// Error Handling
public static void ErrorHandler(string strMethod, object rtnCode)
{
WriteResults(strMethod + " error: " + rtnCode.ToString());
}
// Write output data
public static void WriteResults(string content)
{
// Choose where to write out results
// Debug output
//System.Diagnostics.Debug.WriteLine("ActivePDF: * " + content);
// Console
Console.WriteLine(content);
// Log file
//using (System.IO.TextWriter writer = new System.IO.StreamWriter(System.AppDomain.CurrentDomain.BaseDirectory + "application.log", true))
//{
// writer.WriteLine("[" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "]: => " + content);
//}
}
}