如何将png转换为xps?

时间:2018-05-01 11:30:07

标签: c# xps xpsdocument

我尝试将png转换为xps。我休息了answer。 我的代码:

err_code: 1216 Invalid JSONPath format: Member is not an object.  

我在这里遇到了异常

  

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

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

1 个答案:

答案 0 :(得分:0)

最简单的方法是将所有图像打印到Microsoft XPS Writer Printer中,然后可以合并输出的各个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();