我知道TextBlock
可以提出一个FlowDocument
,例如:
<TextBlock Name="txtFont">
<Run Foreground="Maroon" FontFamily="Courier New" FontSize="24">Courier New 24</Run>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
我想知道如何将FlowDocument
存储在变量中的一个设置为a TextBlock
.我正在寻找类似的东西:
string text = "<Run Foreground="Maroon" FontFamily="Courier New" FontSize="24">Courier New 24</Run>"
txtFont.Text = text;
Run Code Online (Sandbox Code Playgroud)
但是,上面代码的结果是XAML文本显示为未解析.
编辑:我想我的问题不够明确.我真正想要达到的目的是:
FlowDocument
从RichTextBox保存用户输入,并将其序列化到磁盘.FlowDocument
从磁盘到可变反序列化文本.TextBlock
.因此,据我所知,创建一个新的Run对象并手动设置参数将无法解决我的问题.
问题是序列化RichTextBox会创建Section对象,我无法将其添加到TextBlock.Inlines.因此,无法将反序列化的对象设置为TextBlock的TextProperty.
如何将一个FlowDocument的内容复制到下面的另一个FlowDocument是我尝试过的
foreach (var blk in fd1.Blocks)
{
fd2.Blocks.Add(blk);
}
Run Code Online (Sandbox Code Playgroud)
fd1是FlowDocument1,fd2是FlowDocument2.
但我得到以下错误.
收集被修改; 枚举操作可能无法执行.
谢谢
阿文德
我有一个FlowDocument,我想转换为XPS文档并将其附加到电子邮件并将它们一起发送.我正在使用此代码
public static MemoryStream FlowDocumentToXPS(FlowDocument flowDocument, int width, int height)
{
MemoryStream stream = new MemoryStream();
using (Package package = Package.Open(stream, FileMode.Create, FileAccess.ReadWrite))
{
using (XpsDocument xpsDoc = new XpsDocument(package, CompressionOption.Maximum))
{
XpsSerializationManager rsm = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false);
DocumentPaginator paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator;
paginator.PageSize = new System.Windows.Size(width, height);
rsm.SaveAsXaml(paginator);
rsm.Commit();
}
}
stream.Position = 0;
Console.WriteLine(stream.Length);
Console.WriteLine(stream.Position);
return stream;
}
Run Code Online (Sandbox Code Playgroud)
然后我使用以下代码附加它:
Attachment xps = new Attachment(FlowDocumentToXPS(FD, 768, 676), "FileName.xps", "application/vnd.ms-xpsdocument");
Run Code Online (Sandbox Code Playgroud)
其中FD是我要转换的FlowDocument,我接收到附加的0.0KB大小的XPS文件,并且无法使用XPS Viewer打开,我在这里缺少什么?
编辑:有效的最终代码,请参阅注释
提前致谢
我正在编写代码以从FlowDocument打印.
PrintDialog printDialog = new PrintDialog();
bool? result = printDialog.ShowDialog();
if (result == true)
{
FlowDocument fd = new FlowDocument();
fd.Blocks.Add(new Paragraph(new Run(String.Format("Message:\r\n{0}\r\n", txtMessage.Text))));
fd.PageHeight = printDialog.PrintableAreaHeight;
fd.PageWidth = printDialog.PrintableAreaWidth;
printDialog.PrintDocument((fd as IDocumentPaginatorSource).DocumentPaginator, "print test");
}
Run Code Online (Sandbox Code Playgroud)
此代码将在一个页面中打印多个列.怎么避免这个?
你会如何建议我将FlowDocument转换为PDF以附加到电子邮件?
FlowDocument是动态的,而不是静态的.
我希望能够将PDF作为byte []存储在内存中,而不是存储在磁盘上,但这是可以协商的.
谢谢你的帮助!
所有这些文本看起来都一样,但我试图让它们看起来不同.我想要小帽子文字.我在这里错过了什么让小帽子排版效果起作用?
要重现这一点,请打开Visual Studio 2008,Do File | New Project,创建一个新的Windows | WPF应用程序,将下面的标记粘贴到Window1.xaml中,然后运行它.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<FlowDocumentReader>
<FlowDocument>
<Paragraph>
<Run>Some text</Run> <LineBreak />
<Run Typography.Capitals="SmallCaps">Some text</Run> <LineBreak />
<Run Typography.Capitals="AllSmallCaps">Some text</Run> <LineBreak />
<Run Typography.Capitals="PetiteCaps">Some text</Run> <LineBreak />
<Run Typography.Capitals="AllPetiteCaps">Some text</Run> <LineBreak />
</Paragraph>
</FlowDocument>
</FlowDocumentReader>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
根据第一个答案,似乎如果你指定一个特定的字体,你可以到达某个地方.将FlowDocument开始标记更改为:
<FlowDocument FontFamily="Palatino Linotype">
Run Code Online (Sandbox Code Playgroud)
..你得到SmallCaps和AllSmallCaps,但不是PetiteCaps或AllPetiteCaps.所以它取决于字体.但这引起了其他问题:
请考虑以下RTF文档
{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fswiss\fprq2\fcharset0 Segoe UI;}{\f1\fswiss\fcharset0 Arial;}}
{\*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\f0\fs22 Sample Text\f1\fs20\par
}
Run Code Online (Sandbox Code Playgroud)
它包含字体Segoe UI 11 pt中的"Sample Text".现在,当我分别使用WPF的FlowDocument和TextRange.Load()和.Save()加载然后保存文档时,字体大小减小到10.5pt.使用RTF作为输入/输出时,有没有办法保留原始字体大小?
我想要一个表格,根据内容在逻辑上调整列的大小。这在 WPF 中可能吗?
替代文字 http://img43.imageshack.us/img43/2640/flowdocument.jpg
这是我正在使用的代码:
<Window x:Class="FlowDocument.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style TargetType="{x:Type TableCell}">
<Setter Property="BorderBrush" Value="Gray" />
<Setter Property="BorderThickness" Value="3" />
</Style>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Padding" Value="2, 2, 2, 2" />
</Style>
</Window.Resources>
<Grid>
<FlowDocumentScrollViewer>
<FlowDocument>
<Table>
<Table.Columns>
<TableColumn Background="LightBlue" />
<TableColumn Background="Coral" />
</Table.Columns>
<TableRowGroup>
<TableRow>
<TableCell>
<Paragraph>This is a long piece of text</Paragraph>
</TableCell>
<TableCell>
<Paragraph>This isn't</Paragraph>
</TableCell>
</TableRow>
<TableRow>
<TableCell>
<Paragraph>This is a another long piece of text. The column should be …
Run Code Online (Sandbox Code Playgroud) 问候,我在WPF中打印有问题.我正在创建一个流文档并为该流文档添加一些控件.打印预览工作正常,我从打印预览窗口打印没有问题.在没有打印预览的情况下直接打印到打印机时存在问题.但更令人惊讶的是 - 当我使用XPS Document Writer作为打印机时,每次都可以,当我使用某些物理打印机时,我的流文档上的某些控件不会显示.提前致谢
我在解决这个问题时遇到了一些麻烦,希望有人可以提供帮助.
我有一个带有RichTextBox的WPF项目.
当我编辑文本时,我希望文档中的光标始终保持垂直居中.
例如,在编辑时向上或向下推,而不是光标向上,我希望文本下降.这应该会导致光标停留的印象.
非常感谢.