mainform.cs

来自「C#操作pdf,创建PDF文件并能操作文字和图像合成」· CS 代码 · 共 83 行

CS
83
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace PDFWork
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();            
        }

        /// <summary>
        /// 创建PDF
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCreatePdf_Click(object sender, EventArgs e)
        {
            PdfCreator pCreator = new PdfCreator();
            
            try
            {
                this.Cursor = Cursors.WaitCursor;
                pCreator.SuccessEvent += new PdfCreator.SuccessEventHandler(SuccessTip);
                pCreator.CreateImagePDF();
                string strPath = AppDomain.CurrentDomain.BaseDirectory.Replace("bin\\", string.Empty) + "Output\\Demo.pdf";
                if (File.Exists(strPath))
                {
                    labTip.Text = string.Empty;
                    PdfContainer.src = strPath;
                    PdfContainer.Update();

                }
                else
                {
                    labTip.Text = "PDF文件不存在";
                }

                this.Cursor = Cursors.Default;   
            }
            catch
            {
                labTip.Text = "出现内部错误.";
            }                
        }

        /// <summary>
        /// 写入操作日志
        /// </summary>
        /// <param name="strTip"></param>
        public static void SuccessTip(string strTip)
        {
            string path = @"C:\\log.txt";

            if (!File.Exists(path))
            {
                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.WriteLine(strTip);
                    sw.Flush();
                    sw.Close();
                }
            }
            else
            {
                using (StreamWriter sw = File.AppendText(path))
                {
                    sw.WriteLine(strTip);
                    sw.Flush();
                    sw.Close();
                }
            }
        }     
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?