PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

define page size when writing to XPS document
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3666
Page 1 of 1

Author:  MariusIonut [ Sat Sep 23, 2017 11:14 am ]
Post subject:  define page size when writing to XPS document

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

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/