WPF:将流文档转换为PDF [英] WPF: flowdocument to PDF
本文介绍了WPF:将流文档转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hI
是否存在将流文档内容打印为PDF的控件?
Is there any control that prints flow document content to PDF?
不是第3方控件
谢谢
推荐答案
我能够通过将流文档内容保存为.DOCX并使用 Microsoft.Office.Interop.Word <将其转换为PDF来解决我的问题. /strong>
I was able to solve my problem by saving the flowdocument content to .DOCX and converting it to PDF by using Microsoft.Office.Interop.Word
using moiw = Microsoft.Office.Interop.Word;
public static void WordToPDF(string docFileName)
{
// Create a new Microsoft Word application object
moiw.Application word = new moiw.Application();
// C# doesn't have optional arguments so we'll need a dummy value
object oMissing = Missing.Value;
// Get a Word file
FileInfo wordFile = new FileInfo(docFileName);
word.Visible = false;
word.ScreenUpdating = false;
// Cast as Object for word Open method
Object filename = (Object)wordFile.FullName;
// Use the dummy value as a placeholder for optional arguments
moiw.Document doc = word.Documents.Open(ref filename, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
doc.Activate();
object outputFileName = wordFile.FullName.Replace(".docx", ".pdf");
object fileFormat = moiw.WdSaveFormat.wdFormatPDF;
// Save document into PDF Format
doc.SaveAs(ref outputFileName,
ref fileFormat, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
// Close the Word document, but leave the Word application open.
// doc has to be cast to type _Document so that it will find the
// correct Close method.
object saveChanges = moiw.WdSaveOptions.wdDoNotSaveChanges;
((moiw._Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
doc = null;
// word has to be cast to type _Application so that it will find
// the correct Quit method.
((moiw._Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
word = null;
}
这篇关于WPF:将流文档转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文