实例介绍
【实例简介】
【实例截图】
【核心代码】
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) { } } }
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)