C#通过itextsharp实现PDF复制及实现自动打印

///


    /// 实现PDF复制
    ///

    /// 源PDF文件
    /// 目标PDF文件
    /// 是否实现自动打印   

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

你可能感兴趣的:(.net)