问题描述
我重写了一个分页器,以显示文档的页眉和页脚。在我将其另存为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 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,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.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(),这是非常糟糕的做法
任何帮助或线索都将不胜感激。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)