这节我们继续用c#语言对PDF进行操纵,本节主要完成插入功能。
7. 插入功能
单击插入按钮,或者在导航图片上选择右键,选择插入弹出插入设置对话框,点击确定即可完成插入功能。功能如下:
设计代码
1. 主界面设置。
private System.Windows.Forms.ToolStripButton HebingBT_Tool;
this.HebingBT_Tool= new System.Windows.Forms.ToolStripButton();
// HebingBT_Tool
//
this.HebingBT_Tool.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.HebingBT_Tool.Enabled = false;
this.HebingBT_Tool.Image = ((System.Drawing.Image)(resources.GetObject("HebingBT_Tool.Image")));
this.HebingBT_Tool.ImageTransparentColor = System.Drawing.Color.Magenta;
this.HebingBT_Tool.Name = "HebingBT_Tool";
this.HebingBT_Tool.Size = new System.Drawing.Size(29, 25);
this.HebingBT_Tool.Text = "插入Pdf";
this.HebingBT_Tool.Click += new System.EventHandler(this.HebingBT_Tool_Click);
//
功能实现代码
/// <summary>
/// 插入PDF
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void HebingBT_Tool_Click(object sender, EventArgs e)
{
OpenInsert();
}
/// <summary>
/// 打开PDF插入操作界面
/// </summary>
private void OpenInsert()
{
AddForm addPage = new AddForm(currentPage + 1, GuidMenuePdf.Document.PageCount);
addPage.ShowDialog();
if (addPage.DialogResult == DialogResult.OK)
{
StartPdf(AddPage(currentPage + 1, addPage.SelectFile));
}
}
2.插入Pdf设置界面,如下:
界面设置代码
namespace MyPdf
{
partial class AddForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SelectTxtLB = new System.Windows.Forms.Label();
this.SelectBT = new System.Windows.Forms.Button();
this.OKBT = new System.Windows.Forms.Button();
this.CancleBT = new System.Windows.Forms.Button();
this.SelectBoxBT = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.InSertPageTXT = new System.Windows.Forms.TextBox();
this.PdfInfoLB = new System.Windows.Forms.Label();
this.SelectBoxBT.SuspendLayout();
this.SuspendLayout();
//
// SelectTxtLB
//
this.SelectTxtLB.AutoSize = true;
this.SelectTxtLB.Location = new System.Drawing.Point(8, 56);
this.SelectTxtLB.Name = "SelectTxtLB";
this.SelectTxtLB.Size = new System.Drawing.Size(0, 15);
this.SelectTxtLB.TabIndex = 0;
//
// SelectBT
//
this.SelectBT.AutoSize = true;
this.SelectBT.Location = new System.Drawing.Point(483, 66);
this.SelectBT.Name = "SelectBT";
this.SelectBT.Size = new System.Drawing.Size(77, 25);
this.SelectBT.TabIndex = 1;
this.SelectBT.Text = "选择文件";
this.SelectBT.UseVisualStyleBackColor = true;
this.SelectBT.Click += new System.EventHandler(this.SelectBT_Click);
//
// OKBT
//
this.OKBT.AutoSize = true;
this.OKBT.Location = new System.Drawing.Point(495, 138);
this.OKBT.Name = "OKBT";
this.OKBT.Size = new System.Drawing.Size(75, 25);
this.OKBT.TabIndex = 2;
this.OKBT.Text = "确认";
this.OKBT.UseVisualStyleBackColor = true;
this.OKBT.Click += new System.EventHandler(this.OKBT_Click);
//
// CancleBT
//
this.CancleBT.AutoSize = true;
this.CancleBT.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.CancleBT.Location = new System.Drawing.Point(97, 138);
this.CancleBT.Name = "CancleBT";
this.CancleBT.Size = new System.Drawing.Size(75, 25);
this.CancleBT.TabIndex = 3;
this.CancleBT.Text = "取消";
this.CancleBT.UseVisualStyleBackColor = true;
//
// SelectBoxBT
//
this.SelectBoxBT.Controls.Add(this.label1);
this.SelectBoxBT.Controls.Add(this.InSertPageTXT);
this.SelectBoxBT.Controls.Add(this.PdfInfoLB);
this.SelectBoxBT.Controls.Add(this.SelectTxtLB);
this.SelectBoxBT.Controls.Add(this.SelectBT);
this.SelectBoxBT.Location = new System.Drawing.Point(12, 12);
this.SelectBoxBT.Name = "SelectBoxBT";
this.SelectBoxBT.Size = new System.Drawing.Size(630, 109);
this.SelectBoxBT.TabIndex = 4;
this.SelectBoxBT.TabStop = false;
this.SelectBoxBT.Text = "选择插入文件";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(275, 30);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(157, 15);
this.label1.TabIndex = 6;
this.label1.Text = "页后,插入以下文件:";
//
// InSertPageTXT
//
this.InSertPageTXT.Location = new System.Drawing.Point(185, 25);
this.InSertPageTXT.Name = "InSertPageTXT";
this.InSertPageTXT.Size = new System.Drawing.Size(75, 25);
this.InSertPageTXT.TabIndex = 5;
this.InSertPageTXT.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.InSertPageTXT_KeyPress);
//
// PdfInfoLB
//
this.PdfInfoLB.AutoSize = true;
this.PdfInfoLB.Location = new System.Drawing.Point(14, 30);
this.PdfInfoLB.Name = "PdfInfoLB";
this.PdfInfoLB.Size = new System.Drawing.Size(142, 15);
this.PdfInfoLB.TabIndex = 4;
this.PdfInfoLB.Text = "本文档共页,准备在";
//
// AddForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(656, 190);
this.Controls.Add(this.SelectBoxBT);
this.Controls.Add(this.CancleBT);
this.Controls.Add(this.OKBT);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Name = "AddForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "插入新页面";
this.Load += new System.EventHandler(this.AddForm_Load);
this.SelectBoxBT.ResumeLayout(false);
this.SelectBoxBT.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label SelectTxtLB;
private System.Windows.Forms.Button SelectBT;
private System.Windows.Forms.Button OKBT;
private System.Windows.Forms.Button CancleBT;
private System.Windows.Forms.GroupBox SelectBoxBT;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox InSertPageTXT;
private System.Windows.Forms.Label PdfInfoLB;
}
}
功能代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyPdf
{
public partial class AddForm : Form
{
public string SelectFile = "";
public int CurrentPage;
private int totalPage;
public AddForm()
{
InitializeComponent();
}
public AddForm(int Currentpage,int Totalpage)
{
InitializeComponent();
CurrentPage = Currentpage;
totalPage = Totalpage;
}
private void SelectBT_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = "PDF Files (*.pdf)|*.pdf|All Files (*.*)|*.*";
ofd.RestoreDirectory = true;
ofd.Title = "选择合并文件";
if (ofd.ShowDialog(this) != DialogResult.OK)
{
return;
}
SelectFile = SelectTxtLB.Text = ofd.FileName;
}
}
private void AddForm_Load(object sender, EventArgs e)
{
PdfInfoLB.Text = "本文档共"+totalPage+"页,准备在";
InSertPageTXT.Text = CurrentPage.ToString();
}
private void InSertPageTXT_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsNumber(e.KeyChar) && e.KeyChar != '\b')
{
e.Handled = true;
}
}
private void OKBT_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(SelectFile))
{
MessageBox.Show("请选择合并的文件");
return;
}
if (Convert.ToInt32(InSertPageTXT.Text.Trim())>totalPage)
{
InSertPageTXT.Text = totalPage.ToString();
}
this.DialogResult = DialogResult.OK;
this.Close();
}
}
}