我正在尝试从.net框架上的Windows服务打印XPS文档.由于Microsoft不支持使用System.Drawing.Printing进行打印,也不支持使用System.Printing(WPF),因此我使用的是本机XPSPrint API.这是Aspose在http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-print-a-document-on-a-server-via-向我建议的. the-xpsprint-api.html.
当我尝试从Windows服务打印XPS文档时,结果包含奇怪的字符而不是我想要的文本.
我试过不同的打印机(包括像PDFCreator这样的虚拟打印机),不同的用户和服务的用户权限,不同的xps生成器(aspose,word 2007,word 2010),不同的平台(windows 7,windows 2008 R2)但所有有相同的结果.
有谁知道如何解决这个问题?任何帮助,将不胜感激!
对于那些想要尝试它的人,我通过以下方式共享了一些文件:
https://docs.google.com/leaf?id=0B4J93Ly5WzQKNWU2ZjM0MDYtMjFiMi00NzM0LTg4MTgtYjVlNDA5NWQyMTc3&hl=nl
托管Windows服务的示例代码是:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
namespace PrintXpsService
{
public partial class XpsPrintService : ServiceBase
{
// Change name of printer here
private String f_printerName = "PDFCreator";
// path to some file where logging is done
private String f_logFile = @"C:\temp\testdoc\xps_printing_service_log.txt";
// path …Run Code Online (Sandbox Code Playgroud) 要创建一个新的唯一文件名,我使用以下代码:
File file = new File(name);
synchronized (sync) {
int cnt = 0;
while (file.exists()) {
file = new File(name + " (" + (cnt++) + ")");
}
file.createNewFile();
}
Run Code Online (Sandbox Code Playgroud)
接下来,我使用该文件并将其删除.当我在多线程情况下执行此操作时,我有时会在以下情况下获得例外file.createNewFile():
java.io.IOException: Access is denied
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:1012)
Run Code Online (Sandbox Code Playgroud)
以下代码重现了该问题(大多数情况下):
final int runs = 1000;
final int threads = 5;
final String name = "c:\\temp\\files\\file";
final byte[] bytes = getSomeBytes();
final Object sync = new Object();
ExecutorService exec = Executors.newFixedThreadPool(threads);
for (int thread = 0; thread < …Run Code Online (Sandbox Code Playgroud)