代码
/// <summary>
/// 实现PDF复制
/// </summary>
/// <param name="filePath">源PDF文件</param>
/// <param name="toPath">目标PDF文件</param>
/// <param name="print">是否实现自动打印</param>
private void ConvertPDFToPDF(string filePath, string toPath,bool print)
{
PdfReader reader = new PdfReader(filePath);
Document document = new Document(reader.GetPageSizeWithRotation(1));
int n = reader.NumberOfPages;
FileStream baos = new FileStream(toPath,FileMode.Create, FileAccess.Write);
PdfCopy copy = new PdfCopy(document, baos);
copy.ViewerPreferences = PdfWriter.HideToolbar|PdfWriter.HideMenubar;
//往pdf中写入内容
document.Open();
for (int i = 1; i <= n; i++)
{
PdfImportedPage page = copy.GetImportedPage(reader,i);
copy.AddPage(page);
}
if (print)
{
PdfAction.JavaScript("myOnMessage();", copy);
copy.AddJavaScript("this.print(true);function myOnMessage(aMessage) {app.alert('Test',2);} var msgHandlerObject = new Object();doc.onWillPrint = myOnMessage;this.hostContainer.messageHandler = msgHandlerObject;");
}
document.Close();
reader.Close();
}
/// 实现PDF复制
/// </summary>
/// <param name="filePath">源PDF文件</param>
/// <param name="toPath">目标PDF文件</param>
/// <param name="print">是否实现自动打印</param>
private void ConvertPDFToPDF(string filePath, string toPath,bool print)
{
PdfReader reader = new PdfReader(filePath);
Document document = new Document(reader.GetPageSizeWithRotation(1));
int n = reader.NumberOfPages;
FileStream baos = new FileStream(toPath,FileMode.Create, FileAccess.Write);
PdfCopy copy = new PdfCopy(document, baos);
copy.ViewerPreferences = PdfWriter.HideToolbar|PdfWriter.HideMenubar;
//往pdf中写入内容
document.Open();
for (int i = 1; i <= n; i++)
{
PdfImportedPage page = copy.GetImportedPage(reader,i);
copy.AddPage(page);
}
if (print)
{
PdfAction.JavaScript("myOnMessage();", copy);
copy.AddJavaScript("this.print(true);function myOnMessage(aMessage) {app.alert('Test',2);} var msgHandlerObject = new Object();doc.onWillPrint = myOnMessage;this.hostContainer.messageHandler = msgHandlerObject;");
}
document.Close();
reader.Close();
}
一个比较好的学习itextsharp网址:
http://pdfhome.hope.com.cn/ArcSearch.aspx?Content=循序渐进学习iTextSharp&Page=1