HTML to PDF converter for Java and .NET

HOME   FEATURES   PRODUCTS   DOWNLOADS   BUY NOW!   SUPPORT

PD4ML.NET API Usage Example

The following code shows how to use PD4ML.NET API from your .NET application. It is a simple command line HTML-to-PDF conversion utility, which utilizes the basic PD4ML features. An extended version of the command-line utility (pd4net.exe) is included to PD4ML.NET distribution in binary form.

 

using System;
using
System.Collections;
using
System.Drawing;
using
System.Reflection;
using
System.IO;
using
org.zefer.pd4ml;

namespace
PD4Test {

class PD4Test {

const string USAGE =
    "Usage: pd4net.exe '<url>' <htmlWidth> [pageFormat] [-permissions <NUMBER>]"
+
   
"[-bookmarks <HEADINGS|ANCHORS>] [-orientation <PORTRAIT|LANDSCAPE>] " +
   
"[-ttf <ttf_fonts_dir>] [-out <output_file_path>]";

private static void wrongParam(string msg) {
    Console.Out.WriteLine("invalid parameter: " + msg);
   
Console.Out.WriteLine( USAGE );
}

private void generatePDF(String inputUrl, int htmlWidth,
        String pageFormat, int permissions, String bookmarks,
       
String orientation, String fontsDir, String outfile) {

    PD4ML pd4ml = new PD4ML();

    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));
    }
}

[STAThread]

static void Main(string[] args) {
    String
infile = null;
   
int width = -1;
   
String format = "A4";
   
int permissions = -1;
   
String bookmarks = null;
   
String orientation = "portrait";
   
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 ( "-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, ttf, outfile );
}
}
 

PD4ML.NET API  copies the original PD4ML API for Java - it is just adjusted for .NET specifics. So the original PD4ML Java documentation is also a source of useful information for .NET development.

Copyright ©2004-24 zefer|org. All rights reserved. Bookmark and Share