编辑:完全重写问题并添加悬赏。
根据许多教程和stackoverflow的问题,我现在可以:
- 将多个页面打印成一个文档。
- 打印实际内容。
- 正确对齐页面上的内容。
- 打印正确大小的文档。
解决方案需要HTML文档底部有一些空白和样式标签html{ overflow:hidden; } - 隐藏滚动条并允许使用滚动进行分页 - 但我可以接受这一点。
唯一剩下的问题是WPF不会呈现WebBrowser屏幕外的部分。
这意味着如果我倾斜电脑屏幕,我可以正确打印,但如果我不这样做,文档会切掉下面的部分。
我尝试了将其渲染为位图,但当我将结果图像作为我的可视化内容打印时,页面为空。
如果您知道如何强制WPF进行完整呈现或如何正确渲染位图,请帮助我。
打印窗口XAML:(仅在UI线程上打印WPF才能正常工作,否则不会呈现任何内容...)
<Window x:Class="CardLoader2000.PrintWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="PrintWindow" Height="1139" Width="820">
<Grid x:Name="grid">
<!--The alignment and size of the webbrowser is reflected in the print. If larger than document
it will be cut. The width here corresponds to A4 paper width with a little margin-->
<WebBrowser x:Name="webBrowser" Height="1089" Width="770" VerticalAlignment="Top" Margin="0,10,0,0"/>
</Grid>
</Window>
打印窗口代码后台:
public partial class PrintWindow : Window
{
public PrintWindow(string path)
{
InitializeComponent();
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
webBrowser.NavigateToStream(fs);
ContentRendered += OnContentRendered;
}
private void OnContentRendered(object sender, EventArgs eventArgs)
{
PrintDialog pd = new PrintDialog
{
PrintTicket = new PrintTicket
{
Duplexing = Duplexing.TwoSidedLongEdge,
OutputColor = OutputColor.Monochrome,
PageOrientation = PageOrientation.Portrait,
PageMediaSize = new PageMediaSize(794, 1122),
InputBin = InputBin.AutoSelect
}
};
//Ok, final TODO: Page only renders what is on the PC screen...
WebPaginator paginator = new WebPaginator(webBrowser, 1089, 1122, 794);
pd.PrintDocument(paginator, "CustomerLetter");
Close();
}
}
自定义分页器:
public class WebPaginator : DocumentPaginator
{
private readonly WebBrowser webBrowser;
private readonly int pageScroll;
private Size pageSize;
public WebPaginator(WebBrowser webBrowser, int pageScroll, double pageHeight, double pageWidth)
{
this.webBrowser = webBrowser;
this.pageScroll = pageScroll;
pageSize = new Size(pageWidth, pageHeight);
}
public override DocumentPage GetPage(int pageNumber)
{
HTMLDocument htmlDoc = webBrowser.Document as HTMLDocument;
if (htmlDoc != null) htmlDoc.parentWindow.scrollTo(0, pageScroll * pageNumber);
Rect area = new Rect(pageSize);
return new DocumentPage(webBrowser, pageSize, area, area);
}
public override bool IsPageCountValid
{
get { return true; }
}
/// <summary>
/// Returns one less than actual length.
/// Last page should be whitespace, used for scrolling.
/// </summary>
public override int PageCount
{
get
{
var doc = (IHTMLDocument2)webBrowser.Document;
var height = ((IHTMLElement2)doc.body).scrollHeight;
int tempVal = height*10/pageScroll;
tempVal = tempVal%10 == 0
? Math.Max(height/pageScroll, 1)
: height/pageScroll + 1;
return tempVal > 1 ? tempVal-1 : tempVal;
}
}
public override Size PageSize
{
get
{
return pageSize;
}
set
{
pageSize = value;
}
}
/// <summary>
/// Can be null.
/// </summary>
public override IDocumentPaginatorSource Source
{
get
{
return null;
}
}
}