基本信息
源码名称:C# word 批量打印开助手 源码
源码大小:0.32M
文件格式:.zip
开发语言:C#
更新时间:2016-05-19
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using Microsoft.Office.Interop.Word;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
namespace WordPrintApp
{
internal class PrintHelper
{
public void Printword(string filepath, string printername)
{
if (string.IsNullOrEmpty(printername))
{
printername = "Microsoft XPS Document Writer";
}
try
{
Process process = new Process();
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = filepath;
process.StartInfo.Verb = "print";
string defaultPrinter = PrintHelper.GetDefaultPrinter();
PrintHelper.SetDefaultPrinter(printername);
process.Start();
process.WaitForExit(10000);
PrintHelper.SetDefaultPrinter(defaultPrinter);
}
catch (Exception)
{
}
}
[DllImport("Winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool SetDefaultPrinter(string printerName);
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool GetDefaultPrinter(StringBuilder pszBuffer, ref int pcchBuffer);
private static string GetDefaultPrinter()
{
int capacity = 0;
if (PrintHelper.GetDefaultPrinter(null, ref capacity))
{
return null;
}
int lastWin32Error = Marshal.GetLastWin32Error();
if (lastWin32Error == 122)
{
StringBuilder stringBuilder = new StringBuilder(capacity);
if (PrintHelper.GetDefaultPrinter(stringBuilder, ref capacity))
{
return stringBuilder.ToString();
}
lastWin32Error = Marshal.GetLastWin32Error();
}
if (lastWin32Error == 2)
{
return null;
}
throw new Win32Exception(Marshal.GetLastWin32Error());
}
public void PrintMethodOther(string filepath, string printername)
{
try
{
object obj = filepath;
object value = Missing.Value;
object obj2 = true;
object obj3 = false;
object obj4 = 0;
Application application = new ApplicationClass();
application.set_Visible(false);
string activePrinter = application.get_ActivePrinter();
Document document = application.get_Documents().Open(ref obj, ref value, ref obj2, ref obj3, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value);
application.set_ActivePrinter(printername);
document.PrintOut(ref obj3, ref obj3, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value);
application.set_DisplayAlerts(0);
document.Close(ref obj4, ref value, ref value);
application.set_ActivePrinter(activePrinter);
application.Quit(ref value, ref value, ref value);
}
catch (Exception)
{
}
}
public bool CreateFolder()
{
bool result = false;
try
{
string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
string path = string.Empty;
if (this.FindSystemWidth() == "32")
{
path = folderPath "\\config\\systemprofile\\Desktop";
}
else
{
path = folderPath "\\config\\systemprofile\\Desktop";
}
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
result = true;
}
catch (Exception)
{
result = false;
}
return result;
}
public string FindSystemWidth()
{
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope = new ManagementScope("\\\\localhost", options);
ObjectQuery query = new ObjectQuery("select AddressWidth from Win32_Processor");
ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get();
string result = null;
using (ManagementObjectCollection.ManagementObjectEnumerator enumerator = managementObjectCollection.GetEnumerator())
{
while (enumerator.MoveNext())
{
ManagementObject managementObject = (ManagementObject)enumerator.Current;
result = managementObject["AddressWidth"].ToString();
}
}
return result;
}
}
}