//-------------------------------------------------------------------------------- // Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved. //-------------------------------------------------------------------------------- using System; using System.Runtime.InteropServices; using System.Collections; namespace test { class Class1 { public delegate Int32 PageMasterCallback(int mode, string msg, IntPtr user_data); // The following three lines import the Pagemaster functions from pagemaster.dll (via PInvoke). [DllImport("pagemaster.dll")] public static extern int PageMasterInit(string user_name, string company, string license_key); [DllImport("pagemaster.dll")] public static extern int PageMasterRun(string command_str, PageMasterCallback funct, IntPtr user_data); // You can modify the following lines with your registration information. static string username = "John Doe"; static string company = "My Company"; static string lic_key = "my license key"; static int PAGEMASTER_ERROR = 1; // Show the error message static int PAGEMASTER_MSG = 2; // Report the message static int PAGEMASTER_GETPASS = 3; // Get the password static int PAGEMASTER_OUT_FILENAME = 4; // Get the saved filenames /// Error codes static int PAGEMASTER_OK = 0; // No errors static int PAGEMASTER_ERR = 1; // Unspecified error static int PAGEMASTER_ERR_BADKEY = 2; // Bad license key static int PAGEMASTER_ERR_DIRCREATE = 3; // Failed to create the output directory static int PAGEMASTER_ERR_BADFILENAME = 4; // Bad input filename or path static int PAGEMASTER_ERR_NOOUTFILE = 5; // Output file not specified static int PAGEMASTER_ERR_READINGPDF = 6; // error reading the input document static int PAGEMASTER_ERR_PASSWORD = 7; // document is secured. bad password. static int PAGEMASTER_ERR_WRITE = 8; // error writing an output file or folder static int PAGEMASTER_ERR_NOINPUTFILES = 9; // no input files found // A custom callback that can be used to report errors and other messages. public static Int32 MyCallback(int mode, string msg, IntPtr user_data) { if (mode == PAGEMASTER_ERROR) { Console.WriteLine("Error: {0}", msg); } else if (mode == PAGEMASTER_MSG) { Console.Write("{0}", msg); } else if (mode == PAGEMASTER_GETPASS) { //gl_pass = Console.ReadLine(); // return gl_pass.c_str(); } else if(mode == PAGEMASTER_OUT_FILENAME) { //check whether the IntPtr is valid if(user_data.ToInt32() != 0) { //cast it to a GCHandle to //obtain the ArrayList GCHandle gch = (GCHandle)user_data; ArrayList list = (ArrayList)gch.Target; //add the newly created file's path list.Add(msg); } } return 0; } static void Main(string[] args) { PageMasterInit(username, company, lic_key); PageMasterCallback mycallback = new PageMasterCallback(MyCallback); // Variable used to switch between different code samples. int example = 1; if (example == 1) { /// A simple PDF PageMaster program: Pass an explicit command-line string. /// /// This example will split the file bookmark.pdf and create /// one new file for each page. /// /// The -s parameter specifies that split is the operation /// The --digits parameter specifies that the numbers in resulting /// file names should have at least 2 digits /// The -o parameter specifies the output directory. In this /// case that files should be created in the TestOutput/test1 directory PageMasterInit(username,company,lic_key); PageMasterRun("-s --digits 2 -o TestOutput/test1/ TestFiles/bookmark.pdf", mycallback, new IntPtr(0)); } else if (example == 2) { // An example of how to use PageMaster SDK to implement a command-line // application for processing of PDF documents. String s = ""; foreach (string arg in args) { s += arg + " "; } PageMasterRun(s, mycallback, new IntPtr(0)); } else if (example == 3) { bool linearized_output = true; bool aes_encryption = false; string output_file = "TestOutput/test3/test.pdf"; string open_password = "mypass"; string title = "My Title"; string author = "Joe Doe"; string subject = "My Subject"; string keywords = "key1 key2 key2"; string creator = "PDFTron PDFNet"; string producer = "PageMaster"; // Permissions bool printing = false; bool high_rez_printing = false; bool doc_modifications = true; // Changing the document bool content_extraction = true; bool commenting = true; bool forms_editing = true; bool accessibility = true; bool document_assembly = true; // Given the above settings build a command string. string s = ""; //specify the merge option s += "-m "; if (!(output_file=="")) s += "-o " + output_file + " "; if (!(open_password=="")) s += "--userpass " + open_password + " "; if(aes_encryption) s+= "--AES "; // Set the permissions. s += "-e a -d \""; if (printing == false) s += "p"; if (high_rez_printing == false) s += "h"; if (doc_modifications == false) s += "m"; if (content_extraction == false) s += "c"; if (commenting == false) s += "o"; if (forms_editing == false) s += "f"; if (accessibility == false) s += "x"; if (document_assembly == false) s += "s"; s += "\" "; if (linearized_output) s += "-l "; // Set document information. if (!(title=="")) s += "--settitle \"" + title + "\" "; if (!(author=="")) s += "--setauthor \"" + author + "\" "; if (!(subject=="")) s += "--setsubject \"" + subject + "\" "; if (!(keywords=="")) s += "--setkeywords \"" + keywords + "\" "; if (!(creator=="")) s += "--setcreator \"" + creator + "\" "; if (!(producer=="")) s += "--setproducer \"" + producer + "\" "; // specify input files and folders... s += "TestFiles/Bookmark.pdf,1-15 "; s += "TestFiles/secured.pdf|test "; s += "--verb 10"; //an arraylist to store the saved file path(s) ArrayList saved_files=new ArrayList(); //create a GCHandle to allow MyCallback to access //the object saved_files GCHandle gch = GCHandle.Alloc(saved_files); // Execute the command string. PageMasterRun(s, mycallback, (IntPtr)gch ); gch.Free(); //print the saved file(s) Console.WriteLine("The Saved File Is:"); for(int i=0; i