我重写了一个分页器,以显示文档的页眉和页脚。在我将其另存为xps Document之前,一切似乎都可以正常工作:
public void SaveAsXps(string fileName, FlowDocument document, string DocumentTitle, string DocumentFooter)
{
document.PageHeight = 1122.5 - 30;
document.PageWidth = 793.7 - 30;
using (Package container = Package.Open(fileName + ".xps", FileMode.Create))
{
using (XpsDocument xpsDoc = new XpsDocument(container, CompressionOption.Maximum))
{
XpsSerializationManager rsm = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false);
DocumentPaginator paginator = ((IDocumentPaginatorSource)document).DocumentPaginator;
paginator = new VisualPaginator(paginator, new Size(793.7, 1122.5), new Size(30, 30), DocumentTitle, DocumentFooter);
rsm.SaveAsXaml(paginator);
}
}
}
我给你分页器:
public class VisualPaginator : DocumentPaginator
{
string m_DocumentTitle;
string m_DocumentFooter;
private Size pageSize;
private Size margin;
private readonly DocumentPaginator paginator;
private Typeface typeface;
public override Size PageSize
{
get { return pageSize; }
set { pageSize = value; }
}
public override bool IsPageCountValid
{
get
{
return paginator.IsPageCountValid;
}
}
public override int PageCount
{
get
{
return paginator.PageCount;
}
}
public override IDocumentPaginatorSource Source
{
get
{
return paginator.Source;
}
}
public VisualPaginator(DocumentPaginator paginator, Size pageSize, Size margin, string DocumentTitle, string DocumentFooter)
{
PageSize = pageSize;
this.margin = margin;
this.paginator = paginator;
m_DocumentTitle = DocumentTitle;
m_DocumentFooter = DocumentFooter;
this.paginator.PageSize = new Size(PageSize.Width - margin.Width * 2,
PageSize.Height - margin.Height * 2);
}
public void DrawFunction(DrawingVisual content, string drawContent, Point point)
{
try
{
using (DrawingContext ctx = content.RenderOpen())
{
if (typeface == null)
{
typeface = new Typeface("Times New Roman");
}
FormattedText text = new FormattedText(drawContent, CultureInfo.CurrentCulture,
FlowDirection.LeftToRight, typeface, 14, Brushes.Black,
VisualTreeHelper.GetDpi(content).PixelsPerDip);
Thread.Sleep(300);
ctx.DrawText(text, point);
}
}
catch (Exception)
{
throw;
}
}
public override DocumentPage GetPage(int pageNumber)
{
DocumentPage page = paginator.GetPage(pageNumber);
// Create a wrapper visual for transformation and add extras
ContainerVisual newpage = new ContainerVisual();
//Title
DrawingVisual pagetitle = new DrawingVisual();
DrawFunction(pagetitle, m_DocumentTitle, new Point(paginator.PageSize.Width / 2 - 100, -96 / 4));
//Page Number
DrawingVisual pagenumber = new DrawingVisual();
DrawFunction(pagenumber, "Page " + (pageNumber + 1), new Point(paginator.PageSize.Width - 200, paginator.PageSize.Height - 100));
//Footer
DrawingVisual pagefooter = new DrawingVisual();
DrawFunction(pagefooter, m_DocumentFooter, new Point(paginator.PageSize.Width / 2 - 100, paginator.PageSize.Height - 100));
DrawingVisual background = new DrawingVisual();
using (DrawingContext ctx = background.RenderOpen())
{
ctx.DrawRectangle(new SolidColorBrush(Color.FromRgb(240, 240, 240)), null, page.ContentBox);
}
newpage.Children.Add(background); // Scale down page and center
ContainerVisual smallerPage = new ContainerVisual();
smallerPage.Children.Add(page.Visual);
//smallerPage.Transform = new MatrixTransform(0.95, 0, 0, 0.95,
// 0.025 * page.ContentBox.Width, 0.025 * page.ContentBox.Height);
newpage.Children.Add(smallerPage);
newpage.Children.Add(pagetitle);
newpage.Children.Add(pagenumber);
newpage.Children.Add(pagefooter);
newpage.Transform = new TranslateTransform(margin.Width, margin.Height);
return new DocumentPage(newpage, PageSize, page.BleedBox, page.ContentBox);
}
}
除非我使用Thread.Sleep(300)指令,否则代码执行会由于外部过程而导致断点。我认为程序必须等待一些外部过程完成,但是我不知道这里涉及哪些过程,我可以做些什么来等待它们解决问题,而无需使用Thread.Sleep(),这是非常糟糕的做法
任何帮助或线索都将不胜感激。