using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using AcroPDFLib;//引用COM組件Adobe Acrobat Browser Control Type Library 1.0 //http://www.adobe.com/devnet/acrobat/sdk/eula.html using System.Runtime.InteropServices; namespace WindowsChineseCalender { /// <summary> ///安裝 Adobe Reader X /// winform pdf view control /// Geovin Du 塗聚文 /// 20121022 /// </summary> public partial class PDFviewControlForm : Form { [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern uint GetShortPathName( [MarshalAs(UnmanagedType.LPTStr)] string lpszLongPath, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpszShortPath, uint cchBuffer); /// <summary> /// /// </summary> public PDFviewControlForm() { InitializeComponent(); this.addressLeft.Text = "http://www.dusystem.com/%E4%BD%8F%E6%88%BF%E5%92%8C%E5%9F%8E%E4%B9%A1%E5%BB%BA%E8%AE%BE%E9%83%A8%E7%9D%A3%E5%AF%9F%E5%8A%9E%E5%85%AC%E5%AE%A4201200825.pdf"; } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PDFviewControlForm_Load(object sender, EventArgs e) { } /// <summary> /// 瀏覽本地文件 /// 塗聚文 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void browseButtonLeft_Click(object sender, EventArgs e) { OpenFileDialog openFile = new OpenFileDialog(); DialogResult result = openFile.ShowDialog(); if (result == DialogResult.Cancel) return; string strFileName = openFile.FileName; string strFileNameDup = strFileName; if (strFileNameDup.ToUpper().EndsWith(".PDF")) { addressLeft.Text = strFileName; } else MessageBox.Show(this, "請選擇PDF文件!", "文件類型錯誤", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); } /// <summary> /// 打開文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void goButtonLeft_Click(object sender, EventArgs e) { if (0 == addressLeft.Text.Length) MessageBox.Show(this, "輸入正解的 \"地址\"比 !", "錯誤提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); if (addressLeft.Text.StartsWith("http://")) pdfWindowLeft.src = addressLeft.Text; //網絡文件地址 else { //本地文件 StringBuilder shortFileName = new StringBuilder(4096); uint shortFileNameSize = (uint)shortFileName.Capacity; if (GetShortPathName(addressLeft.Text, shortFileName, shortFileNameSize) != 0) pdfWindowLeft.LoadFile(shortFileName.ToString()); else MessageBox.Show(this, "不正確的文件.", "需正確的文件", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); } } } }