//create the list to store each documentpage object.
listPages = new List<DocumentPage>();
for (int iPage = 0, iLine = 0; iPage < numPages; iPage++)
{
DrawingVisual vis = new DrawingVisual();
DrawingContext dc = vis.RenderOpen();
//Display Headr at top of page.
if (Header != null && Header.Length > 0)
{
FormattedText formtxt = GetFormattedText(Header);
formtxt.SetFontWeight(FontWeights.Bold);
Point ptText = new Point(xStart, yStart - 2 * formtxt.Height);
dc.DrawText(formtxt, ptText);
}
//Display footer at botton of page
if (numPages > 1)
{
FormattedText formtxt = GetFormattedText("Page" + (iPage + 1) + " of " + numPages);
formtxt.SetFontWeight(FontWeights.Bold);
Point ptText = new Point((PageSize.Width + Margins.Left - margins.Right - formtxt.Width) / 2,
PageSize.Height - margins.Bottom + formtxt.Height);
dc.DrawText(formtxt, ptText);
}
//look through the lines on the page
for (int i = 0; i < linesPerPage; i++, iLine++)
{
if (iLine == listLines.Count)
break;
//set up information to display the text of the line
string str = listLines[iLine].String;
FormattedText formtxt = GetFormattedText(str);
Point ptText = new Point(xStart, yStart + i * heightLine);
dc.DrawText(formtxt, ptText);
//possibly display the little arrow flag
if (listLines[iLine].Flag)
{
double x = xStart + width + 6;
double y = yStart + i * heightLine + formtxt.Baseline;
double len = face.CapsHeight * em;
dc.DrawLine(pn,new Point(x,y),new Point(x+len,y-len));
dc.DrawLine(pn, new Point(x, y), new Point(x + len / 2, y));
}
}
//画出矩形
Rect rectPage = new Rect(margins.Left, margins.Top, PageSize.Width - (margins.Left + margins.Right), PageSize.Height - (margins.Top + margins.Bottom));
dc.DrawRectangle(null, pn, rectPage);
dc.Close(); DocumentPage page = new DocumentPage(vis);
listPages.Add(page);
}
reader.Close();
}
//
private void ProcessLine(string strLine, double width, List<PrintLine> listLines)
{
strLine = strLine.TrimEnd(' ');
if (TextWrapping == TextWrapping.NoWrap)
{
do
{
int length = strLine.Length;
while (GetFormattedText(strLine.Substring(0, length)).Width > width)
length--;
listLines.Add(new PrintLine(strLine.Substring(0, length), length < strLine.Length));
strLine = strLine.Substring(length);
} while (strLine.Length > 0);
}
else
{
do
{
int length = strLine.Length;
bool flag = false;
while (GetFormattedText(strLine.Substring(0, length)).Width > width)
{
int index = strLine.LastIndexOfAny(charBreak);
if (index != -1)
length = index + 1;
else
{
if (TextWrapping == TextWrapping.Wrap)
{
while (GetFormattedText(strLine.Substring(0, length)).Width > width)
length--;
flag = true;
}
break;
}
}
listLines.Add(new PrintLine(strLine.Substring(0, length), flag));
strLine = strLine.Substring(length);
}
while (strLine.Length > 0);
xps文件从编辑是比较困难的,就算是生成,也需要写一大堆代码。
楼主如想在WPF下打印,可以参考以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows;
using System.Printing;
using System.IO;
using System.Globalization;
namespace StudyEnglish
{
public class EnglishPlainTestDocumentPaginator:DocumentPaginator
{
char[] charBreak = new char[] { ' ', '-' };
string txt = "";
string txtHeader = null;
Typeface face = new Typeface("");
double em = 11;
Size sizePage = new Size(8.5 *96,11*96);
Size sizeMax = new Size(0, 0);
Thickness margins = new Thickness(96);
PrintTicket prntkt = new PrintTicket();
TextWrapping txtwrap = TextWrapping.Wrap;
List<DocumentPage> listPages;
public string Text
{
set { txt = value; }
get { return txt; }
}
public TextWrapping TextWrapping
{
set { txtwrap = value; }
get { return txtwrap; }
}
public Thickness Margins
{
set { margins = value; }
get { return margins; }
}
public Typeface Typeface
{
set { face = value; }
get { return face; }
}
public double FaceSize
{
set { em = value; }
get { return em; }
}
public PrintTicket PrintTicket
{
set { prntkt = value; }
get { return prntkt; }
}
public string Header
{
set { txtHeader = value; }
get { return txtHeader; }
}
//需要重写的内容
public override bool IsPageCountValid
{
get {
if (listPages == null)
{
Format();
}
return true;
}
}
public override int PageCount
{
get {
if (listPages == null)
return 0;
return listPages.Count;
}
}
public override Size PageSize
{
get
{
return sizePage;
}
set
{
sizePage = value;
}
}
#region 得到页
public override DocumentPage GetPage(int pageNumber)
{
return listPages[pageNumber];
}
#endregion
public override IDocumentPaginatorSource Source
{
get { return null; }
}
//internal class
class PrintLine
{
public string String;
public bool Flag;
public PrintLine(string str, bool flag)
{
String = str;
Flag = flag;
}
}