PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 4:38 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Sat Sep 23, 2017 11:14 am 
Offline

Joined: Tue Apr 26, 2016 8:24 pm
Posts: 20
Hello,

I'm trying to write a XPS document in a background thread and I cannot use PageContent or FixedPage since the require STA thread. Reason is that it take quite a lot to load and it blocks the UI.
This works great, but the page size is not correct or if i have a landscape page it is still the default?! size in document viewer.
Question is. In what I must put the DrawingVisual in order to be able to change the page size/orientation without using PageContent or FixedPage?
Code:
            // XPS package definition
            var packetName = "undefined";
            var stream = new MemoryStream();
            var uri = new Uri($"pack://{packetName}.xps", UriKind.Absolute);
            PackageStore.RemovePackage(uri);
            var package = Package.Open(stream, FileMode.Create, FileAccess.ReadWrite);
            PackageStore.AddPackage(uri, package);
           
            // Creating the XPS document
            var xpsDocument = new XpsDocument(package, CompressionOption.NotCompressed, new Uri($"pack://{packetName}.xps", UriKind.Absolute).AbsoluteUri);
            // XPS document writer
            var docWriter = XpsDocument.CreateXpsDocumentWriter(xpsDocument);

            var pageCount = renderer.FormattedDocument.PageCount;
            for (int pageNumber = 1; pageNumber <= pageCount; pageNumber++) {
                DrawingVisual drawingVisual = new DrawingVisual();
                DrawingContext drawingContext = drawingVisual.RenderOpen();

                XGraphics graphics = XGraphics.FromDrawingContext(drawingContext,
                    new XSize(renderer.FormattedDocument.GetPageInfo(pageNumber).Width.Point, renderer.FormattedDocument.GetPageInfo(pageNumber).Height.Point),
                    XGraphicsUnit.Point);

                renderer.RenderPage(graphics, pageNumber, PageRenderOptions.All);

                drawingContext.Close();

                var container = new ContainerVisual();
                container.Children.Add(drawingVisual);

                list.Add(container);
            }

            visualcollator.BeginBatchWrite();
            foreach (var item in list) {
                visualcollator.Write(item);
            }
            visualcollator.EndBatchWrite();

            DocumentViewer = xpsDoc.GetFixedDocumentSequence();


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 142 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group