user3541816
user3541816

Reputation: 23

c# convert fixed document to xps, but xps viewer will not open the new created xps file

i am trying to take a fixed document in c# and convert it to an xps and then save the xps so that i may attach it to an outlook email, but after i create the xps file and for testing purposes try to open the xps file in the xps file viewer, i get an error saying that the file cannot be opened, copy of the portion of the code where i am converting the fixed doc to an xps is pasted below:

//save fixed document in temp directory as xps document
            string filename = System.Environment.GetEnvironmentVariable("TEMP") + @"\TempFixedDocument.xps";
            System.IO.File.Delete(filename);
            XpsDocument xpsd = new XpsDocument(filename, FileAccess.ReadWrite);
            XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
            xw.WriteAsync(fxdDoc);
            xpsd.Close();

Upvotes: 1

Views: 1810

Answers (1)

Greenonion
Greenonion

Reputation: 87

I think it's because you use WriteAsync, but not wait until writing is finished. You should wait until file writing is finished and only then close the document.

You can use await keyword:

await xw.WriteAsync(fxdDoc);

Upvotes: 1

Related Questions