XpsDocument loaded from disk
Create a FixedDocumentSequence via XpsDocument.GetFixedDocumentSequence Method ()
Display via DocumentViewer
I would like to add print headers and footers
I don't need to add the header and footer back to the XPS document - just for printing
I tried to implement DocumentPaginator , but could not get the size or placement for work
NOT the same as the footer for a FlowDocument
I use the solution there for FlowDocuments just fine Convert XAML Flow document to XPS with style
On FixedDocumentSequence, the size depends on the document (I think)
For example, you can mix landscape and portrait
I can't figure out how to connect to size and make room for the header and footer
I can put a title on it, but itβs located above the top of the page
And landscape pages are cropped
Cannot assign PageSize when it comes from FixedDocumentSequence
As indicated in the link, this is the recommended page size
DocumentPage Read-only size
Even if I create a DocumentPage of the size that I want, when the page is loaded using DocumentPaginator.GetPage , then the size will be overwritten
Even worse, the DocumentPaginator does not seem to be aware of the size, as it is not related to the mixed landscape and portrait properly. The landscape prints a portrait and just runs away from the page.
someone asked me to send a code
there might also be just a better approach see footer in FlowDocument on how to use it
this is where it breaks
// this gets the page size from GetPage - ignores the size above
public class DocumentPaginatorWrapperXPS : DocumentPaginator { System.Windows.Size m_PageSize; System.Windows.Size m_Margin; DocumentPaginator m_Paginator; FixedDocumentSequence fixedDocumentSequence; Typeface m_Typeface; private string printHeader; private int? sID; private string docID; public DocumentPaginatorWrapperXPS(FixedDocumentSequence FixedDocumentSequence, System.Windows.Size margin, string PrintHeader, int? SID, string DOCID) { //m_PageSize = pageSize; //m_PageSize = new Size(800, 600); fixedDocumentSequence = FixedDocumentSequence; m_Margin = margin; m_Paginator = fixedDocumentSequence.DocumentPaginator; //m_Paginator.PageSize = new System.Windows.Size(m_PageSize.Width - margin.Width * 2, // //m_PageSize.Height - margin.Width * 2); printHeader = PrintHeader; sID = SID; docID = DOCID; } Rect Move(Rect rect) { if (rect.IsEmpty) { return rect; } else { return new Rect(rect.Left + m_Margin.Width, rect.Top + m_Margin.Height, rect.Width, rect.Height); } } private Size sizeXPS; public override DocumentPage GetPage(int pageNumber) { System.Windows.Documents.DocumentPage page = m_Paginator.GetPage(pageNumber); Debug.WriteLine(page.Size.Width + " " + page.Size.Height); m_Paginator.PageSize = new System.Windows.Size(page.Size.Width/2 - m_Margin.Width * 2, page.Size.Height/2 - m_Margin.Height * 2); page = m_Paginator.GetPage(pageNumber); // this get the page size from GetPage - ignores size above Debug.WriteLine(page.Size.Width + " " + page.Size.Height); DynamicDocumentPaginator paginatorPage = fixedDocumentSequence.DocumentPaginator as DynamicDocumentPaginator; paginatorPage.PageSize = new System.Windows.Size(page.Size.Width / 2 - m_Margin.Width * 2, page.Size.Height / 2 - m_Margin.Height * 2); sizeXPS = new System.Windows.Size(page.Size.Width / 2 - m_Margin.Width * 2, page.Size.Height / 2 - m_Margin.Height * 2); page = paginatorPage.GetPage(pageNumber); // still does not change the page size Debug.WriteLine(page.Size.Width + " " + page.Size.Height); //page.PageSize = new System.Windows.Size(m_PageSize.Width - margin.Width * 2, //m_PageSize.Height - margin.Width * 2); //page.Size = new System.Windows.Size(page.Size.Width - m_Margin.Width * 2, page.Size.Height - m_Margin.Height * 2); //page.Size.Width = page.Size.Width - m_Margin.Width * 2; // Create a wrapper visual for transformation and add extras ContainerVisual newpage = new ContainerVisual(); DrawingVisual title = new DrawingVisual(); using (DrawingContext ctx = title.RenderOpen()) { if (m_Typeface == null) { m_Typeface = new Typeface("Segoe UI Symbol"); //Ariel } //FormattedText text = new FormattedText("Attorney eyes only \uE18B \u1F440 Page " + (pageNumber + 1), // System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, // m_Typeface, 14, System.Windows.Media.Brushes.Black); string pageS = "m_PageSize.Width " + m_PageSize.Width + " m_Margin.Width " + m_Margin.Width + " m_PageSize.Height " + m_PageSize.Height + " m_Margin.Height " + m_Margin.Height + Environment.NewLine + "page.Size.Width " + page.Size.Width + " page.Size.Height " + page.Size.Height; FormattedText header = new FormattedText("\u1F440 " + printHeader + Environment.NewLine + pageS, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, m_Typeface, 14, System.Windows.Media.Brushes.Black); ctx.DrawText(header, new System.Windows.Point(96 / 2, -96 / 4)); // 1/4 inch above page content //text = new FormattedText(pageS, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, // m_Typeface, 14, System.Windows.Media.Brushes.Black); string goGabe = string.Empty; if (sID != null) { goGabe += Environment.NewLine + "Gabriel Docsβ’ sysDocID: " + sID; if (!string.IsNullOrEmpty(docID)) goGabe += " docID: " + docID; } goGabe += Environment.NewLine + "Page: " + (pageNumber + 1) + " Date: " + DateTime.Now.ToString() + " User: " + App.StaticGabeLib.CurUserP.UserID; FormattedText footer = new FormattedText(goGabe , System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, m_Typeface, 14, System.Windows.Media.Brushes.Black); ctx.DrawText(footer, new System.Windows.Point(96 / 2, page.Size.Height - m_Margin.Height)); //ctx.DrawText(footer, new System.Windows.Point(96 / 2, m_Paginator.PageSize.Height - m_Margin.Height)); } DrawingVisual background = new DrawingVisual(); using (DrawingContext ctx = background.RenderOpen()) { ctx.DrawRectangle(new SolidColorBrush(System.Windows.Media.Color.FromRgb(240, 240, 240)), null, page.ContentBox); } newpage.Children.Add(background); // Scale down page and center ContainerVisual smallerPage = new ContainerVisual(); smallerPage.Children.Add(page.Visual); smallerPage.Transform = new MatrixTransform(0.8, 0, 0, 0.8, 0.1 * page.ContentBox.Width, 0.1 * page.ContentBox.Height); newpage.Children.Add(smallerPage); newpage.Children.Add(title); newpage.Transform = new TranslateTransform(m_Margin.Width, m_Margin.Height); return new DocumentPage(newpage, m_PageSize, Move(page.BleedBox), Move(page.ContentBox)); } public override bool IsPageCountValid { get { return m_Paginator.IsPageCountValid; } } public override int PageCount { get { return m_Paginator.PageCount; } } public override System.Windows.Size PageSize { get { Debug.WriteLine("PageSize " + sizeXPS.Width + " " + sizeXPS.Height); return sizeXPS; // this is not called //m_Paginator.PageSize; } set { sizeXPS = value; // m_Paginator.PageSize = value; } } public override IDocumentPaginatorSource Source { get { return m_Paginator.Source; } } }