我想将我的WPF控件转换为pdf,有人帮我一些例子吗?
我有一个WPF图表和一个列表视图:
上面的图片是我的WPF的输出,我想将此页面转换为pdf?
任何人都可以帮助我吗?下面是我的代码,可以创建PDF格式但在pdf内部没有内容,请帮忙。
public void SaveCurrentViewToXPS()
{
var dialog = new SaveFileDialog();
dialog.AddExtension = true;
dialog.DefaultExt = "pdf";
dialog.Filter = "PDF Document (*.pdf)|*.pdf";
if (dialog.ShowDialog() == false)
return;
FixedDocument fixedDoc = new FixedDocument();
PageContent pageContent = new PageContent();
FixedPage fixedPage = new FixedPage();
PrintDialog printDlg = new PrintDialog();
Size pageSize = new Size(printDlg.PrintableAreaWidth, printDlg.PrintableAreaHeight - 100);
lw.Width = 2100;
lw.Height = 2970;
//Create first page of document
fixedPage.Children.Add(lw);
//fixedPage.Children.Add(ColumnChart2);
((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
fixedDoc.Pages.Add(pageContent);
// write to PDF file
string tempFilename = "temp.xps";
File.Delete(tempFilename);
XpsDocument xpsDoc = new XpsDocument(tempFilename, FileAccess.Write);
XpsDocumentWriter xWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
xWriter.Write(fixedDoc.DocumentPaginator);
xpsDoc.Close();
PdfSharp.Xps.XpsConverter.Convert(tempFilename);
}
由于
答案 0 :(得分:0)
问题可能是在xps文档中添加视觉效果的方式。 `FixedDocument fixedDoc = new FixedDocument(); PageContent pageContent = new PageContent(); FixedPage fixedPage = new FixedPage();
PrintDialog printDlg = new PrintDialog();
Size pageSize = new Size(printDlg.PrintableAreaWidth, printDlg.PrintableAreaHeight - 100);
var visual = ((System.Windows.Controls.Panel)this.Content).Children[0] as UIElement;
((System.Windows.Controls.Panel)this.Content).Children.Remove(visual);
fixedPage.Children.Add(visual);
((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
fixedDoc.Pages.Add(pageContent);
// write to PDF file
string tempFilename = "temp.xps";
File.Delete(tempFilename);
XpsDocument xpsd = new XpsDocument(tempFilename, FileAccess.ReadWrite);
System.Windows.Xps.XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
xw.Write(fixedDoc);
xpsd.Close();
PdfSharp.Xps.XpsConverter.Convert(tempFilename, dialog.FileName, 1);` refer https://github.com/EbenZhang/PdfSharp.XPS