当前位置:   article > 正文

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

c#拆分pdf文档

这节我们继续用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);
            //
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
功能实现代码
        /// <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));
            }
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
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;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156

功能代码如下

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76

源代码下载

单击下载源代码

相关连接

  1. 【c#系列】PDF进行操作-浏览、分割、合并、插入、删除(1)
  2. 【c#系列】PDF进行操作-浏览、分割、合并、插入、删除(2)
  3. 【c#系列】PDF进行操作-浏览、分割、合并、插入、删除(3)
  4. 【c#系列】PDF进行操作-浏览、分割、合并、插入、删除(4)完
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/1018956
推荐阅读
相关标签
  

闽ICP备14008679号