在C#中,FixedDocument是一个用于表示固定格式文档的类,它通常与WPF(Windows Presentation Foundation)一起使用
using System.Windows;using System.Windows.Documents;using System.Windows.Media;using System.Windows.Xps.Packaging;创建一个FixedDocument实例,并向其添加页面:// 创建一个新的FixedDocumentFixedDocument fixedDocument = new FixedDocument();// 添加页面for (int i = 0; i < 5; i++){ // 创建一个新的PageContent PageContent pageContent = new PageContent(); // 创建一个新的FixedPage FixedPage fixedPage = new FixedPage(); fixedPage.Width = 96 * 8.5; // 8.5英寸宽度 fixedPage.Height = 96 * 11; // 11英寸高度 // 将FixedPage添加到PageContent中 ((IAddChild)pageContent).AddChild(fixedPage); // 将PageContent添加到FixedDocument中 fixedDocument.Pages.Add(pageContent);}向FixedPage中添加内容:// 获取第一个页面FixedPage firstPage = (FixedPage)fixedDocument.Pages[0].Child;// 创建一个文本块并设置属性TextBlock textBlock = new TextBlock();textBlock.Text = "Hello, FixedDocument!";textBlock.FontSize = 24;textBlock.Foreground = Brushes.Black;// 将文本块添加到页面中firstPage.Children.Add(textBlock);保存FixedDocument为XPS文件:// 创建一个XpsDocumentWriter实例XpsDocumentWriter xpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(new FileStream("output.xps", FileMode.Create));// 将FixedDocument写入XPS文件xpsDocumentWriter.Write(fixedDocument);这个示例展示了如何创建一个包含5个空白页面的FixedDocument,并向第一个页面添加一个文本块。然后,将FixedDocument保存为XPS文件。你可以根据需要修改代码以添加更多内容和页面。


