小编Ste*_*ven的帖子

从Windows服务打印Xps

我正在尝试从.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

  • document.xps:要打印的XPS文档
  • document_printed_to_pdfcreator.pdf:用于演示出错的打印文档
  • XpsPrintTest.zip:带有示例代码的示例VS2010解决方案

托管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)

printing windows-services xps

9
推荐指数
1
解决办法
5390
查看次数

同时创建新文件

要创建一个新的唯一文件名,我使用以下代码:

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)

java windows multithreading file

5
推荐指数
1
解决办法
605
查看次数

标签 统计

file ×1

java ×1

multithreading ×1

printing ×1

windows ×1

windows-services ×1

xps ×1