Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Wetzel402 opened this issue Apr 18, 2017 · 3 comments

Comments

@Wetzel402
Copy link

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
Copy link

o-leary commented Apr 19, 2017

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
Copy link
Owner

groege commented Apr 19, 2017

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

@groege groege closed this as completed Apr 19, 2017
@Wetzel402
Copy link
Author

Wetzel402 commented Apr 19, 2017

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants