xpstopdf
来源:互联网 发布:云计算案例分析 编辑:程序博客网 时间:2024/05/16 12:32
XMLtoXPS
public MainWindow()
{
InitializeComponent();
Stream srcXamlStream = new FileStream(@"D:\LZ21674\XAMLtoXPS\WpfBrowserApplication1\Page1.xaml", FileMode.Open);
string destXpsFile = @"D:/eqtg/Demo.Xps";
ParserContext context = new ParserContext();
context.BaseUri = new Uri(Directory.GetCurrentDirectory() + "//");
XamlStreamToXps(srcXamlStream, context, destXpsFile);
}
public void XamlStreamToXps(Stream srcXamlStream, ParserContext context, string destXpsFile)
{
XpsDocument document = null;
try
{
if (System.IO.File.Exists(destXpsFile)) System.IO.File.Delete(destXpsFile);
document = new XpsDocument(destXpsFile, FileAccess.Write);
XpsPackagingPolicy packagePolicy = new XpsPackagingPolicy(document);
XpsSerializationManager serializationMgr =
new XpsSerializationManager(packagePolicy, false);
object parsedDocObject = XamlReader.Load(srcXamlStream);
serializationMgr.SaveAsXaml(parsedDocObject);
MessageBox.Show("OK");
}
catch (Exception ex)
{
string exString = ex.Message;
MessageBox.Show(exString);
}
finally
{
if (document != null) document.Close();
if (srcXamlStream != null) srcXamlStream.Close();
}
}
XPS to PDF:
string sourceXpsFile = @"D:\eqtg\Demo.Xps";
string destPdfFile = @"D:\eqtg\pdf.pdf";
PdfSharp.Xps.XpsConverter.Convert(sourceXpsFile, destPdfFile, 0);