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();
}
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();
}
4)c#自带打印类库
System.Drawing.Printing.PrintDocument
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
相关文章推荐
- c# 直接打印pdf文件
- C# 打印PDF文件
- js直接打印pdf文件内容
- C# 打印PDF文件之使用不同打印机打印所有页面或部分页面
- C#中直接打印Report文件(rdlc)
- (转)C# 打印PDF文件使用第三方DLL
- C#使用指定打印机打印Word,Excel等Office文件和打印PDF文件的代码
- C# 直接打印指定路径文件 + 可选择指定打印机
- c#通过程序将文件直接送到打印机打印
- c#asp.net表格Html数据模板转换Pdf文件web打印pdf报表dll
- C#不使用ReportViewer直接打印Report文件(rdlc)的实现代码
- C#中直接打印Report文件(rdlc)
- C#打印pdf文件
- C# 打印PDF文件之使用不同打印机打印所有页面或部分页面
- (比如C#)里面调用AutoCAD ActiveX API实现后台打印DWG文件为PDF文件
- 使用firefox直接 打开pdf文件可以破解禁止打印的功能
- C#:MVC打印PDF文件
- C#中直接打印Report文件(rdlc)
- C#使用指定打印机打印Word,Excel等Office文件和打印PDF文件的代码
- C# 打印PDF文件