您的位置:首页 > 编程语言 > C#

Pdf之C#直接打印pdf文件

2013-07-29 09:17 573 查看
以下1,2种方法是可以正常进行pdf直接打印,其他方法为网页的参考资料,没有具体实践过。

1)确认本机安装adobeAcrobat 软件,用 Acrobat.exel /h /p c:/test2.pdf

2)调用本地命令行打印

[c-sharp]
view plaincopyprint?

//印刷  
public void printPDF(Dictionary<string, string> dt)  
{  
    if(File.Exists(pdfPath))  
    {  
        File.Delete(pdfPath);  
    }  
    createPDF(dt);  
  
    System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();  
    Process processInstance = new Process();  
    ProcessStartInfo startInfo = new ProcessStartInfo();  
    startInfo.UseShellExecute = true;  
    startInfo.Verb = "Print";  
    startInfo.CreateNoWindow = true;  
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;  
    startInfo.Arguments = @"/p /h /" + pdfPath + "/" /"" + pd.PrinterSettings.PrinterName + " /"";//pd.PrinterSettings.PrinterName;  
  
  
    startInfo.FileName = pdfPath;  
    processInstance.StartInfo = startInfo;  
  
    processInstance.Start();  
    //processInstance.CloseMainWindow();  
}  

//印刷
public void printPDF(Dictionary<string, string> dt)
{
if(File.Exists(pdfPath))
{
File.Delete(pdfPath);
}
createPDF(dt);

System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
Process processInstance = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.Verb = "Print";
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = @"/p /h /" + pdfPath + "/" /"" + pd.PrinterSettings.PrinterName + " /"";//pd.PrinterSettings.PrinterName;

startInfo.FileName = pdfPath;
processInstance.StartInfo = startInfo;

processInstance.Start();
//processInstance.CloseMainWindow();
}


3)通过itextsharp类

[c-sharp]
view plaincopyprint?

/// <summary>  
/// 实现PDF复制  
/// </summary>  
/// <param name="filePath">源PDF文件</param>  
/// <param name="toPath">目标PDF文件</param>  
/// <param name="print">是否实现自动打印</param>     
private static 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();  
}  

/// <summary>
/// 实现PDF复制
/// </summary>
/// <param name="filePath">源PDF文件</param>
/// <param name="toPath">目标PDF文件</param>
/// <param name="print">是否实现自动打印</param>
private static 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();
}


4)c#自带打印类库

System.Drawing.Printing.PrintDocument
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PDF