vs2008 documentview 怎么用,谁发个示例共享一下啊

xinyun80 2009-02-10 05:34:25
本人初次用vs2008 ,documentview 有哪些主要功能
...全文
138 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lnwuyaowei 2009-02-15
  • 打赏
  • 举报
回复
private void Format()
{
List<PrintLine> listLines = new List<PrintLine>();


FormattedText formtxtSample = GetFormattedText("w");

double width = PageSize.Width - Margins.Left - Margins.Right;

if (width < formtxtSample.Width)
return;

string strLine;
Pen pn = new Pen(Brushes.Black, 2);
StringReader reader = new StringReader(txt);

//
while (null != (strLine = reader.ReadLine()))
ProcessLine(strLine, width, listLines);
reader.Close();

//now start getting ready to print pages.
double heightLine = formtxtSample.LineHeight + formtxtSample.Height;
double height = PageSize.Height - margins.Top - margins.Bottom;
int linesPerPage = (int)(height / heightLine);

//serious problem:abandon ship
if (linesPerPage < 1)
return;

int numPages = (listLines.Count + linesPerPage - 1) / linesPerPage;
double xStart = margins.Left;
double yStart = margins.Top;

//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);

}
}

#region 得到FormattedText
private FormattedText GetFormattedText(string p)
{
return new FormattedText(p,CultureInfo.CurrentCulture,FlowDirection.LeftToRight,face,em,Brushes.Black);
}
#endregion
}
}
lnwuyaowei 2009-02-15
  • 打赏
  • 举报
回复
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;
}
}
宝_爸 2009-02-11
  • 打赏
  • 举报
回复
System.Windows.Xps.Packaging 命名空间
http://msdn.microsoft.com/zh-cn/library/system.windows.xps.packaging.aspx
xinyun80 2009-02-11
  • 打赏
  • 举报
回复
谢谢1楼的分享,想问多一个问题,xps文件可以编辑吗,像rtf一样
宝_爸 2009-02-10
  • 打赏
  • 举报
回复
documentview 时MFC的东西,C#没法用。
Fibona 2009-02-10
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/zh-cn/library/4x1xy43a(VS.80).aspx

17,741

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧