基本信息
源码名称:PDF拆分(按照页码拆分成多份)C#源码
源码大小:1.62M
文件格式:.zip
开发语言:C#
更新时间:2019-01-14
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
namespace PDF
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int iPageNum;
string filePath = "";
private void button1_Click(object sender, EventArgs e)
{
if (filePath == "")
{
MessageBox.Show("选择PDF文件无效!", "文件无效!");
}
else
{
if (numericUpDownEndPageNum.Value > iPageNum)
{
MessageBox.Show("最大页码值有误!请检查!", "页码有误!");
}
else if (numericUpDownStartPageNum.Value < 1)
{
MessageBox.Show("最小页码值有误!请检查!", "页码有误!");
}
else if (numericUpDownStartPageNum.Value > numericUpDownEndPageNum.Value)
{
MessageBox.Show("最小页码值比最大页码值大!请检查!", "页码有误!");
}
else
{
string splitPDFPath = filePath.Remove(filePath.Length - 4)
"(" numericUpDownStartPageNum.Value.ToString()
"-" numericUpDownEndPageNum.Value.ToString()
").pdf";
int startPageNum = int.Parse(numericUpDownStartPageNum.Value.ToString());
int endPageNum = int.Parse(numericUpDownEndPageNum.Value.ToString());
copypdf(startPageNum, endPageNum, filePath, splitPDFPath);
MessageBox.Show("处理完毕!","提示");
}
}
}
private void copypdf(int StartPage, int EndPage, string file1, string splitFileName)
//将file1中页码StartPage到EndPage的文件拷贝至splitFileName
{
iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(file1);
Document document = new Document(reader.GetPageSizeWithRotation(StartPage));
//创建document对象
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(splitFileName, FileMode.Create));
//实例化document对象
document.Open();
int rotation;
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
while (StartPage <= EndPage)
{
document.SetPageSize(reader.GetPageSizeWithRotation(StartPage));
document.NewPage();
page = writer.GetImportedPage(reader, StartPage);
rotation = reader.GetPageRotation(StartPage);
if (rotation == 90 || rotation == 270)
{
//document.NewPage();
if (rotation == 90)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(StartPage).Height);
}
if (rotation == 270)
{
cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(StartPage).Width, 0);
}
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
StartPage ;
}
try
{
document.Close();
reader = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog myBlockDialog = new OpenFileDialog();
myBlockDialog.Filter = "PDF文件(*.pdf)|*.pdf";
myBlockDialog.Title = "打开PDF文件";
myBlockDialog.ShowDialog();
filePath = myBlockDialog.FileName;
if (filePath == "")
{
MessageBox.Show("选择PDF文件无效!", "文件无效!");
//Application.Exit();
}
labSelectInfo.Text = "已选择PDF文件:" filePath;
//iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(filePath);
Document document = new Document();
//PdfWriter.GetInstance(document,new FileStream(filePath,FileMode.Open));
PdfReader reader = new PdfReader(filePath);
iPageNum = reader.NumberOfPages;
reader.Close(); //不关闭会一直占用pdf资源,对接下来的操作会有影响
//PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(splitFileName, FileMode.Create));
labMaxPageNum.Text = @"/" iPageNum.ToString();
}
private void button3_Click(object sender, EventArgs e)
{
Document doc = new Document(PageSize.A4);
PdfWriter.GetInstance(doc,new FileStream("test1.pdf",FileMode.Create));
doc.Open();
doc.Add(new Paragraph("Hello!"));
doc.Close();
}
private void numericUpDownStartPageNum_ValueChanged(object sender, EventArgs e)
{
}
}
}