context: /*
   * << Haru Free PDF Library 2.0.6 >> -- hpdf.cs
   *
   * C# wrapper for libhpdf.dll
   *
   * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
   *
   * Permission to use, copy, modify, distribute and sell this software
   * and its documentation for any purpose is hereby granted without fee,
   * provided that the above copyright notice appear in all copies and
   * that both that copyright notice and this permission notice appear
   * in supporting documentation.
   * It is provided "as is" without express or implied warranty.
   *
   */
  
  using System;
  using System.Runtime.InteropServices;
  
  
  namespace HPdf {
  
  public enum HPdfPageLayout : uint {
      HPDF_PAGE_LAYOUT_SINGLE = 0,
      HPDF_PAGE_LAYOUT_ONE_COLUMN,
      HPDF_PAGE_LAYOUT_TWO_CLUMN_LEFT,
      HPDF_PAGE_LAYOUT_TWO_CLUMN_RIGHT,
      HPDF_PAGE_LAYOUT_EOF
  };
  
  public enum HPdfPageMode : uint {
      HPDF_PAGE_MODE_USE_NONE = 0,
      HPDF_PAGE_MODE_USE_OUTLINE,
      HPDF_PAGE_MODE_USE_THUMBS,
      HPDF_PAGE_MODE_FULL_SCREEN,
      HPDF_PAGE_MODE_EOF
  };
  
  public enum HPdfPageSizes : uint {
      HPDF_PAGE_SIZE_LETTER = 0,
      HPDF_PAGE_SIZE_LEGAL,
      HPDF_PAGE_SIZE_A3,
      HPDF_PAGE_SIZE_A4,
      HPDF_PAGE_SIZE_A5,
      HPDF_PAGE_SIZE_B4,
      HPDF_PAGE_SIZE_B5,
      HPDF_PAGE_SIZE_EXECUTIVE,
      HPDF_PAGE_SIZE_US4x6,
      HPDF_PAGE_SIZE_US4x8,
      HPDF_PAGE_SIZE_US5x7,
      HPDF_PAGE_SIZE_COMM10,
      HPDF_PAGE_SIZE_EOF
  };
  
  
  public enum HPdfPageDirection : uint {
      HPDF_PAGE_PORTRAIT = 0,
      HPDF_PAGE_LANDSCAPE
  };
  
  
  public enum HPdfPageNumStyle : uint {
      HPDF_PAGE_NUM_STYLE_DECIMAL = 0,
      HPDF_PAGE_NUM_STYLE_UPPER_ROMAN,
      HPDF_PAGE_NUM_STYLE_LOWER_ROMAN,
      HPDF_PAGE_NUM_STYLE_UPPER_LETTERS,
      HPDF_PAGE_NUM_STYLE_LOWER_LETTERS,
      HPDF_PAGE_NUM_STYLE_EOF
  };
  
  public enum HPdfWritingMode : uint {
      HPDF_WMODE_HORIZONTAL = 0,
      HPDF_WMODE_VERTICAL,
      HPDF_WMODE_EOF
  };
  
  
  public enum HPdfEncoderType : uint {
      HPDF_ENCODER_TYPE_SINGLE_BYTE = 0,
      HPDF_ENCODER_TYPE_DOUBLE_BYTE,
      HPDF_ENCODER_TYPE_UNINITIALIZED,
      HPDF_ENCODER_UNKNOWN
  };
  
  
  public enum HPdfByteType : uint {
      HPDF_BYTE_TYPE_SINGLE = 0,
      HPDF_BYTE_TYPE_LEAD,
      HPDF_BYTE_TYPE_TRIAL,
      HPDF_BYTE_TYPE_UNKNOWN
  };
  
  
  public enum HPdfAnnotHighlightMode : uint {
      HPDF_ANNOT_NO_HIGHTLIGHT = 0,
      HPDF_ANNOT_INVERT_BOX,
      HPDF_ANNOT_INVERT_BORDER,
      HPDF_ANNOT_DOWN_APPEARANCE,
      HPDF_ANNOT_HIGHTLIGHT_MODE_EOF
  };
  
  
  public enum HPdfAnnotIcon : uint {
      HPDF_ANNOT_ICON_COMMENT = 0,
      HPDF_ANNOT_ICON_KEY,
      HPDF_ANNOT_ICON_NOTE,
      HPDF_ANNOT_ICON_HELP,
      HPDF_ANNOT_ICON_NEW_PARAGRAPH,
      HPDF_ANNOT_ICON_PARAGRAPH,
      HPDF_ANNOT_ICON_INSERT,
      HPDF_ANNOT_ICON_EOF
  };
  
  public enum HPdfColorSpace : uint {
      HPDF_CS_DEVICE_GRAY = 0,
      HPDF_CS_DEVICE_RGB,
      HPDF_CS_DEVICE_CMYK,
      HPDF_CS_CAL_GRAY,
      HPDF_CS_CAL_RGB,
      HPDF_CS_LAB,
      HPDF_CS_ICC_BASED,
      HPDF_CS_SEPARATION,
      HPDF_CS_DEVICE_N,
      HPDF_CS_INDEXED,
      HPDF_CS_PATTERN,
      HPDF_CS_EOF
  };
  
  
  public enum HPdfInfoType : uint {
      /* date-time type parameters */
      HPDF_INFO_CREATION_DATE = 0,
      HPDF_INFO_MOD_DATE = 1,
  
      /* string type parameters */
      HPDF_INFO_AUTHOR = 2,
      HPDF_INFO_CREATOR = 3,
      HPDF_INFO_PRODUCER = 4,
      HPDF_INFO_TITLE = 5,
      HPDF_INFO_SUBJECT = 6,
      HPDF_INFO_KEYWORDS = 7,
      HPDF_INFO_EOF = 8
  };
  
  public enum  HPdfEncryptMode: uint {
      HPDF_ENCRYPT_R2    = 2,
      HPDF_ENCRYPT_R3    = 3
  };
  
  public enum  HPdfTextRenderingMode: uint {
      HPDF_FILL = 0,
      HPDF_STROKE = 1,
      HPDF_FILL_THEN_STROKE = 2,
      HPDF_INVISIBLE = 3,
      HPDF_FILL_CLIPPING = 4,
      HPDF_STROKE_CLIPPING = 5,
      HPDF_FILL_STROKE_CLIPPING = 6,
      HPDF_CLIPPING = 7,
      HPDF_RENDERING_MODE_EOF = 8
  };
  
  public enum HPdfLineCap {
      HPDF_BUTT_END = 0,
      HPDF_ROUND_END = 1,
      HPDF_PROJECTING_SCUARE_END = 2,
      HPDF_LINECAP_EOF = 3
  };
  
  public enum HPdfLineJoin {
      HPDF_MITER_JOIN = 0,
      HPDF_ROUND_JOIN = 1,
      HPDF_BEVEL_JOIN = 2,
      HPDF_LINEJOIN_EOF = 3
  };
  
  public enum HPdfTextAlignment {
      HPDF_TALIGN_LEFT = 0,
      HPDF_TALIGN_RIGHT = 1,
      HPDF_TALIGN_CENTER = 2,
      HPDF_TALIGN_JUSTIFY = 3
  };
  
  public enum HPdfTransitionStyle {
      HPDF_TS_WIPE_RIGHT = 0,
      HPDF_TS_WIPE_UP,
      HPDF_TS_WIPE_LEFT,
      HPDF_TS_WIPE_DOWN,
      HPDF_TS_BARN_DOORS_HORIZONTAL_OUT,
      HPDF_TS_BARN_DOORS_HORIZONTAL_IN,
      HPDF_TS_BARN_DOORS_VERTICAL_OUT,
      HPDF_TS_BARN_DOORS_VERTICAL_IN,
      HPDF_TS_BOX_OUT,
      HPDF_TS_BOX_IN,
      HPDF_TS_BLINDS_HORIZONTAL,
      HPDF_TS_BLINDS_VERTICAL,
      HPDF_TS_DISSOLVE,
      HPDF_TS_GLITTER_RIGHT,
      HPDF_TS_GLITTER_DOWN,
      HPDF_TS_GLITTER_TOP_LEFT_TO_BOTTOM_RIGHT,
      HPDF_TS_REPLACE,
      HPDF_TS_EOF
  };
  
  
  public enum HPdfBlendMode {
      HPDF_BM_NORMAL = 0,
      HPDF_BM_MULTIPLY,
      HPDF_BM_SCREEN,
      HPDF_BM_OVERLAY,
      HPDF_BM_DARKEN,
      HPDF_BM_LIGHTEN,
      HPDF_BM_COLOR_DODGE,
      HPDF_BM_COLOR_BUM,
      HPDF_BM_HARD_LIGHT,
      HPDF_BM_SOFT_LIGHT,
      HPDF_BM_DIFFERENCE,
      HPDF_BM_EXCLUSHON,
      HPDF_BM_EOF
  };
  
  [StructLayout(LayoutKind.Sequential)]
  public struct HPdfRect {
      public float left;
      public float  bottom;
      public float  right;
      public float  top;
  }
  
  [StructLayout(LayoutKind.Sequential)]
  public struct HPdfBox {
      public float left;
      public float  bottom;
      public float  right;
      public float  top;
  }
  
  [StructLayout(LayoutKind.Sequential)]
  public struct HPdfPoint {
      public float  x;
      public float  y;
  }
  
  [StructLayout(LayoutKind.Sequential)]
  public struct HPdfDate {
      public int    year;
      public int    month;
      public int    day;
      public int    hour;
      public int    minutes;
      public int    seconds;
      public char   ind;
      public int    off_hour;
      public int    off_minutes;
  };
  
  [StructLayout(LayoutKind.Sequential)]
  public struct HPdfTextWidth {
      public uint numchars;
  
      /* don't use this value (it may be change in the feature).
         use numspace as alternated. */
      public uint numwords;
  
      public uint width;
      public uint numspace;
  }
  
  [StructLayout(LayoutKind.Sequential)]
  public struct HPdfTransMatrix {
      public float    a;
      public float    b;
      public float    c;
      public float    d;
      public float    x;
      public float    y;
  };
  
  [StructLayout(LayoutKind.Sequential)]
  public struct HPdfDashMode_Internal {
      public ushort  ptn0;
      public ushort  ptn1;
      public ushort  ptn2;
      public ushort  ptn3;
      public ushort  ptn4;
      public ushort  ptn5;
      public ushort  ptn6;
      public ushort  ptn7;
      public uint    num_ptn;
      public uint    phase;
  };
  
  public struct HPdfDashMode {
      public ushort[]  ptn;
      public uint      phase;
  };
  
  [StructLayout(LayoutKind.Sequential)]
  public struct HPdfRGBColor {
      public float   r;
      public float   g;
      public float   b;
  };
  
  [StructLayout(LayoutKind.Sequential)]
  public struct HPdfCMYKColor {
      public float   c;
      public float   y;
      public float   m;
      public float   k;
  };
  
  
  /* error handler (call back function) */
  public delegate void HPDF_ErrorHandler(uint error_no, uint detail_no,
          IntPtr user_data);
  
  public class HPdfDoc: IDisposable {
      public const int HPDF_TRUE = 1;
      public const int HPDF_FALSE = 0;
      public const uint HPDF_COMP_NONE = 0;
      public const uint HPDF_COMP_TEXT = 1;
      public const uint HPDF_COMP_IMAGE = 2;
      public const uint HPDF_COMP_METADATA = 4;
      public const uint HPDF_COMP_ALL = 15;
  
      public const uint HPDF_HIDE_TOOLBAR = 1;
      public const uint HPDF_HIDE_MENUBAR = 2;
      public const uint HPDF_HIDE_WINDOW_UI = 4;
      public const uint HPDF_FIT_WINDOW = 8;
      public const uint HPDF_CENTER_WINDOW = 16;
  
      public const uint HPDF_ENABLE_READ = 0;
      public const uint HPDF_ENABLE_PRINT = 4;
      public const uint HPDF_ENABLE_EDIT_ALL = 8;
      public const uint HPDF_ENABLE_COPY = 16;
      public const uint HPDF_ENABLE_EDIT = 32;
  
  
      [DllImport("libhpdf.dll")]
      private extern static string HPDF_GetVersion();
  
      [DllImport("libhpdf.dll")]
      private extern static IntPtr HPDF_New(HPDF_ErrorHandler user_error_fn,
              IntPtr user_data);
  
      [DllImport("libhpdf.dll")]
      private extern static void HPDF_Free(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_NewDoc(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_FreeDoc(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_FreeDocAll(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_HasDoc(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_SaveToFile(IntPtr pdf, string file_name);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_GetError(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_GetErrorDetail(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static void HPDF_ResetError(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_SetPagesConfiguration(IntPtr pdf,
              uint page_per_pages);
  
      [DllImport("libhpdf.dll")]
      private extern static IntPtr HPDF_GetPageByIndex(IntPtr pdf,
              uint index);
  
      [DllImport("libhpdf.dll")]
      private extern static HPdfPageLayout HPDF_GetPageLayout(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_SetPageLayout(IntPtr pdf,
              HPdfPageLayout layout);
  
      [DllImport("libhpdf.dll")]
      private extern static HPdfPageMode HPDF_GetPageMode(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_SetPageMode(IntPtr pdf, HPdfPageMode mode);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_SetOpenAction(IntPtr pdf,
              IntPtr open_action);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_GetViewerPreference(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_SetViewerPreference(IntPtr pdf, uint value);
  
      [DllImport("libhpdf.dll")]
      private extern static IntPtr HPDF_GetCurrentPage(IntPtr  pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static IntPtr HPDF_AddPage(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static IntPtr HPDF_InsertPage(IntPtr pdf,
                      IntPtr  page);
  
      [DllImport("libhpdf.dll")]
      private extern static IntPtr HPDF_GetFont(IntPtr pdf, string font_name,
                      string encoding_name);
  
      [DllImport("libhpdf.dll")]
      private extern static string HPDF_LoadType1FontFromFile(IntPtr pdf,
                      string afmfilename, string pfmfilename);
  
      [DllImport("libhpdf.dll")]
      private extern static string HPDF_LoadTTFontFromFile(IntPtr pdf,
                      string file_name, int embedding);
  
      [DllImport("libhpdf.dll")]
      private extern static string HPDF_LoadTTFontFromFile2(IntPtr pdf,
                      string file_name, uint index, int embedding);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_AddPageLabel(IntPtr pdf, uint page_num,
                    HPdfPageNumStyle style, uint first_page, string prefix);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_UseJPFonts(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_UseKRFonts(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_UseCNSFonts(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_UseCNTFonts(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static IntPtr HPDF_CreateOutline(IntPtr pdf, IntPtr parent,
                      string title, IntPtr encoder);
  
      [DllImport("libhpdf.dll")]
      private extern static IntPtr HPDF_GetEncoder(IntPtr pdf,
                      string encoding_name);
  
      [DllImport("libhpdf.dll")]
      private extern static IntPtr HPDF_GetCurrentEncoder(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_SetCurrentEncoder(IntPtr pdf,
                      string encoding_name);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_UseJPEncodings(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_UseKREncodings(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_UseCNSEncodings(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_UseCNTEncodings(IntPtr pdf);
  
      [DllImport("libhpdf.dll")]
      private extern static IntPtr HPDF_LoadPngImageFromFile(IntPtr pdf,
                      string filename);
  
      [DllImport("libhpdf.dll")]
      private extern static IntPtr HPDF_LoadPngImageFromFile2(IntPtr pdf,
                      string filename);
  
      [DllImport("libhpdf.dll")]
      private extern static IntPtr HPDF_LoadJpegImageFromFile(IntPtr pdf,
                      string filename);
  
      [DllImport("libhpdf.dll")]
      private extern static IntPtr HPDF_LoadRawImageFromFile(IntPtr pdf,
                      string filename, uint width, uint height,
                      HPdfColorSpace color_space);
  
      [DllImport("libhpdf.dll")]
      private extern static IntPtr HPDF_LoadRawImageFromMem(IntPtr pdf,
                      byte[] data, int width, int height,
                      HPdfColorSpace color_space,
                      uint bits_per_component);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_SetInfoAttr(IntPtr pdf,
                      HPdfInfoType type, string value);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_SetInfoDateAttr(IntPtr pdf,
                      HPdfInfoType type, HPdfDate value);
  
      [DllImport("libhpdf.dll")]
      private extern static string HPDF_GetInfoAttr(IntPtr pdf,
                      HPdfInfoType type);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_SetPassword(IntPtr pdf,
                      string owner_passwd, string user_passwd);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_SetPermission(IntPtr pdf,
                      uint permission);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_SetEncryptionMode(IntPtr pdf,
                      HPdfEncryptMode mode, uint key_len);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_SetCompressionMode(IntPtr pdf,
                      uint mode);
  
      [DllImport("libhpdf.dll")]
      private extern static IntPtr HPDF_CreateExtGState(IntPtr pdf);
  
      // handle to an instance of a HPdfDoc object.
      private IntPtr hpdf;
  
      public HPdfDoc() {
          HPDF_ErrorHandler error_handler =
                  new HPDF_ErrorHandler(HPdfDoc.ErrorProc);
          hpdf = HPDF_New(error_handler, IntPtr.Zero);
          if (hpdf == IntPtr.Zero) {
              throw new Exception("cannot create HPdfDoc object.");
          }
      }
  
      void IDisposable.Dispose() {
          if (hpdf != IntPtr.Zero) {
              HPDF_Free(hpdf);
          }
  
          hpdf = IntPtr.Zero;
      }
  
      ~HPdfDoc() {
          if (hpdf != IntPtr.Zero) {
              HPDF_Free(hpdf);
          }
      }
  
      public static string HPdfGetVersion() {
          return HPDF_GetVersion();
      }
  
      public static void ErrorProc(uint error_no, uint detail_no,
              IntPtr user_data) {
          string s = "error-code=0x" + error_no.ToString("X") + " detail-code=" + detail_no;
          throw new Exception(s);
      }
  
      public void NewDoc() {
          HPDF_NewDoc(hpdf);
      }
  
      public void FreeDoc() {
          HPDF_FreeDoc(hpdf);
      }
  
      public void FreeDocAll() {
          HPDF_FreeDocAll(hpdf);
      }
  
      public bool HasDoc() {
          return (HPDF_HasDoc(hpdf) != 0);
      }
  
      public void SaveToFile(string file_name) {
          HPDF_SaveToFile(hpdf, file_name);
      }
  
      public uint GetError() {
          return HPDF_GetError(hpdf);
      }
  
      public uint GetErrorDetail() {
          return HPDF_GetErrorDetail(hpdf);
      }
  
      public void ResetError() {
          HPDF_ResetError(hpdf);
      }
  
      public void SetPagesConfiguration(uint page_per_pages) {
          HPDF_SetPagesConfiguration(hpdf, page_per_pages);
      }
  
      public HPdfPage GetPageByIndex(uint index) {
          IntPtr hpage;
  
          hpage = HPDF_GetPageByIndex(hpdf, index);
          return new HPdfPage(hpage);
      }
  
      public HPdfPageLayout GetPageLayout() {
          return HPDF_GetPageLayout(hpdf);
      }
  
      public void SetPageLayout(HPdfPageLayout layout) {
          HPDF_SetPageLayout(hpdf, layout);
      }
  
      public HPdfPageMode GetPageMode() {
          return HPDF_GetPageMode(hpdf);
      }
  
      public void SetPageMode(HPdfPageMode mode) {
          HPDF_SetPageMode(hpdf, mode);
      }
  
      public void SetOpenAction(HPdfDestination open_action) {
          if (open_action == null) {
              // ERROR!!
          } else {
              HPDF_SetOpenAction(hpdf, open_action.GetHandle());
          }
      }
  
      public uint GetViewerPreference() {
          return HPDF_GetViewerPreference(hpdf);
      }
  
      public void SetViewerPreference(uint value) {
          HPDF_SetViewerPreference(hpdf, value);
      }
  
      public HPdfPage GetCurrentPage() {
          IntPtr hpage;
  
          hpage = HPDF_GetCurrentPage(hpdf);
          return new HPdfPage(hpage);
      }
  
      public HPdfPage AddPage() {
          IntPtr hpage;
  
          hpage = HPDF_AddPage(hpdf);
          return new HPdfPage(hpage);
      }
  
      public HPdfPage InsertPage(HPdfPage page) {
          IntPtr hpage;
  
          hpage = HPDF_InsertPage(hpdf, page.GetHandle());
          return new HPdfPage(hpage);
      }
  
      public HPdfFont GetFont(string font_name, string encoding_name) {
          IntPtr hfont;
  
          hfont = HPDF_GetFont(hpdf, font_name, encoding_name);
          return new HPdfFont(hfont);
      }
  
      public string LoadType1FontFromFile(string afmfilename,
              string pfmfilename) {
          string font_name;
  
          font_name = HPDF_LoadType1FontFromFile(hpdf, afmfilename, pfmfilename);
          return font_name;
      }
  
      public string LoadTTFontFromFile(string file_name, bool embedding) {
          string font_name;
          int emb;
  
          if (embedding)
              emb = HPDF_TRUE;
          else
              emb = HPDF_FALSE;
  
          font_name = HPDF_LoadTTFontFromFile(hpdf, file_name, emb);
          return font_name;
      }
  
      public string LoadTTFontFromFile2(string file_name, uint index,
              bool embedding) {
          string font_name;
          int emb;
  
          if (embedding)
              emb = HPDF_TRUE;
          else
              emb = HPDF_FALSE;
  
          font_name = HPDF_LoadTTFontFromFile2(hpdf, file_name, index, emb);
          return font_name;
      }
  
      public void AddPageLabel(uint page_num, HPdfPageNumStyle style,
              uint first_page, string prefix) {
          HPDF_AddPageLabel(hpdf, page_num, style, first_page, prefix);
      }
  
      public uint UseJPFonts() {
          return HPDF_UseJPFonts(hpdf);
      }
  
      public uint UseKRFonts() {
          return HPDF_UseKRFonts(hpdf);
      }
  
      public uint UseCNSFonts() {
          return HPDF_UseCNSFonts(hpdf);
      }
  
      public uint UseCNTFonts() {
          return HPDF_UseCNTFonts(hpdf);
      }
  
      public HPdfOutline CreateOutline(HPdfOutline parent, string title,
              HPdfEncoder encoder) {
          IntPtr hparent;
          IntPtr hencoder;
          IntPtr houtline;
  
          if (encoder != null)
              hencoder = encoder.GetHandle();
          else
              hencoder = IntPtr.Zero;
  
          if (parent != null)
              hparent = parent.GetHandle();
          else
              hparent = IntPtr.Zero;
  
          houtline = HPDF_CreateOutline(hpdf, hparent, title, hencoder);
          return new HPdfOutline(houtline);
      }
  
      public HPdfEncoder GetEncoder(string encoding_name) {
          IntPtr hencoder;
  
          hencoder = HPDF_GetEncoder(hpdf, encoding_name);
  
          return new HPdfEncoder(hencoder);
      }
  
      public HPdfEncoder GetCurrentEncoder() {
          IntPtr hencoder;
  
          hencoder = HPDF_GetCurrentEncoder(hpdf);
  
          return new HPdfEncoder(hencoder);
      }
  
      public void SetCurrentEncoder(string encoding_name) {
          HPDF_SetCurrentEncoder(hpdf, encoding_name);
      }
  
      public uint UseJPEncodings() {
          return HPDF_UseJPEncodings(hpdf);
      }
  
      public uint UseKREncodings() {
          return HPDF_UseKREncodings(hpdf);
      }
  
      public uint UseCNSEncodings() {
          return HPDF_UseCNSEncodings(hpdf);
      }
  
      public uint UseCNTEncodings() {
          return HPDF_UseCNTEncodings(hpdf);
      }
  
      public HPdfImage LoadPngImageFromFile(string filename) {
          IntPtr hobj;
  
          hobj = HPDF_LoadPngImageFromFile(hpdf, filename);
  
          return new HPdfImage(hobj);
      }
  
      public HPdfImage LoadPngImageFromFile2(string filename) {
          IntPtr hobj;
  
          hobj = HPDF_LoadPngImageFromFile2(hpdf, filename);
  
          return new HPdfImage(hobj);
      }
  
      public HPdfImage LoadJpegImageFromFile(string filename) {
          IntPtr hobj;
  
          hobj = HPDF_LoadJpegImageFromFile(hpdf, filename);
  
          return new HPdfImage(hobj);
      }
  
      public HPdfImage LoadRawImageFromFile(string filename,
                      uint width, uint height,
                      HPdfColorSpace color_space) {
          IntPtr hobj;
  
          hobj = HPDF_LoadRawImageFromFile(hpdf, filename, width, height,
                      color_space);
  
          return new HPdfImage(hobj);
      }
  
      public HPdfImage LoadRawImageFromMem(byte[] data, int width, int height,
                      HPdfColorSpace color_space, uint bits_per_component) {
          if (width * height < data.Length) {
              string s = "error-code=0x1030 detail-code=0";
              throw new Exception(s);
          }
          IntPtr hobj;
  
          hobj = HPDF_LoadRawImageFromMem(hpdf, data, width, height, color_space,
              bits_per_component);
  
          return new HPdfImage(hobj);
      }
  
      public void SetInfoAttr(HPdfInfoType type, string value) {
          HPDF_SetInfoAttr(hpdf, type, value);
      }
  
      public void SetInfoDateAttr(HPdfInfoType type, HPdfDate value) {
          HPDF_SetInfoDateAttr(hpdf, type, value);
      }
  
      public string GetInfoAttr(HPdfInfoType type) {
          return HPDF_GetInfoAttr(hpdf, type);
      }
  
      public void SetPassword(string owner_passwd, string user_passwd) {
          HPDF_SetPassword(hpdf, owner_passwd, user_passwd);
      }
  
      public void SetPermission(uint permission) {
          HPDF_SetPermission(hpdf, permission);
      }
  
      public void SetEncryptionMode(HPdfEncryptMode mode, uint key_len) {
          HPDF_SetEncryptionMode(hpdf, mode, key_len);
      }
  
      public void SetCompressionMode(uint mode) {
          HPDF_SetCompressionMode(hpdf, mode);
      }
  
      public HPdfExtGState CreateExtGState() {
          IntPtr hgstate;
  
          hgstate = HPDF_CreateExtGState(hpdf);
  
          return new HPdfExtGState(hgstate);
      }
  }
  
  public class HPdfPage{
      public const int HPDF_TRUE = 1;
      public const int HPDF_FALSE = 0;
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetWidth(IntPtr page, float value);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetHeight(IntPtr page, float value);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetSize(IntPtr page,
              HPdfPageSizes size, HPdfPageDirection direction);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetRotate(IntPtr page, ushort angle);
  
      [DllImport("libhpdf.dll")]
      private extern static IntPtr HPDF_Page_CreateDestination(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static IntPtr HPDF_Page_CreateTextAnnot(IntPtr page,
          HPdfRect rect, string text, IntPtr encoder);
  
      [DllImport("libhpdf.dll")]
      private extern static IntPtr HPDF_Page_CreateLinkAnnot(IntPtr page,
          HPdfRect rect, IntPtr dst);
  
      [DllImport("libhpdf.dll")]
      private extern static IntPtr HPDF_Page_CreateURILinkAnnot(IntPtr page,
          HPdfRect rect, string url);
  
      [DllImport("libhpdf.dll")]
      private extern static float HPDF_Page_TextWidth(IntPtr page,
          string text);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_MeasureText(IntPtr page,
          string text, float width, int wordwrap, ref float real_width);
  
      [DllImport("libhpdf.dll")]
      private extern static float HPDF_Page_GetWidth(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static float HPDF_Page_GetHeight(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static ushort HPDF_Page_GetGMode(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static HPdfPoint HPDF_Page_GetCurrentPos(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static HPdfPoint HPDF_Page_GetCurrentTextPos(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static IntPtr HPDF_Page_GetCurrentFont(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static float HPDF_Page_GetCurrentFontSize(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static HPdfTransMatrix HPDF_Page_GetTransMatrix(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static float HPDF_Page_GetLineWidth(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static HPdfLineCap HPDF_Page_GetLineCap(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static HPdfLineJoin HPDF_Page_GetLineJoin(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static float HPDF_Page_GetMiterLimit(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static HPdfDashMode_Internal HPDF_Page_GetDash(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static float HPDF_Page_GetFlat(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static float HPDF_Page_GetCharSpace(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static float HPDF_Page_GetWordSpace(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static float HPDF_Page_GetHorizontalScalling(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static float HPDF_Page_GetTextLeading(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static HPdfTextRenderingMode HPDF_Page_GetTextRenderingMode(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static float HPDF_Page_GetTextRaise(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static HPdfRGBColor HPDF_Page_GetRGBFill(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static HPdfRGBColor HPDF_Page_GetRGBStroke(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static HPdfCMYKColor HPDF_Page_GetCMYKFill(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static HPdfCMYKColor HPDF_Page_GetCMYKStroke(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static float HPDF_Page_GetGrayFill(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static float HPDF_Page_GetGrayStroke(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static HPdfColorSpace HPDF_Page_GetStrokingColorSpace(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static HPdfColorSpace HPDF_Page_GetFillingColorSpace(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static HPdfTransMatrix HPDF_Page_GetTextMatrix(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_GetGStateDepth(IntPtr page);
  
  /*--- General graphics state -----------------------------------------------*/
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetLineWidth(IntPtr page,
                  float line_width);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetLineCap(IntPtr page,
                  HPdfLineCap line_cap);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetLineJoin(IntPtr page,
                  HPdfLineJoin line_join);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetMiterLimit(IntPtr page,
                  float miter_limit);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetDash(IntPtr page,
                  ushort[] array, uint num_param, uint phase);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetFlat(IntPtr page,
                  float flatness);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetExtGState(IntPtr page,
                  IntPtr ext_gstate);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_GSave(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_GRestore(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_Concat(IntPtr page,
              float a, float b, float c, float d, float x, float y);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_MoveTo(IntPtr page,
              float x, float y);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_LineTo(IntPtr page,
              float x, float y);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_CurveTo(IntPtr page,
              float x1, float y1, float x2, float y2, float x3, float y3);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_CurveTo2(IntPtr page,
              float x2, float y2, float x3, float y3);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_CurveTo3(IntPtr page,
              float x1, float y1, float x3, float y3);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_ClosePath(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_Rectangle(IntPtr page,
              float x, float y, float width, float height);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_Stroke(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_ClosePathStroke(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_Fill(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_Eofill(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_FillStroke(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_EofillStroke(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_ClosePathFillStroke(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_ClosePathEofillStroke(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_EndPath(IntPtr page);
  
  /*--- Clipping paths operator --------------------------------------------*/
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_Clip(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_Eoclip(IntPtr page);
  
  /*--- Text object operator -----------------------------------------------*/
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_BeginText(IntPtr page);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_EndText(IntPtr page);
  
  /*--- Text state ---------------------------------------------------------*/
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetCharSpace(IntPtr page,
                  float value);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetWordSpace(IntPtr page,
                  float value);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetHorizontalScalling(IntPtr page,
                  float value);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetTextLeading(IntPtr page,
                  float value);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetFontAndSize(IntPtr page,
                  IntPtr hfont, float size);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetTextRenderingMode(IntPtr page,
                  HPdfTextRenderingMode  mode);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetTextRaise(IntPtr page,
                  float value);
  
  /*--- Text positioning ---------------------------------------------------*/
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_MoveTextPos(IntPtr page,
              float x, float y);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_MoveTextPos2(IntPtr page,
              float x, float y);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetTextMatrix(IntPtr page,
              float a, float b, float c, float d, float x, float y);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_MoveToNextLine(IntPtr page);
  
  /*--- Text showing -------------------------------------------------------*/
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_ShowText(IntPtr page,
                  string text);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_ShowTextNextLine(IntPtr page,
                  string text);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_ShowTextNextLineEx(IntPtr page,
                  float word_space, float char_space, string text);
  
  /*--- Color showing ------------------------------------------------------*/
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetGrayFill(IntPtr page,
                  float gray);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetGrayStroke(IntPtr page,
                  float gray);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetRGBFill(IntPtr page,
                  float r, float g, float b);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetRGBStroke(IntPtr page,
                  float r, float g, float b);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetCMYKFill(IntPtr page,
                  float c, float m, float y, float k);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetCMYKStroke(IntPtr page,
                  float c, float m, float y, float k);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_ExecuteXObject(IntPtr page,
                  IntPtr obj);
  
  /*---------------------------------------------------------------------*/
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_DrawImage(IntPtr page,
                  IntPtr image, float x, float y, float width, float height);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_Circle(IntPtr page,
                  float x, float y, float ray);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_Arc(IntPtr page,
                  float x, float y, float ray, float ang1, float ang2);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_Ellipse(IntPtr page,
                  float x, float y, float xray, float yray);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_TextOut(IntPtr page,
                  float xpos, float ypos, string text);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_TextRect(IntPtr page,
                  float left, float top, float right, float bottom,
                  string text, HPdfTextAlignment align, ref uint len);
  
      [DllImport("libhpdf.dll")]
      private extern static uint HPDF_Page_SetSlideShow(IntPtr page,
                  HPdfTransitionStyle type, float disp_time, float trans_time);
  
  
      // handle to an instance of a HPDF_Doc object.
      private IntPtr hpage;
  
      public HPdfPage(IntPtr hpage) {
          if (hpage == IntPtr.Zero) {
              throw new Exception("cannot create HPdfPage object.");
          }
  
          this.hpage = hpage;
      }
  
      public void SetWidth(float value) {
          HPDF_Page_SetWidth(hpage, value);
      }
  
      public void SetHeight(float value) {
          HPDF_Page_SetHeight(hpage, value);
      }
  
      public void SetSize(HPdfPageSizes size,
              HPdfPageDirection direction) {
          HPDF_Page_SetSize(hpage, size, direction);
      }
  
      public void SetRotate(ushort angle) {
          HPDF_Page_SetRotate(hpage, angle);
      }
  
      public HPdfDestination CreateDestination() {
          IntPtr hdest;
  
          hdest = HPDF_Page_CreateDestination(hpage);
          return new HPdfDestination(hdest);
      }
  
      public HPdfAnnotation CreateTextAnnot(HPdfRect rect, string text,
              HPdfEncoder encoder) {
          IntPtr hannot;
  
          hannot = HPDF_Page_CreateTextAnnot(hpage, rect, text, encoder.GetHandle());
          return new HPdfAnnotation(hannot);
      }
  
      public HPdfAnnotation CreateLinkAnnot(HPdfRect rect, HPdfDestination dst) {
          IntPtr hannot;
  
          hannot = HPDF_Page_CreateLinkAnnot(hpage, rect, dst.GetHandle());
          return new HPdfAnnotation(hannot);
      }
  
      public HPdfAnnotation CreateURILinkAnnot(HPdfRect rect, string uri) {
          IntPtr hannot;
  
          hannot = HPDF_Page_CreateURILinkAnnot(hpage, rect, uri);
          return new HPdfAnnotation(hannot);
      }
  
  
      public float TextWidth(string text) {
          return HPDF_Page_TextWidth(hpage, text);
      }
  
      public uint MeasureText(string text, float width, bool wordwrap,
                  ref float real_width) {
          int ww;
  
          if (wordwrap)
              ww = HPDF_TRUE;
          else
              ww = HPDF_FALSE;
  
          return HPDF_Page_MeasureText(hpage, text, width, ww, ref real_width);
      }
  
      public uint MeasureText(string text, float width, bool wordwrap) {
          float dummy = 0;
          int ww;
  
          if (wordwrap)
              ww = HPDF_TRUE;
          else
              ww = HPDF_FALSE;
  
  
gt:         return HPDF_Page_MeasureText(hpage, text, width, ww, ref dummy);