我刚刚开始为我当前的项目创建契约(Contract)生成例程,其中一项要求是契约(Contract)中的每一行都必须编号。编号应位于左边距,并用垂直线将编号与文档的其余部分分开。
我很确定我可以用 FixedDocument 来满足这个要求,但它不会很有趣。无论如何,我可以用 FlowDocument 来做到这一点吗?
最佳答案
您可以使用我的 WpfPrint 帮助程序类轻松地使用 FixedDocument 执行此操作,如下所示。要将其用于 XPS 创建,只需使用 XPS 的构造函数:
WpfPrint printer = new WpfPrint(new Size(1024, 768));
printer.CurrentElementMargin = new Thickness(2, 2, 2, 2);
printer.CurrentFontFamily = new FontFamily("Arial");
printer.CurrentFontSize = BaseFontSize;
printer.MarginX = 24;
printer.MarginY = 24;
您现在可以遍历并添加您想要的任何内容; TextBlocks 很可能就是您正在寻找的:
printer.AddTextBlock("Basics", printer.PageSizeUsed.Width, 0, WpfPrint.ElementFlags.NewLine | WpfPrint.ElementFlags.BottomCheck2);
如果您将它们包裹起来,它们会自行包裹起来;添加的每个项目都经过测量。如果它不适合页面,帮助程序类会添加一个新页面并将其移动到新页面上,这样您就可以从 FlowDocument 中获得想要的内容。您可以随时更改 CurX 和 CurY 以在放置项目时跳过水平或垂直空间。完成后,您可以像这样打印到打印机:
// Print final document
XpsDocumentWriter xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(dlg.PrintQueue);
xpsDocumentWriter.Write(printer.CurrentFixedDocument);
或者您可以另存为 XPS 文件:
using (XpsDocument doc = new XpsDocument(filename, FileAccess.Write))
{
XpsDocumentWriter xpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(doc);
xpsDocumentWriter.Write(printer.CurrentFixedDocument);
}
这是辅助类。享受!您再也不想使用 FlowDocument :-)
//****************************************************************************************
// WpfPrint
//
// Various helpers for printing WPF UI to a printer
//****************************************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Documents;
using System.Windows.Markup;
using System.Windows.Controls;
using System.Windows.Xps;
using System.Windows;
using System.Printing;
using System.Windows.Media;
namespace csheet
{
/// <summary>
/// Various helpers for printing WPF UI to a printer
/// </summary>
public class WpfPrint
{
#region Supporting Types
//****************************************************************************************
// Suporting Types
//****************************************************************************************
/// <summary>
/// Element flags define the way the elements print; OR them for multiple effects
/// </summary>
[Flags]
public enum ElementFlags
{
/// <summary>No special flags</summary>
None = 0,
/// <summary>Move to the next line after output</summary>
NewLine = 1,
/// <summary>if there isn't 2x room, then do new page first</summary>
BottomCheck2 = 2,
/// <summary>Center the item horizontally</summary>
HorzCenter = 4,
/// <summary>Right align the item (center overrides)</summary>
HorzRight = 8
}
#endregion Supporting Types
#region Data
//****************************************************************************************
// Data
//****************************************************************************************
FixedDocument _fixedDocument;
FixedPage _curFixedPage;
Canvas _curCanvas;
double _marginX;
double _marginY;
Size _infiniteSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
#endregion Data
#region Properties
//****************************************************************************************
// Properties
//****************************************************************************************
/// <summary>Current font family used for known objects</summary>
public FontFamily CurrentFontFamily { get; set; }
/// <summary>Current font size used for known objects</summary>
public double CurrentFontSize { get; set; }
/// <summary>Current font weight used for known objects</summary>
public FontWeight CurrentFontWeight { get; set; }
/// <summary>Current font style used for known objects</summary>
public FontStyle CurrentFontStyle { get; set; }
/// <summary>Current margin for known objects</summary>
public Thickness CurrentElementMargin { get; set; }
/// <summary>Current background for known objects</summary>
public Brush CurrentElementBackground { get; set; }
/// <summary>Current foreground for known objects</summary>
public Brush CurrentElementForeground { get; set; }
/// <summary>Gets the current fixed document being worked on</summary>
public FixedDocument CurrentFixedDocument
{
get { return _fixedDocument; }
}
/// <summary>The current horizontal position</summary>
public double CurX { get; set; }
/// <summary>The current vertical position</summary>
public double CurY { get; set; }
/// <summary>The starting and ending X margins on the page</summary>
public double MarginX
{
get { return _marginX; }
set
{
if (value < 0)
value = 0;
_marginX = value;
if (CurX < _marginX)
CurX = _marginX;
}
}
/// <summary>The starting and ending Y margins on the page</summary>
public double MarginY
{
get { return _marginY; }
set
{
if (value < 0)
value = 0;
_marginY = value;
if (CurY < _marginY)
CurY = _marginY;
}
}
/// <summary>Gets the page size for the document minus the margins</summary>
public Size PageSizeUsed
{
get
{
Size sz = CurrentFixedDocument.DocumentPaginator.PageSize;
sz.Width -= 2 * _marginX;
sz.Height -= 2 * _marginY;
return sz;
}
}
#endregion Properties
#region Construction
//****************************************************************************************
// Construction
//****************************************************************************************
/// <summary>
/// Constructor for printing
/// </summary>
/// <param name="printQueue"></param>
/// <param name="printTicket"></param>
public WpfPrint(PrintQueue printQueue, PrintTicket printTicket)
{
PrintCapabilities capabilities = printQueue.GetPrintCapabilities(printTicket);
Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
_fixedDocument = new FixedDocument();
_fixedDocument.DocumentPaginator.PageSize = sz;
StartPage();
}
/// <summary>
/// Constructor for XPS creation
/// </summary>
/// <param name="sz"></param>
public WpfPrint(Size sz)
{
_fixedDocument = new FixedDocument();
_fixedDocument.DocumentPaginator.PageSize = sz;
StartPage();
}
#endregion Construction
#region Interfaces
//****************************************************************************************
// Interfaces
//****************************************************************************************
/// <summary>
/// Add a new page to the document (start a new page)
/// </summary>
public void StartPage()
{
// Create a new page content and fixed page
PageContent content = new PageContent();
_fixedDocument.Pages.Add(content);
_curFixedPage = new FixedPage();
((IAddChild)content).AddChild(_curFixedPage);
// Create a new drawing canvas for the page
_curCanvas = new Canvas();
_curCanvas.Width = _fixedDocument.DocumentPaginator.PageSize.Width;
_curCanvas.Height = _fixedDocument.DocumentPaginator.PageSize.Height;
_curFixedPage.Children.Add(_curCanvas);
// Reset the current position
CurX = MarginX;
CurY = MarginY;
}
/// <summary>
/// Adds a new element at the current position, and updates the current position
/// </summary>
/// <param name="element">New element to add</param>
/// <param name="flags">Print options</param>
public void AddUIElement(UIElement element, ElementFlags flags)
{
element.Measure(_infiniteSize);
if (CurX > _fixedDocument.DocumentPaginator.PageSize.Width - MarginX)
{
CurY += element.DesiredSize.Height;
CurX = MarginX;
}
double extraCheck = 0;
if ((flags & ElementFlags.BottomCheck2) == ElementFlags.BottomCheck2)
extraCheck = element.DesiredSize.Height;
if (CurY > _fixedDocument.DocumentPaginator.PageSize.Height - MarginY - extraCheck)
StartPage();
_curCanvas.Children.Add(element);
element.SetValue(Canvas.LeftProperty, CurX);
element.SetValue(Canvas.TopProperty, CurY);
CurX += element.DesiredSize.Width;
if ((flags & ElementFlags.NewLine) == ElementFlags.NewLine)
{
CurX = MarginX;
CurY += element.DesiredSize.Height;
}
}
/// <summary>
/// Add a current style TextBlock element at the current position
/// </summary>
/// <param name="text">Text to add</param>
/// <param name="width">Width of element</param>
/// <param name="height">Height of element</param>
/// <param name="flags">Print options</param>
public void AddTextBlock(string text, double width, double height, ElementFlags flags)
{
TextBlock tb = new TextBlock();
tb.Text = text;
tb.FontFamily = CurrentFontFamily;
tb.FontSize = CurrentFontSize;
tb.FontWeight = CurrentFontWeight;
tb.FontStyle = CurrentFontStyle;
tb.VerticalAlignment = VerticalAlignment.Center;
if ((flags & ElementFlags.HorzCenter) == ElementFlags.HorzCenter)
tb.HorizontalAlignment = HorizontalAlignment.Center;
else if ((flags & ElementFlags.HorzRight) == ElementFlags.HorzRight)
tb.HorizontalAlignment = HorizontalAlignment.Right;
tb.Margin = CurrentElementMargin;
if (CurrentElementForeground != null)
tb.Foreground = CurrentElementForeground;
if (CurrentElementBackground != null)
tb.Background = CurrentElementBackground;
Grid grid = new Grid();
if (CurrentElementBackground != null)
grid.Background = CurrentElementBackground;
if (width != 0)
grid.Width = width;
if (height != 0)
grid.Height = height;
grid.Children.Add(tb);
AddUIElement(grid, flags);
}
/// <summary>
/// Adds a current style TextBox element at the current position
/// </summary>
/// <param name="text">Text to add</param>
/// <param name="width">Width of element</param>
/// <param name="height">Height of element</param>
/// <param name="flags">Print options</param>
public void AddTextBox(string text, double width, double height, ElementFlags flags)
{
TextBox tb = new TextBox();
tb.Text = text;
tb.FontFamily = CurrentFontFamily;
tb.FontSize = CurrentFontSize;
tb.FontWeight = CurrentFontWeight;
tb.FontStyle = CurrentFontStyle;
tb.VerticalAlignment = VerticalAlignment.Center;
tb.VerticalContentAlignment = VerticalAlignment.Center;
if ((flags & ElementFlags.HorzCenter) == ElementFlags.HorzCenter)
tb.HorizontalContentAlignment = HorizontalAlignment.Center;
else if ((flags & ElementFlags.HorzRight) == ElementFlags.HorzRight)
tb.HorizontalContentAlignment = HorizontalAlignment.Right;
tb.Margin = CurrentElementMargin;
if (CurrentElementBackground != null)
tb.Background = CurrentElementBackground;
if (CurrentElementForeground != null)
tb.Foreground = CurrentElementForeground;
Grid grid = new Grid();
if (CurrentElementBackground != null)
grid.Background = CurrentElementBackground;
if (width != 0)
grid.Width = width;
if (height != 0)
grid.Height = height;
grid.Children.Add(tb);
AddUIElement(grid, flags);
}
/// <summary>
/// Add a current style CheckBox element at the current position
/// </summary>
/// <param name="value">Checkbox value to add</param>
/// <param name="width">Width of element</param>
/// <param name="height">Height of element</param>
/// <param name="flags">Print options</param>
public void AddCheckBox(bool value, double width, double height, ElementFlags flags)
{
CheckBox cb = new CheckBox();
cb.IsChecked = value;
cb.VerticalAlignment = VerticalAlignment.Center;
if ((flags & ElementFlags.HorzCenter) == ElementFlags.HorzCenter)
cb.HorizontalAlignment = HorizontalAlignment.Center;
else if ((flags & ElementFlags.HorzRight) == ElementFlags.HorzRight)
cb.HorizontalAlignment = HorizontalAlignment.Right;
if (CurrentElementForeground != null)
cb.Foreground = CurrentElementForeground;
if (CurrentElementBackground != null)
cb.Background = CurrentElementBackground;
Grid grid = new Grid();
if (CurrentElementBackground != null)
grid.Background = CurrentElementBackground;
if (width != 0)
grid.Width = width;
if (height != 0)
grid.Height = height;
grid.Children.Add(cb);
AddUIElement(grid, flags);
}
#endregion Interfaces
}
}
关于c# - WPF - FlowDocument 的自动行号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6073644/
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.
我们目前正在为ROR3.2开发自定义cms引擎。在这个过程中,我们希望成为我们的rails应用程序中的一等公民的几个类类型起源,这意味着它们应该驻留在应用程序的app文件夹下,它是插件。目前我们有以下类型:数据源数据类型查看我在app文件夹下创建了多个目录来保存这些:应用/数据源应用/数据类型应用/View更多类型将随之而来,我有点担心应用程序文件夹被这么多目录污染。因此,我想将它们移动到一个子目录/模块中,该子目录/模块包含cms定义的所有类型。所有类都应位于MyCms命名空间内,目录布局应如下所示:应用程序/my_cms/data_source应用程序/my_cms/data_ty
我最喜欢的Google文档功能之一是它会在我工作时不断自动保存我的文档版本。这意味着即使我在进行关键更改之前忘记在某个点进行保存,也很有可能会自动创建一个保存点。至少,我可以将文档恢复到错误更改之前的状态,并从该点继续工作。对于在MacOS(或UNIX)上运行的Ruby编码器,是否有具有等效功能的工具?例如,一个工具会每隔几分钟自动将Gitcheckin我的本地存储库以获取我正在处理的文件。也许我有点偏执,但这点小保险可以让我在日常工作中安心。 最佳答案 虚拟机有些人可能讨厌我对此的回应,但我在编码时经常使用VIM,它具有自动保存功
我想知道是否可以通过自动创建数组来插入数组,如果数组不存在的话,就像在PHP中一样:$toto[]='titi';如果尚未定义$toto,它将创建数组并将“titi”压入。如果已经存在,它只会推送。在Ruby中我必须这样做:toto||=[]toto.push('titi')可以一行完成吗?因为如果我有一个循环,它会测试“||=”,除了第一次:Person.all.eachdo|person|toto||=[]#with1billionofperson,thislineisuseless999999999times...toto.push(person.name)你有更好的解决方案吗?
我如何做Ruby方法"Flatten"RubyMethod在C#中。此方法将锯齿状数组展平为一维数组。例如:s=[1,2,3]#=>[1,2,3]t=[4,5,6,[7,8]]#=>[4,5,6,[7,8]]a=[s,t,9,10]#=>[[1,2,3],[4,5,6,[7,8]],9,10]a.flatten#=>[1,2,3,4,5,6,7,8,9,10 最佳答案 递归解决方案:IEnumerableFlatten(IEnumerablearray){foreach(variteminarray){if(itemisIEnume
我最近从C#转向了Ruby,我发现自己无法制作可折叠的标记代码区域。我只是想到做这种事情应该没问题:classExamplebegin#agroupofmethodsdefmethod1..enddefmethod2..endenddefmethod3..endend...但是这样做真的可以吗?method1和method2最终与method3是同一种东西吗?还是有一些我还没有见过的用于执行此操作的Ruby惯用语? 最佳答案 正如其他人所说,这不会改变方法定义。但是,如果要标记方法组,为什么不使用Ruby语义来标记它们呢?您可以使用