Как конвертировать png в xps? - PullRequest
0 голосов
/ 01 мая 2018

Я пытался конвертировать PNG в XPS. Я принимаю этот ответ . Мой код:

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». Или я все неправильно понял.

1 Ответ

0 голосов
/ 14 декабря 2018

Самый простой способ сделать это - распечатать все изображения на принтере 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();
...