【问题标题】:How to convert png to xps?如何将png转换为xps?
【发布时间】:2018-10-11 10:42:14
【问题描述】:

我试图将 png 转换为 xps。我休闲这个answer。 我的代码:

XpsDocument xpsDocument = new XpsDocument(@"C:\pathRoot\fileName.xps", FileAccess.ReadWrite);

XpsDocumentWriter xpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDocument);

xpsDocumentWriter.Write(@"C:\pathRoot\fileName.png");

这里有个异常

System.IO.FileFormatException: '文件包含损坏的数据。'

我假设答案的作者说“YourImageYouWishToWrite”表示 png 文件的路径,如“C:\pathRoot\fileName.png”。或者我完全错了。

【问题讨论】:

  • Write(String) 显然需要 xps 文档的路径。但我不明白你怎么能写一张图片
  • 您是否有理由相信 XPSDocumentWriter 会编写 PNG?
  • 是否有任何 API 支持将图像(png。等)打印到打印机“Microsoft XPS Document Writer”?
  • @John 我认为评论“YourImageYouWishToWrite”意味着任何图像的路径。

标签: c# xps xpsdocument


【解决方案1】:

最简单的方法是将所有图像打印到 Microsoft XPS Writer 打印机中,然后您可以合并输出的单个 XPS 文件,例如:

       public void MergeXpsDocument(string outputXPS, string[] ListOfXPS)
    {
        if (File.Exists(outputXPS))
        {
            File.Delete(outputXPS);
        }

        XpsDocument[] XpsFiles = new XpsDocument[ListOfXPS.Length];
        for (int i = 0; i < ListOfXPS.Length; i++)
        {
            XpsDocument xpsFile = new XpsDocument(ListOfXPS[i], FileAccess.Read);
            XpsFiles[i] = xpsFile;
        }

        XpsDocument tempPackage = new XpsDocument(outputXPS, FileAccess.ReadWrite);
        XpsDocumentWriter xpsDocWriter = XpsDocument.CreateXpsDocumentWriter(tempPackage);
        FixedDocumentSequence fixedDocSequence = new FixedDocumentSequence();

        foreach (XpsDocument XPSdoc in XpsFiles)
        {
            FixedDocumentSequence fixedDocSeq = XPSdoc.GetFixedDocumentSequence();
            foreach (DocumentReference sourceSequence in fixedDocSeq.References)
            {
                DocumentReference docRef = new DocumentReference();
                docRef.Source = sourceSequence.Source;
                (docRef as IUriContext).BaseUri = (sourceSequence as IUriContext).BaseUri;
                FixedDocument fd = docRef.GetDocument(true);
                docRef.SetDocument(fd);
                fixedDocSequence.References.Add(docRef);
            }
        }
        xpsDocWriter.Write(fixedDocSequence);
        tempPackage.Close();

【讨论】:

    猜你喜欢
    • 2011-11-25
    • 2011-04-22
    • 1970-01-01
    • 2011-03-10
    • 2011-06-30
    • 2011-01-18
    • 2016-01-19
    • 2011-06-25
    • 1970-01-01
    相关资源
    最近更新 更多