using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UdPdfWin
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            //mnuOpenResource_Click(sender, e);
        }

        #region 文件菜单

        private void mnuOpenFile_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == this.openFileDialog.ShowDialog(this))
            {
                System.IO.FileInfo fi = new System.IO.FileInfo(this.openFileDialog.FileName);
                string ext = fi.Extension.ToLower();
                if (ext.CompareTo(".enpdf") == 0)//读取加密过的enpdf文件
                {
                    this.axUdPdf1.Encrypted = true;
                    //this.axUdPdf1.HashKey = "3CE59E850E2C590AFD6E4D3516BA3F1B";//使用加密读取的密码hash值，原密码为：123abc
                    this.axUdPdf1.HashKey = this.axUdPdf1.Hash("123abc");
                }
                else
                {
                    this.axUdPdf1.Encrypted = false;
                }
                //文件先读取到内存中然后加载
                //using (System.IO.FileStream fs = new System.IO.FileStream(this.openFileDialog.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                //{
                //    byte[] buf = new byte[fs.Length];
                //    fs.Read(buf, 0, (int)fs.Length);
                //    this.axUdPdf1.LoadFromData(buf);
                //}

                //直接从文件加载
                this.axUdPdf1.LoadFromFile(this.openFileDialog.FileName);
            }
        }

        private void mnuOpenResource_Click(object sender, EventArgs e)
        {
            string assembleName = this.GetType().Assembly.GetName().Name;
            System.IO.Stream stream =
                this.GetType().Assembly.GetManifestResourceStream(assembleName + ".PDFSample.pdf");

            byte[] buf = new byte[(int)stream.Length];
            stream.Read(buf, 0, buf.Length);
            stream.Close();

            this.axUdPdf1.Encrypted = false;
            this.axUdPdf1.LoadFromData(buf);
        }

        private void mnuPrint_Click(object sender, EventArgs e)
        {
            this.axUdPdf1.Print();
        }

        private void mnuClose_Click(object sender, EventArgs e)
        {
            this.axUdPdf1.Close();
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            this.Close();
            this.mnuBookmark.Enabled = true;
        }

        #endregion

        #region 查看菜单

        private void mnuZoomFitWidth_Click(object sender, EventArgs e)
        {
            this.axUdPdf1.ZoomFitWidth();
        }

        private void mnuZoomFitPage_Click(object sender, EventArgs e)
        {
            this.axUdPdf1.ZoomFitPage();
        }

        private void mnuZoomActual_Click(object sender, EventArgs e)
        {
            this.axUdPdf1.ZoomActual();
        }

        private void mnuZoomIn_Click(object sender, EventArgs e)
        {
            this.axUdPdf1.ZoomIn();
        }

        private void mnuZoomOut_Click(object sender, EventArgs e)
        {
            this.axUdPdf1.ZoomOut();
        }

        private void mnuRotateLeft_Click(object sender, EventArgs e)
        {
            this.axUdPdf1.RotateLeft();
        }

        private void mnuRotateRight_Click(object sender, EventArgs e)
        {
            this.axUdPdf1.RotateRight();
        }


        private void mnuGotoFirstPage_Click(object sender, EventArgs e)
        {
            this.axUdPdf1.GoToFirstPage();
        }

        private void mnuGotoPrevPage_Click(object sender, EventArgs e)
        {
            this.axUdPdf1.GoToPrevPage();
        }

        private void mnuGotoNextPage_Click(object sender, EventArgs e)
        {
            this.axUdPdf1.GoToNextPage();
        }

        private void mnuGotoLastPage_Click(object sender, EventArgs e)
        {
            this.axUdPdf1.GoToLastPage();
        }

        private void mnuToolBar_Click(object sender, EventArgs e)
        {
            this.mnuToolBar.Checked = !this.mnuToolBar.Checked;
            this.axUdPdf1.ToolbarVisible = this.mnuToolBar.Checked;
        }


        private void mnuBookmark_Click(object sender, EventArgs e)
        {
            this.mnuBookmark.Checked = !this.mnuBookmark.Checked;
            this.axUdPdf1.BookmarkVisible = this.mnuBookmark.Checked;
        }

        private void mnuFullScreen_Click(object sender, EventArgs e)
        {
            this.axUdPdf1.ToggleFullScreen();
        }

        #endregion

        #region 安全菜单

        private void mnuAllowCopy_Click(object sender, EventArgs e)
        {
            this.mnuAllowCopy.Checked = !this.mnuAllowCopy.Checked;
            this.axUdPdf1.EnableCopy = this.mnuAllowCopy.Checked;
        }

        private void mnuAllowPrint_Click(object sender, EventArgs e)
        {
            this.mnuAllowPrint.Checked = !this.mnuAllowPrint.Checked;
            this.mnuPrint.Enabled = this.mnuAllowPrint.Checked;
            this.axUdPdf1.EnablePrint = this.mnuAllowPrint.Checked;
        }

        private void mnuAllowCapture_Click(object sender, EventArgs e)
        {
            this.mnuAllowCapture.Checked = !this.mnuAllowCapture.Checked;
            this.axUdPdf1.EnableCapture = this.mnuAllowCapture.Checked;
        }

        //动态水印演示
        int water1 = -1;
        int water2 = -1;
        private void mnuWaterMark_Click(object sender, EventArgs e)
        {
            if (water1 > -1)
            {
                this.axUdPdf1.RemoveWaterMark(water1);
                water1 = -1;
            }
            else
            {
                this.axUdPdf1.SetWaterFont("宋体", 20, 100, true, false, false, false);
                water1 = this.axUdPdf1.AddWaterText("这里是动态添加的水印", 20, 100);
            }

            if (water2 > -1)
            {
                this.axUdPdf1.RemoveWaterMark(water2);
                water2 = -1;
            }
            else
            {
                this.axUdPdf1.SetWaterFont("宋体", 40, 300, false, false, false, false);
                water2 = this.axUdPdf1.AddWaterText("斜体的水印文字", 100, 300, 50, 45, 0, 0);
            }
            this.mnuWaterMark.Checked = !this.mnuWaterMark.Checked;
        }

        #endregion

        private void mnuAbout_Click(object sender, EventArgs e)
        {
            MessageBox.Show("UUDoc PDF Control Sample for Windows\r\n\r\n(C)2013 珠海市优道信息科技有限公司", "关于", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void mnuWebSite_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.uudoc.com");
        }

        private void axUdPdf_OnReady(object sender, EventArgs e)
        {
            this.mnuBookmark.Enabled = this.axUdPdf1.HasBookmark;
            this.mnuBookmark.Checked = this.axUdPdf1.HasBookmark;
            this.axUdPdf1.BookmarkVisible = this.axUdPdf1.HasBookmark;
        }        

    }
}
