Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ReportPaginator : DocumentPaginator
- {
- #region Private properties
- /// <summary> The paginator </summary>
- private readonly DocumentPaginator paginator;
- /// <summary> The page counter </summary>
- private int pageCounter = 0;
- #endregion
- /// <summary> Initializes a new instance of the ReportPaginator class. </summary>
- /// <exception cref="ArgumentNullException"> Thrown when one or more required arguments are null. </exception>
- /// <param name="document"> The document to be paginated. </param>
- public ReportPaginator(FlowDocument document)
- {
- if (document == null)
- {
- throw new ArgumentNullException("document");
- }
- // Create a copy of the flow document, so we can modify it without modifying the original.
- var copy = XamlReader.Parse(XamlWriter.Save(document)) as FlowDocument;
- if (copy != null)
- {
- paginator = ((IDocumentPaginatorSource)copy).DocumentPaginator;
- paginator.PageSize = new Size(800,600);
- // Change page size of the document to the size of the content area
- copy.ColumnWidth = double.MaxValue; // prevent columns displaying
- copy.PageWidth = 600;
- copy.PageHeight = 800;
- copy.PagePadding = new Thickness(0);
- PrecalculateNumberOfPages();
- ; // definition.UpdatePageNumberTexts(pages, footerStringPage0From1);
- }
- }
- #region DocumentPaginator members
- /// <summary> When overridden in a derived class, gets a value indicating whether <see cref="P:System.Windows.Documents.DocumentPaginator.PageCount" /> is the total number of pages. </summary>
- /// <value> true if this object is page count valid, false if not. </value>
- /// ### <returns> true if pagination is complete and <see cref="P:System.Windows.Documents.DocumentPaginator.PageCount" /> is the total number of pages; otherwise, false, if pagination is in process and <see cref="P:System.Windows.Documents.DocumentPaginator.PageCount" /> is the number of pages currently formatted (not the total).This value may revert to false, after being true, if <see cref="P:System.Windows.Documents.DocumentPaginator.PageSize" /> or content changes; because those events would force a repagination. </returns>
- public override bool IsPageCountValid
- {
- get { return (pageCounter > 0) || paginator.IsPageCountValid; }
- }
- /// <summary> When overridden in a derived class, gets a count of the number of pages currently formatted. </summary>
- /// <value> The number of pages. </value>
- /// ### <returns> A count of the number of pages that have been formatted. </returns>
- public override int PageCount
- {
- get { return (pageCounter > 0) ? pageCounter : paginator.PageCount; }
- }
- /// <summary> When overridden in a derived class, gets or sets the suggested width and height of each page. </summary>
- /// <value> The size of the page. </value>
- /// ### <returns> A <see cref="T:System.Windows.Size" /> representing the width and height of each page. </returns>
- public override Size PageSize
- {
- get
- {
- return paginator.PageSize;
- }
- set
- {
- paginator.PageSize = value;
- }
- }
- /// <summary> When overridden in a derived class, returns the element being paginated. </summary>
- /// <value> The source. </value>
- /// ### <returns> An <see cref="T:System.Windows.Documents.IDocumentPaginatorSource" /> representing the element being paginated. </returns>
- public override IDocumentPaginatorSource Source
- {
- get { return paginator.Source; }
- }
- #endregion
- /// <summary> When overridden in a derived class, gets the <see cref="T:System.Windows.Documents.DocumentPage" /> for the specified page number. </summary>
- /// <param name="pageNumber"> The zero-based page number of the document page that is needed. </param>
- /// <returns> The <see cref="T:System.Windows.Documents.DocumentPage" /> for the specified <paramref name="pageNumber" />, or <see cref="F:System.Windows.Documents.DocumentPage.Missing" /> if the page does not exist. </returns>
- /// ### <exception cref="T:System.ArgumentOutOfRangeException"> <paramref name="pageNumber" /> is negative. </exception>
- public override DocumentPage GetPage(int pageNumber)
- {
- return GetPageInternal(pageNumber);
- }
- /// <summary> Creates a visual to draw the header/footer. </summary>
- /// <returns> Returns the visual of the header/footer. </returns>
- private Visual CreateHeaderVisual()
- {
- var visual = new DrawingVisual();
- using (DrawingContext context = visual.RenderOpen())
- {
- new DrawTextHelper(context).DrawText();
- }
- return visual;
- }
- #region Precalculating pages
- /// <summary> Determines whether [is document page empty] [the specified page]. </summary>
- /// <param name="page">The page.</param>
- /// <returns> True if page is empty. </returns>
- private bool IsDocumentPageEmpty(DocumentPage page)
- {
- return page.ContentBox.IsEmpty;
- }
- /// <summary> Precalculates the pages. </summary>
- private void PrecalculateNumberOfPages()
- {
- while (true)
- {
- var page = GetPageInternal(pageCounter, false);
- if (page == null || IsDocumentPageEmpty(page))
- {
- break;
- }
- // pages.Add(page);
- ++pageCounter;
- }
- }
- /// <summary> Gets the page, makes composition of the page. </summary>
- /// <param name="pageNumber">The page number.</param>
- /// <param name="fullRendering">if set to <c>true</c> [full rendering].</param>
- /// <returns> Page or null if not present. </returns>
- private DocumentPage GetPageInternal(int pageNumber, bool fullRendering = true)
- {
- // Use default paginator to handle pagination
- var originalPage = paginator.GetPage(pageNumber).Visual;
- if (originalPage == null)
- {
- return null;
- }
- var visual = new ContainerVisual();
- if (fullRendering)
- {
- visual.Children.Add(CreateHeaderVisual());
- }
- return new DocumentPage(
- visual,
- new Size(800, 600),
- new Rect(new Point(), new Size(800, 600)),
- new Rect(new Point(), new Size(100, 100)));
- }
- #endregion
- }
- public class DrawTextHelper
- {
- private DrawingContext drawingContext;
- public DrawTextHelper(DrawingContext drawingContext)
- {
- this.drawingContext = drawingContext;
- }
- public void DrawText()
- {
- var customFont = CustomFontsHelper.GetCustomFontFontFamily(CustomFonts.CustomFonts.TitilliumMaps29L_Black);
- var customFont2 = CustomFontsHelper.GetCustomFontFontFamily(CustomFonts.CustomFonts.TitilliumMaps29L_Thin);
- var typeface = new Typeface(customFont, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
- var typeface2 = new Typeface(customFont2, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
- var formattedText = new FormattedText("lazy dog", CultureInfo.InvariantCulture, System.Windows.FlowDirection.LeftToRight, typeface, 20, Brushes.Blue);
- var formattedText2 = new FormattedText("lazy dog", CultureInfo.InvariantCulture, System.Windows.FlowDirection.LeftToRight, typeface2, 20, Brushes.Blue);
- drawingContext.DrawText(formattedText, new Point(0, 0));
- drawingContext.DrawText(formattedText2, new Point(0, 50));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement