赞
踩
http://blog.csdn.net/yinshuhai/article/details/3042702
using iTextSharp.text;
using iTextSharp.text.pdf;
/// <summary>
/// pdf文件合并处理类
/// </summary>
class PDFMergeManager
{
private PdfWriter pw;
private PdfReader reader;
private Document document;
private PdfContentByte cb;
private PdfImportedPage newPage;
/// <summary>
/// 通过输出文件来构建合并管理,合并到新增文件中,合并完成后调用FinishedMerge方法
/// </summary>
/// <param name="sOutFiles"></param>
public PDFMergeManager(string sOutFiles)
{
document = new Document(PageSize.A3);
if (File.Exists(sOutFiles) && !MessageHelper.ShowQuestion("对应的文件已经存在,文件目录:/r/n "+sOutFiles+"/r/n是否覆盖该文件?"))
{
document = null;
throw new IOException("用户取消操作");
}
pw = PdfWriter.GetInstance(document, new FileStream(sOutFiles, FileMode.Create));
document.Open();
cb = pw.DirectContent;
}
/// <summary>
/// 通过文件流来合并文件,合并到当前的可写流中,合并完成后调用FinishedMerge方法
/// </summary>
/// <param name="sm"></param>
public PDFMergeManager(Stream sm)
{
document = new Document();
pw = PdfWriter.GetInstance(document, sm);
document.Open();
cb = pw.DirectContent;
}
/// <summary>
/// 合并文件
/// </summary>
/// <param name="sFiles">需要合并的文件路径名称</param>
/// <returns></returns>
public bool MergeFile(string sFiles)
{
reader = new PdfReader(sFiles);
{
int iPageNum = reader.NumberOfPages;
for (int j = 1; j <= iPageNum; j++)
{
newPage = pw.GetImportedPage(reader, j);
Rectangle r = reader.GetPageSize(j);
document.SetPageSize(r);
document.NewPage();
cb.AddTemplate(newPage, 0, 0);
}
}
reader.Close();
return true;
}
/// <summary>
/// 通过字节数据合并文件
/// </summary>
/// <param name="pdfIn">PDF字节数据</param>
/// <returns></returns>
public bool MergeFile(byte[] pdfIn)
{
reader = new PdfReader(pdfIn);
{
int iPageNum = reader.NumberOfPages;
for (int j = 1; j <= iPageNum; j++)
{
newPage = pw.GetImportedPage(reader, j);
Rectangle r = reader.GetPageSize(j);
document.SetPageSize(r);
document.NewPage();
cb.AddTemplate(newPage, 0, 0);
}
}
reader.Close();
return true;
}
/// <summary>
/// 通过PDF文件流合并文件
/// </summary>
/// <param name="pdfStream">PDF文件流</param>
/// <returns></returns>
public bool MergeFile(Stream pdfStream)
{
reader = new PdfReader(pdfStream);
{
int iPageNum = reader.NumberOfPages;
for (int j = 1; j <= iPageNum; j++)
{
newPage = pw.GetImportedPage(reader, j);
Rectangle r = reader.GetPageSize(j);
document.SetPageSize(r);
document.NewPage();
cb.AddTemplate(newPage, 0, 0);
}
}
reader.Close();
return true;
}
/// <summary>
/// 通过网络地址来合并文件
/// </summary>
/// <param name="pdfUrl">需要合并的PDF的网络路径</param>
/// <returns></returns>
public bool MergeFile(Uri pdfUrl)
{
reader = new PdfReader(pdfUrl);
{
int iPageNum = reader.NumberOfPages;
for (int j = 1; j <= iPageNum; j++)
{
newPage = pw.GetImportedPage(reader, j);
Rectangle r = reader.GetPageSize(j);
document.SetPageSize(r);
document.NewPage();
cb.AddTemplate(newPage, 0, 0);
}
}
reader.Close();
return true;
}
/// <summary>
/// 完成合并
/// </summary>
public void FinishedMerge()
{
try
{
if (reader != null)
{
reader.Close();
}
if (pw != null)
{
pw.Flush();
pw.Close();
}
if (document.IsOpen())
{
document.Close();
}
}
catch
{
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。