using System;
using System.Collections;
using System.Drawing;
using System.Reflection;
using System.IO;
using org.zefer.pd4ml;
namespace PD4Test
{
class Tokenizer
{
//Element list identified
private System.Collections.ArrayList elements;
//Source string to use
private string source;
//The tokenizer uses the default delimiter set: the space character, the tab character, the newline character, and the carriage-return character
private string delimiters = " \t\n\r";
///
/// Initializes a new class instance with a specified string to process
///
/// String to tokenize
public Tokenizer(string source)
{
this.elements = new System.Collections.ArrayList();
this.elements.AddRange(source.Split(this.delimiters.ToCharArray()));
this.RemoveEmptyStrings();
this.source = source;
}
///
/// Initializes a new class instance with a specified string to process
/// and the specified token delimiters to use
///
/// String to tokenize
/// String containing the delimiters
public Tokenizer(string source, string delimiters)
{
this.elements = new System.Collections.ArrayList();
this.delimiters = delimiters;
this.elements.AddRange(source.Split(this.delimiters.ToCharArray()));
this.RemoveEmptyStrings();
this.source = source;
}
///
/// Current token count for the source string
///
public int Count
{
get
{
return (this.elements.Count);
}
}
///
/// Determines if there are more tokens to return from the source string
///
/// True or false, depending if there are more tokens
public bool HasMoreTokens()
{
return (this.elements.Count > 0);
}
///
/// Returns the next token from the token list
///
/// The string value of the token
public string NextToken()
{
string result;
if (source == "") throw new System.Exception();
else
{
this.elements = new System.Collections.ArrayList();
this.elements.AddRange(this.source.Split(delimiters.ToCharArray()));
RemoveEmptyStrings();
result = (string) this.elements[0];
this.elements.RemoveAt(0);
this.source = this.source.Remove(this.source.IndexOf(result),result.Length);
this.source = this.source.TrimStart(this.delimiters.ToCharArray());
return result;
}
}
///
/// Returns the next token from the source string, using the provided
/// token delimiters
///
/// String containing the delimiters to use
/// The string value of the token
public string NextToken(string delimiters)
{
this.delimiters = delimiters;
return NextToken();
}
///
/// Removes all empty strings from the token list
///
private void RemoveEmptyStrings()
{
for (int index=0; index < this.elements.Count; index++)
if ((string)this.elements[index]== "")
{
this.elements.RemoveAt(index);
index--;
}
}
}
///
/// Summary description for Class1.
///
class PD4Test
{
const string USAGE = "Usage: pd4ml.net.exe '' [pageFormat] [-permissions ]" +
"[-bookmarks ] [-orientation ] " +
"[-insets ] [-ttf ] [-out ]";
private static void wrongParam(string msg) {
Console.Out.WriteLine("invalid parameter: " + msg);
Console.Out.WriteLine( USAGE );
}
///
/// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
//System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
//System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
String infile = null;
int width = -1;
String format = "A4";
int permissions = -1;
String bookmarks = null;
String orientation = "portrait";
String insets = null;
String ttf = null;
String outfile = null;
bool formatSet = false;
FileInfo ff;
for( int i = 0; i < args.Length; i++ ) {
if ( "-permissions".Equals( args[i] ) ) {
++i;
if ( i == args.Length ) {
wrongParam("missing permissions number (a sum of single permission codes)");
return;
}
permissions = Int32.Parse(args[i]);
continue;
}
if ( "-bookmarks".Equals( args[i] ) ) {
++i;
if ( i == args.Length ) {
wrongParam("missing bookmark type (HEADINGS or ANCHORS)");
return;
}
bookmarks = args[i];
continue;
}
if ( "-orientation".Equals( args[i] ) ) {
++i;
if ( i == args.Length ) {
wrongParam("missing orientation type (PORTRAIT or LANDSCAPE)");
return;
}
orientation = args[i];
continue;
}
if ( "-insets".Equals( args[i] ) ) {
++i;
if ( i == args.Length ) {
wrongParam("missing insets (for example: -insets 5,10,5,5mm)");
return;
}
insets = args[i];
continue;
}
if ( "-ttf".Equals( args[i] ) ) {
++i;
if ( i == args.Length ) {
wrongParam("missing TTF fonts directory");
return;
}
ff = new FileInfo(args[i]);
ttf = ff.FullName;
continue;
}
if ( "-out".Equals( args[i] ) ) {
++i;
if ( i == args.Length ) {
wrongParam("missing output file name");
return;
}
outfile = args[i];
continue;
}
if ( args[i].StartsWith("-") ) {
wrongParam("unknown: " + args[i]);
return;
}
if ( infile == null ) {
string ss = args[i];
if(!ss.StartsWith("http://") && !ss.StartsWith("file://")) {
ff = new FileInfo(ss);
infile = "file:///" + ff.FullName;
}
else
infile = ss;
continue;
}
if ( width == -1 ) {
width = Int32.Parse(args[i]);
continue;
}
if ( !formatSet ) {
formatSet = true;
format = args[i];
continue;
}
wrongParam(args[i]);
return;
}
if ( infile == null ) {
Console.Out.WriteLine("source URL is missing");
Console.Out.WriteLine( USAGE );
return;
}
if ( width == -1 ) {
Console.Out.WriteLine("HTML width parameter is missing");
Console.Out.WriteLine( USAGE );
return;
}
PD4Test converter = new PD4Test();
converter.generatePDF(
infile, width, format, permissions, bookmarks, orientation,
insets, ttf, outfile );
}
private void generatePDF(String inputUrl, int htmlWidth, String pageFormat, int permissions,
String bookmarks, String orientation, String insets, String fontsDir, String outfile)
{
PD4ML pd4ml = new PD4ML();
if ( insets != null ) {
Tokenizer st = new Tokenizer( insets, ",");
try {
int top = Int32.Parse(st.NextToken());
int left = Int32.Parse(st.NextToken());
int bottom = Int32.Parse(st.NextToken());
int right = Int32.Parse(st.NextToken());
String units = st.NextToken();
Rectangle ins = new Rectangle(top, left, bottom, right);
if ("MM".Equals(units.ToUpper())) {
pd4ml.PageInsetsMM = (ins);
} else {
pd4ml.PageInsets = (ins);
}
} catch (Exception e) {
throw new Exception(
"Invalid page insets (top, left, bottom, right, units): "
+ insets );
}
}
pd4ml.HtmlWidth = (htmlWidth);
if(orientation == null) orientation = "portrait";
if ("PORTRAIT".Equals(orientation.ToUpper()))
pd4ml.PageSize = PD4Constants.getSizeByName(pageFormat);
else
pd4ml.PageSize = pd4ml.changePageOrientation( PD4Constants.getSizeByName(pageFormat) );
if ( permissions != -1 ) pd4ml.setPermissions("empty", permissions, true);
if ( bookmarks != null ) {
if ( "ANCHORS".Equals(bookmarks.ToUpper()) )
pd4ml.generateOutlines(false);
else if ( "HEADINGS".Equals(bookmarks.ToUpper()) )
pd4ml.generateOutlines(true);
}
if ( fontsDir != null && fontsDir.Length > 0 ) pd4ml.useTTF( fontsDir, true );
if (outfile == null || outfile.Equals("-"))
{
pd4ml.render(new Uri(inputUrl), Console.Out);
}
else
pd4ml.render(
new Uri(inputUrl),
new System.IO.FileStream(outfile, System.IO.FileMode.Create));
}
}
}