groege / PdfSharpCore

PdfSharp port NetCore
47 stars 13 forks source link

Calling document.Save() in UWP solution causes UnauthorizedAccessException #6

Closed Wetzel402 closed 7 years ago

Wetzel402 commented 7 years ago

I am new to UWP development and might be implementing it incorrectly but when I call document.save I seem to get an unauthorized access exception every time. I have not had time to look at the library code in detail yet.

Thank you for your efforts in rewriting the PdfSharp library!

o-leary commented 7 years ago

Are you including the projects as source or using pre-built dlls?

Do you have file/folder permissions? Specifically in UWP you need to use either the default app directory, specify library folders to require access to in your manifest, or get a file/folder permission via a file/folder picker (you can then save permissions for future executions). This means you can't just pass a string file name as most examples seem to show.

In case it is useful I have a small excerpt of how I save my migradoc documents (I'm also new). This assumes you already saved permissions for a custom folder. If you aren't using migradoc, and just pdfsharp you likely need the same arguments anyway.

 StorageFolder folder = null;

if (StorageApplicationPermissions.FutureAccessList.ContainsItem("mytokenname"))
{
    folder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync("mytokenname");
}
var pdfRenderer = new PdfDocumentRenderer(true);

// Set the MigraDoc document.
pdfRenderer.Document = _document;

// Create the PDF document.
pdfRenderer.RenderDocument();

// Save the PDF document...
Stream newFile = await folder.OpenStreamForWriteAsync(filename, CreationCollisionOption.ReplaceExisting);
pdfRenderer.Save(newFile, true);
groege commented 7 years ago

Since this is a general problem I'm closing the issue ;)

Wetzel402 commented 7 years ago

I am rewriting my .Net4.5 application that ran as admin so the folder permission issue is new to me ;)

@o-leary Your code snippet was very helpful and I am now on my way. I ended up using a folder picker to save a token.

Thanks again @groege for your wonderful work!