【c#系列】PDF进行操作-浏览、分割、合并、插入、删除(3)

这节我们继续用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设置界面,如下:

插入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();
        }

    }
}

源代码下载

单击下载源代码

相关连接

  1. 【c#系列】PDF进行操作-浏览、分割、合并、插入、删除(1)
  2. 【c#系列】PDF进行操作-浏览、分割、合并、插入、删除(2)
  3. 【c#系列】PDF进行操作-浏览、分割、合并、插入、删除(3)
  4. 【c#系列】PDF进行操作-浏览、分割、合并、插入、删除(4)完
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

游戏自学

生活不易,打赏随意

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值