继续,这节我们实现了,c#语言对PDF文件进行分割、把PDF转png图片和PDF打印和预览三项功能。
8. 分割功能
单击分割按钮,或者在导航图片上选择右键,选择分割弹出插入设置对话框,点击确定即可完成插入功能。功能如下:
A.分割设置页面
先看一下分割页面,如下:
设计代码:
namespace MyPdf
{
partial class SpliteForm
{
/// <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.WxtsLB = new System.Windows.Forms.Label();
this.Tslb = new System.Windows.Forms.Label();
this.SpiteBT = new System.Windows.Forms.Button();
this.CancleBT = new System.Windows.Forms.Button();
this.fengeLB = new System.Windows.Forms.Label();
this.SpliteTXT = new System.Windows.Forms.TextBox();
this.infoLB = new System.Windows.Forms.Label();
this.SelectLB = new System.Windows.Forms.Label();
this.SlectFileBT = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// WxtsLB
//
this.WxtsLB.AutoSize = true;
this.WxtsLB.Location = new System.Drawing.Point(99, 65);
this.WxtsLB.Name = "WxtsLB";
this.WxtsLB.Size = new System.Drawing.Size(493, 15);
this.WxtsLB.TabIndex = 13;
this.WxtsLB.Text = "分割可以进行如下设置:1-3,4-5;表示1至3为一个文件,4至5为一个文件";
//
// Tslb
//
this.Tslb.AutoSize = true;
this.Tslb.ForeColor = System.Drawing.Color.Red;
this.Tslb.Location = new System.Drawing.Point(22, 65);
this.Tslb.Name = "Tslb";
this.Tslb.Size = new System.Drawing.Size(82, 15);
this.Tslb.TabIndex = 12;
this.Tslb.Text = "温馨提示:";
//
// SpiteBT
//
this.SpiteBT.AutoSize = true;
this.SpiteBT.Location = new System.Drawing.Point(620, 151);
this.SpiteBT.Name = "SpiteBT";
this.SpiteBT.Size = new System.Drawing.Size(77, 25);
this.SpiteBT.TabIndex = 11;
this.SpiteBT.Text = "确认分割";
this.SpiteBT.UseVisualStyleBackColor = true;
this.SpiteBT.Click += new System.EventHandler(this.SpiteBT_Click);
//
// CancleBT
//
this.CancleBT.AutoSize = true;
this.CancleBT.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.CancleBT.Location = new System.Drawing.Point(407, 151);
this.CancleBT.Name = "CancleBT";
this.CancleBT.Size = new System.Drawing.Size(77, 25);
this.CancleBT.TabIndex = 10;
this.CancleBT.Text = "取消删除";
this.CancleBT.UseVisualStyleBackColor = true;
//
// fengeLB
//
this.fengeLB.AutoSize = true;
this.fengeLB.Location = new System.Drawing.Point(149, 18);
this.fengeLB.Name = "fengeLB";
this.fengeLB.Size = new System.Drawing.Size(82, 15);
this.fengeLB.TabIndex = 16;
this.fengeLB.Text = "分割设置:";
//
// SpliteTXT
//
this.SpliteTXT.Location = new System.Drawing.Point(229, 15);
this.SpliteTXT.Name = "SpliteTXT";
this.SpliteTXT.Size = new System.Drawing.Size(540, 25);
this.SpliteTXT.TabIndex = 17;
this.SpliteTXT.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.SpliteTXT_KeyPress);
//
// infoLB
//
this.infoLB.AutoSize = true;
this.infoLB.Location = new System.Drawing.Point(22, 18);
this.infoLB.Name = "infoLB";
this.infoLB.Size = new System.Drawing.Size(121, 15);
this.infoLB.TabIndex = 18;
this.infoLB.Text = "本文档共300页码";
//
// SelectLB
//
this.SelectLB.AutoSize = true;
this.SelectLB.Location = new System.Drawing.Point(165, 111);
this.SelectLB.Name = "SelectLB";
this.SelectLB.Size = new System.Drawing.Size(0, 15);
this.SelectLB.TabIndex = 19;
//
// SlectFileBT
//
this.SlectFileBT.AutoSize = true;
this.SlectFileBT.Location = new System.Drawing.Point(25, 107);
this.SlectFileBT.Name = "SlectFileBT";
this.SlectFileBT.Size = new System.Drawing.Size(125, 25);
this.SlectFileBT.TabIndex = 20;
this.SlectFileBT.Text = "选择输出路径";
this.SlectFileBT.UseVisualStyleBackColor = true;
this.SlectFileBT.Click += new System.EventHandler(this.SlectFileBT_Click);
//
// SpliteForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.ClientSize = new System.Drawing.Size(792, 218);
this.Controls.Add(this.SlectFileBT);
this.Controls.Add(this.SelectLB);
this.Controls.Add(this.infoLB);
this.Controls.Add(this.SpliteTXT);
this.Controls.Add(this.fengeLB);
this.Controls.Add(this.WxtsLB);
this.Controls.Add(this.Tslb);
this.Controls.Add(this.SpiteBT);
this.Controls.Add(this.CancleBT);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Name = "SpliteForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "PDF分割";
this.Load += new System.EventHandler(this.SpliteForm_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label WxtsLB;
private System.Windows.Forms.Label Tslb;
private System.Windows.Forms.Button SpiteBT;
private System.Windows.Forms.Button CancleBT;
private System.Windows.Forms.Label fengeLB;
private System.Windows.Forms.TextBox SpliteTXT;
private System.Windows.Forms.Label infoLB;
private System.Windows.Forms.Label SelectLB;
private System.Windows.Forms.Button SlectFileBT;
}
}
功能实现代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
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 SpliteForm : Form
{
public String SpliteOption,SavePath;
private int CurrentPage,totalPage;
public SpliteForm()
{
InitializeComponent();
}
public SpliteForm(int Currentpage, int Totalpage)
{
InitializeComponent();
CurrentPage = Currentpage;
totalPage = Totalpage;
}
private void SpiteBT_Click(object sender, EventArgs e)
{
if (SpliteTXT.Text.Trim() == "")
{
MessageBox.Show("分割设置不能为空!");
SpliteTXT.Focus();
return;
}
if (SelectLB.Text == "")
{
MessageBox.Show("请选择保存路径!");
SpliteTXT.Focus();
return;
}
SpliteOption = SpliteTXT.Text.Trim();
this.DialogResult = DialogResult.OK;
this.Close();
}
private void SpliteForm_Load(object sender, EventArgs e)
{
infoLB.Text = "本文档共"+totalPage+"页";
if (totalPage==2)
{
SpliteTXT.Text = "1,2";
return;
}
if (CurrentPage==1)
{
SpliteTXT.Text = "1,2-" + totalPage.ToString();
}
else if (CurrentPage==totalPage)
{
SpliteTXT.Text = "1-"+(totalPage-1).ToString()+","+totalPage.ToString();
}
else
{
SpliteTXT.Text = "1-" + CurrentPage.ToString() + ","+(CurrentPage+1).ToString()+"-" + totalPage.ToString();
}
if (BaseClass.GetAppConfig("last") != "")
{
SelectLB.Text = BaseClass.GetAppConfig("last");
SavePath = BaseClass.GetAppConfig("last");
}
}
private void SlectFileBT_Click(object sender, EventArgs e)
{
using (var form = new FolderBrowserDialog())
{
if (BaseClass.GetAppConfig("last") != "")
{
form.SelectedPath = BaseClass.GetAppConfig("last");
}
if (form.ShowDialog(this) != DialogResult.OK)
return;
BaseClass.SetAppConfig("last", form.SelectedPath);
SavePath = form.SelectedPath;
SelectLB.Text = SavePath;
}
}
private void SpliteTXT_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsNumber(e.KeyChar) && e.KeyChar != '\b' && e.KeyChar != ',' && e.KeyChar != '-')
{
e.Handled = true;
}
}
}
}
B. 主页面
设计代码
//
// FenGeBT_Tool
//
this.FenGeBT_Tool.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.FenGeBT_Tool.Enabled = false;
this.FenGeBT_Tool.Image = ((System.Drawing.Image)(resources.GetObject("FenGeBT_Tool.Image")));
this.FenGeBT_Tool.ImageTransparentColor = System.Drawing.Color.Magenta;
this.FenGeBT_Tool.Name = "FenGeBT_Tool";
this.FenGeBT_Tool.Size = new System.Drawing.Size(29, 25);
this.FenGeBT_Tool.Text = "分割Pdf";
this.FenGeBT_Tool.Click += new System.EventHandler(this.FenGeBT_Tool_Click);
功能实现代码:
/// <summary>
/// PDF分割
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FenGeBT_Tool_Click(object sender, EventArgs e)
{
OpenSplite();
}
/// <summary>
/// 打开PDF分割操作界面
/// </summary>
private void OpenSplite()
{
if (GuidMenuePdf.Document.PageCount == 1)
{
MessageBox.Show("共1页文档,不需分割。");
return;
}
SpliteForm splitePage = new SpliteForm(currentPage + 1, GuidMenuePdf.Document.PageCount);
splitePage.ShowDialog();
if (splitePage.DialogResult == DialogResult.OK)
{
string filename = Path.GetFileNameWithoutExtension(oldPdfPath);
string[] commandParam = splitePage.SpliteOption.Split(',');
for (int i = 0; i < commandParam.Length; i++)
{
SplitePDF(commandParam[i], splitePage.SavePath + "\\" + filename + "_" + i + ".pdf");
}
MessageBox.Show("分割完成");
System.Diagnostics.Process.Start("explorer.exe", splitePage.SavePath);
}
}
9. PDF转图片功能
A、生成图片设置页面
设计代码
using System;
namespace MyPdf
{
partial class ExportBitmapsForm
{
/// <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.label1 = new System.Windows.Forms.Label();
this._dpiXTextBox = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this._dpiYTextBox = new System.Windows.Forms.TextBox();
this._acceptButton = new System.Windows.Forms.Button();
this._cancelButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 15);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(45, 15);
this.label1.TabIndex = 0;
this.label1.Text = "宽度:";
//
// _dpiXTextBox
//
this._dpiXTextBox.Location = new System.Drawing.Point(106, 12);
this._dpiXTextBox.Name = "_dpiXTextBox";
this._dpiXTextBox.Size = new System.Drawing.Size(240, 25);
this._dpiXTextBox.TabIndex = 1;
this._dpiXTextBox.Text = "96";
this._dpiXTextBox.TextChanged += new System.EventHandler(this._dpiX_TextChanged);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 41);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(45, 15);
this.label2.TabIndex = 2;
this.label2.Text = "高度:";
//
// _dpiYTextBox
//
this._dpiYTextBox.Location = new System.Drawing.Point(106, 38);
this._dpiYTextBox.Name = "_dpiYTextBox";
this._dpiYTextBox.Size = new System.Drawing.Size(240, 25);
this._dpiYTextBox.TabIndex = 3;
this._dpiYTextBox.Text = "96";
this._dpiYTextBox.TextChanged += new System.EventHandler(this._dpiY_TextChanged);
//
// _acceptButton
//
this._acceptButton.AutoSize = true;
this._acceptButton.Location = new System.Drawing.Point(190, 64);
this._acceptButton.Name = "_acceptButton";
this._acceptButton.Size = new System.Drawing.Size(75, 25);
this._acceptButton.TabIndex = 4;
this._acceptButton.Text = "确定";
this._acceptButton.UseVisualStyleBackColor = true;
this._acceptButton.Click += new System.EventHandler(this._acceptButton_Click);
//
// _cancelButton
//
this._cancelButton.AutoSize = true;
this._cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this._cancelButton.Location = new System.Drawing.Point(271, 64);
this._cancelButton.Name = "_cancelButton";
this._cancelButton.Size = new System.Drawing.Size(75, 25);
this._cancelButton.TabIndex = 5;
this._cancelButton.Text = "取消";
this._cancelButton.UseVisualStyleBackColor = true;
//
// ExportBitmapsForm
//
this.AcceptButton = this._acceptButton;
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this._cancelButton;
this.ClientSize = new System.Drawing.Size(384, 105);
this.Controls.Add(this._cancelButton);
this.Controls.Add(this._acceptButton);
this.Controls.Add(this._dpiYTextBox);
this.Controls.Add(this.label2);
this.Controls.Add(this._dpiXTextBox);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "ExportBitmapsForm";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "ExportBitmapsForm";
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox _dpiXTextBox;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox _dpiYTextBox;
private System.Windows.Forms.Button _acceptButton;
private System.Windows.Forms.Button _cancelButton;
#endregion
}
}
B、功能实现代码
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 ExportBitmapsForm : Form
{
private int _dpiX;
private int _dpiY;
public int DpiX
{
get { return _dpiX; }
}
public int DpiY
{
get { return _dpiY; }
}
public ExportBitmapsForm()
{
InitializeComponent();
UpdateEnabled();
}
private void _acceptButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
}
private void _dpiX_TextChanged(object sender, EventArgs e)
{
UpdateEnabled();
}
private void _dpiY_TextChanged(object sender, EventArgs e)
{
UpdateEnabled();
}
private void UpdateEnabled()
{
_acceptButton.Enabled =
int.TryParse(_dpiXTextBox.Text, out _dpiX) &&
int.TryParse(_dpiYTextBox.Text, out _dpiY);
}
}
}
2.主页面设置
A、设计代码
// PicBT_Tool
//
this.PicBT_Tool.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.PicBT_Tool.Enabled = false;
this.PicBT_Tool.Image = ((System.Drawing.Image)(resources.GetObject("PicBT_Tool.Image")));
this.PicBT_Tool.ImageTransparentColor = System.Drawing.Color.Magenta;
this.PicBT_Tool.Name = "PicBT_Tool";
this.PicBT_Tool.Size = new System.Drawing.Size(29, 25);
this.PicBT_Tool.Text = "转化成图片";
this.PicBT_Tool.Click += new System.EventHandler(this.PicBT_Tool_Click);
//
B、 功能实现代码
/// <summary>
/// PDF转图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PicBT_Tool_Click(object sender, EventArgs e)
{
int dpiX;
int dpiY;
using (var form = new ExportBitmapsForm())
{
if (form.ShowDialog() != DialogResult.OK)
return;
dpiX = form.DpiX;
dpiY = form.DpiY;
}
string path;
using (var form = new FolderBrowserDialog())
{
if (BaseClass.GetAppConfig("last") != "")
{
form.SelectedPath = BaseClass.GetAppConfig("last");
}
if (form.ShowDialog(this) != DialogResult.OK)
return;
path = form.SelectedPath;
}
var document = ViewerPdf.Document;
string filename= Path.GetFileNameWithoutExtension(oldPdfPath);
for (int i = 0; i < document.PageCount; i++)
{
using (var image = document.Render(i, (int)document.PageSizes[i].Width, (int)document.PageSizes[i].Height, dpiX, dpiY, false))
{
image.Save(Path.Combine(path, filename+"_" + i + ".png"));
}
}
}
10. PDF打印功能
设计代码
// PintfBT_Tool
//
this.PintfBT_Tool.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.PintfBT_Tool.Enabled = false;
this.PintfBT_Tool.Image = ((System.Drawing.Image)(resources.GetObject("PintfBT_Tool.Image")));
this.PintfBT_Tool.ImageTransparentColor = System.Drawing.Color.Magenta;
this.PintfBT_Tool.Name = "PintfBT_Tool";
this.PintfBT_Tool.Size = new System.Drawing.Size(29, 25);
this.PintfBT_Tool.Text = "打印";
this.PintfBT_Tool.Click += new System.EventHandler(this.PintfBT_Tool_Click);
//
功能代码
/// <summary>
/// 打印PDF
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PintfBT_Tool_Click(object sender, EventArgs e)
{
using (var form = new PrintPreviewDialog())
{
form.Document = ViewerPdf.Document.CreatePrintDocument(ViewerPdf.DefaultPrintMode);
form.ShowDialog(this);
}
}
10、保存功能
设计代码
// SaveBT_Tool
//
this.SaveBT_Tool.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.SaveBT_Tool.Enabled = false;
this.SaveBT_Tool.Image = ((System.Drawing.Image)(resources.GetObject("SaveBT_Tool.Image")));
this.SaveBT_Tool.ImageTransparentColor = System.Drawing.Color.Magenta;
this.SaveBT_Tool.Name = "SaveBT_Tool";
this.SaveBT_Tool.Size = new System.Drawing.Size(29, 25);
this.SaveBT_Tool.Text = "保存pdf文件";
this.SaveBT_Tool.Click += new System.EventHandler(this.SaveBT_Tool_Click);
功能实现代码
/// <summary>
/// 保存功能
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SaveBT_Tool_Click(object sender, EventArgs e)
{
using (var sfd = new SaveFileDialog())
{
sfd.Title = "请选择要保存的文件路径";
//sfd.InitialDirectory = Application.StartupPath;
sfd.RestoreDirectory = true;
//设置保存文件的类型
sfd.Filter = "Pdf文件|*.pdf";
if (sfd.ShowDialog() == DialogResult.OK)
{
ViewerPdf.Document.Save(sfd.FileName);
}
ok,这样我们的PDF操作就告一段落。