Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.47 KB | None | 0 0
  1. public class ReportPaginator : DocumentPaginator
  2. {
  3. #region Private properties
  4.  
  5. /// <summary> The paginator </summary>
  6. private readonly DocumentPaginator paginator;
  7.  
  8. /// <summary> The page counter </summary>
  9. private int pageCounter = 0;
  10.  
  11. #endregion
  12.  
  13. /// <summary> Initializes a new instance of the ReportPaginator class. </summary>
  14. /// <exception cref="ArgumentNullException"> Thrown when one or more required arguments are null. </exception>
  15. /// <param name="document"> The document to be paginated. </param>
  16. public ReportPaginator(FlowDocument document)
  17. {
  18. if (document == null)
  19. {
  20. throw new ArgumentNullException("document");
  21. }
  22.  
  23. // Create a copy of the flow document, so we can modify it without modifying the original.
  24. var copy = XamlReader.Parse(XamlWriter.Save(document)) as FlowDocument;
  25.  
  26. if (copy != null)
  27. {
  28. paginator = ((IDocumentPaginatorSource)copy).DocumentPaginator;
  29.  
  30. paginator.PageSize = new Size(800,600);
  31.  
  32. // Change page size of the document to the size of the content area
  33. copy.ColumnWidth = double.MaxValue; // prevent columns displaying
  34. copy.PageWidth = 600;
  35. copy.PageHeight = 800;
  36. copy.PagePadding = new Thickness(0);
  37.  
  38. PrecalculateNumberOfPages();
  39. ; // definition.UpdatePageNumberTexts(pages, footerStringPage0From1);
  40. }
  41. }
  42.  
  43. #region DocumentPaginator members
  44.  
  45. /// <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>
  46. /// <value> true if this object is page count valid, false if not. </value>
  47. /// ### <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>
  48. public override bool IsPageCountValid
  49. {
  50. get { return (pageCounter > 0) || paginator.IsPageCountValid; }
  51. }
  52.  
  53. /// <summary> When overridden in a derived class, gets a count of the number of pages currently formatted. </summary>
  54. /// <value> The number of pages. </value>
  55. /// ### <returns> A count of the number of pages that have been formatted. </returns>
  56. public override int PageCount
  57. {
  58. get { return (pageCounter > 0) ? pageCounter : paginator.PageCount; }
  59. }
  60.  
  61. /// <summary> When overridden in a derived class, gets or sets the suggested width and height of each page. </summary>
  62. /// <value> The size of the page. </value>
  63. /// ### <returns> A <see cref="T:System.Windows.Size" /> representing the width and height of each page. </returns>
  64. public override Size PageSize
  65. {
  66. get
  67. {
  68. return paginator.PageSize;
  69. }
  70.  
  71. set
  72. {
  73. paginator.PageSize = value;
  74. }
  75. }
  76.  
  77. /// <summary> When overridden in a derived class, returns the element being paginated. </summary>
  78. /// <value> The source. </value>
  79. /// ### <returns> An <see cref="T:System.Windows.Documents.IDocumentPaginatorSource" /> representing the element being paginated. </returns>
  80. public override IDocumentPaginatorSource Source
  81. {
  82. get { return paginator.Source; }
  83. }
  84.  
  85. #endregion
  86.  
  87. /// <summary> When overridden in a derived class, gets the <see cref="T:System.Windows.Documents.DocumentPage" /> for the specified page number. </summary>
  88. /// <param name="pageNumber"> The zero-based page number of the document page that is needed. </param>
  89. /// <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>
  90. /// ### <exception cref="T:System.ArgumentOutOfRangeException"> <paramref name="pageNumber" /> is negative. </exception>
  91. public override DocumentPage GetPage(int pageNumber)
  92. {
  93. return GetPageInternal(pageNumber);
  94. }
  95.  
  96. /// <summary> Creates a visual to draw the header/footer. </summary>
  97. /// <returns> Returns the visual of the header/footer. </returns>
  98. private Visual CreateHeaderVisual()
  99. {
  100. var visual = new DrawingVisual();
  101. using (DrawingContext context = visual.RenderOpen())
  102. {
  103. new DrawTextHelper(context).DrawText();
  104. }
  105.  
  106. return visual;
  107. }
  108.  
  109. #region Precalculating pages
  110.  
  111. /// <summary> Determines whether [is document page empty] [the specified page]. </summary>
  112. /// <param name="page">The page.</param>
  113. /// <returns> True if page is empty. </returns>
  114. private bool IsDocumentPageEmpty(DocumentPage page)
  115. {
  116. return page.ContentBox.IsEmpty;
  117. }
  118.  
  119. /// <summary> Precalculates the pages. </summary>
  120. private void PrecalculateNumberOfPages()
  121. {
  122. while (true)
  123. {
  124. var page = GetPageInternal(pageCounter, false);
  125. if (page == null || IsDocumentPageEmpty(page))
  126. {
  127. break;
  128. }
  129.  
  130. // pages.Add(page);
  131. ++pageCounter;
  132. }
  133. }
  134.  
  135. /// <summary> Gets the page, makes composition of the page. </summary>
  136. /// <param name="pageNumber">The page number.</param>
  137. /// <param name="fullRendering">if set to <c>true</c> [full rendering].</param>
  138. /// <returns> Page or null if not present. </returns>
  139. private DocumentPage GetPageInternal(int pageNumber, bool fullRendering = true)
  140. {
  141. // Use default paginator to handle pagination
  142. var originalPage = paginator.GetPage(pageNumber).Visual;
  143. if (originalPage == null)
  144. {
  145. return null;
  146. }
  147.  
  148. var visual = new ContainerVisual();
  149. if (fullRendering)
  150. {
  151. visual.Children.Add(CreateHeaderVisual());
  152. }
  153.  
  154. return new DocumentPage(
  155. visual,
  156. new Size(800, 600),
  157. new Rect(new Point(), new Size(800, 600)),
  158. new Rect(new Point(), new Size(100, 100)));
  159. }
  160.  
  161. #endregion
  162. }
  163.  
  164. public class DrawTextHelper
  165. {
  166. private DrawingContext drawingContext;
  167.  
  168. public DrawTextHelper(DrawingContext drawingContext)
  169. {
  170. this.drawingContext = drawingContext;
  171. }
  172.  
  173. public void DrawText()
  174. {
  175. var customFont = CustomFontsHelper.GetCustomFontFontFamily(CustomFonts.CustomFonts.TitilliumMaps29L_Black);
  176. var customFont2 = CustomFontsHelper.GetCustomFontFontFamily(CustomFonts.CustomFonts.TitilliumMaps29L_Thin);
  177. var typeface = new Typeface(customFont, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
  178. var typeface2 = new Typeface(customFont2, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
  179. var formattedText = new FormattedText("lazy dog", CultureInfo.InvariantCulture, System.Windows.FlowDirection.LeftToRight, typeface, 20, Brushes.Blue);
  180. var formattedText2 = new FormattedText("lazy dog", CultureInfo.InvariantCulture, System.Windows.FlowDirection.LeftToRight, typeface2, 20, Brushes.Blue);
  181.  
  182. drawingContext.DrawText(formattedText, new Point(0, 0));
  183. drawingContext.DrawText(formattedText2, new Point(0, 50));
  184. }
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement