c# - 生成和打印 Microsoft Word 文件
问题描述
基本上,我想生成一个 Word 文件,然后打印它。
这是我到目前为止所拥有的:
private void CreateDocument()
{
try
{
// Create an instance for word app
Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();
// Set status for word application is to be visible or not.
winword.Visible = false;
// Create a missing variable for missing value
object missing = System.Reflection.Missing.Value;
// Create a new document
Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);
// Add paragraph with Heading 1 style
Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
object styleHeading1 = "Heading 1";
para1.Range.set_Style(ref styleHeading1);
para1.Range.Text = "BRGY. BOLO WATER SERVICE COOPERATIVE";
para1.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
para1.Range.InsertParagraphAfter();
// Save the document
filename = Application.StartupPath + @"\Disconnection\temp1.docx";
document.SaveAs(ref filename);
document.Close(ref missing, ref missing, ref missing);
document = null;
MessageBox.Show("Document created successfully !");
if (File.Exists(filename.ToString()))
{
PrintDocument printDoc = new PrintDocument();
PrinterSettings prnsetting = new PrinterSettings();
prnsetting.PrintFileName = Application.StartupPath + @"\Disconnection\.do";
printDoc.DocumentName = "temp1";
printPreviewDialog1.Document = printDoc;
printPreviewDialog1.ShowDialog();
}
}
catch (Exception ex)
{
//debug purposes
MessageBox.Show(ex.Message +"\n"+filename.ToString());
}
}
问题是当打印预览出现时,它只是一个空白页。有没有正确的方法来做到这一点?我必须为打印机设置文件路径吗?
解决方案
有许多将 Word 文档打印到打印机的示例。下面是另一个。希望这可以帮助?
public void PrintWord(string strFileName)
{
object oMissing = System.Reflection.Missing.Value;
string strCurrentActivePrinter;
//Start Word and open the test document.
Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word._Document oDoc;
oWord.Visible = false;
object oPath = strFileName;
//Get and store the current activeprinter
strCurrentActivePrinter = oWord.ActivePrinter;
//Assign new activeprinter to Word
oWord.ActivePrinter = PDFprinter;
oDoc = oWord.Documents.Open(ref oPath,
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);
//print it
object xcopies = 1;
object xpt = false;
object oFalse = false;
oDoc.PrintOut(ref xpt, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
xcopies, ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref oMissing, ref oMissing);
//close the document
oDoc.Close(ref oFalse, ref oMissing, ref oMissing);
oWord.Options.SaveNormalPrompt = false;
oWord.Options.SavePropertiesPrompt = false;
//Reset activerprinter
oWord.ActivePrinter = strCurrentActivePrinter;
//close word
oWord.Quit(ref oFalse, ref oMissing, ref oMissing);
//Drop our reference to the COM object
//Marshal.ReleaseComObject(oWord);
//Marshal.ReleaseThreadCache();
oDoc = null;
oWord = null;
}
推荐阅读
- r - 从相关矩阵生成数据:双变量分布的情况
- python-3.x - 尝试使用 Python 清理 QGIS 的文本文件
- c# - 隐藏 vuforia 标记
- python - Python Pandas - notnull() 和 ~notnull() 不是互斥的
- loadable-component - 使用可加载组件进行代码拆分的服务器端渲染反应应用程序
- python - 如何使用 SQLAlchemy 模型生成能够创建所有表的原始 SQL?
- mobile - 有没有可以检查阅读古兰经准确性的移动应用程序?
- git - 从 GitHub 中删除文件
- node.js - 使用 GRPC 在另一个微服务 nodejs 中调用一个服务
- java - NoClassDefFoundError:gherkin.deps.com.google.gson.Gson 的 java/sql/Time。
(Gson.java:232)