0

I tried to convert png to xps. I fallow this answer . My code:

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

XpsDocumentWriter xpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDocument);

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

Here I got an exception

System.IO.FileFormatException: 'File contains corrupted data.'

I assumed that answer's author by saying "YourImageYouWishToWrite" means a path to png file like 'C:\pathRoot\fileName.png'. Or I am getting it completely wrong.

mazury
  • 177
  • 1
  • 2
  • 12
  • [`Write(String)`](https://learn.microsoft.com/nl-nl/dotnet/api/system.windows.xps.xpsdocumentwriter.write?view=netframework-4.7.1#System_Windows_Xps_XpsDocumentWriter_Write_System_String_) apparently requires a path to an **xps** document. But I don't see how you can write an image – Hans Kesting May 01 '18 at 12:05
  • Is there a basis for you believing that an **XPS**DocumentWriter would write a PNG? – ProgrammingLlama May 01 '18 at 12:21
  • Is there any API which support printing images(png. etc) to printer 'Microsoft XPS Document Writer' ? – mazury May 01 '18 at 12:36
  • @John I thought that comment "YourImageYouWishToWrite" meant path to any image. – mazury May 01 '18 at 12:41
  • It's not simple. https://social.msdn.microsoft.com/Forums/en-US/8c6eb3ee-f541-4e00-b0e9-a982db7c73f5/how-to-add-images-like-jpgtiff-into-xps-document?forum=windowsxps –  May 01 '18 at 16:28

1 Answers1

0

the easiest way to do this through printing all Images into Microsoft XPS Writer Printer , then you can merge the output individual XPS files , like :

       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();