我正在尝试在c#应用程序中为游戏打印记分卡。 我想要遵循的方法是 - 使用模板word文件并执行字符串替换和添加,以便在需要时添加团队名称,玩家信息和分数。
这是我到目前为止使用的代码段。但它似乎并不完美。
object readOnly = false; //default
object isVisible = false;
object addToRecentFiles = false;
wordApp.Visible = false;
aDoc = wordApp.Documents.Open(ref filename, ref missing, ref readOnly,
ref addToRecentFiles, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref isVisible,
ref missing, ref missing, ref missing, ref missing);
aDoc.Activate();
this.FindAndReplace(wordApp, "pname", tFirstname.Text);
this.FindAndReplace(wordApp, "teamname", tLastname.Text);
this.FindAndReplace(wordApp, "tel", tPhone.Text);
this.FindAndReplace(wordApp, "Company", tCompany.Text);
this.FindAndReplace(wordApp, "Date", DateTime.Now.ToShortDateString());
object copies = "1";
object pages = "1";
object range = Word.WdPrintOutRange.wdPrintCurrentPage;
object items = Word.WdPrintOutItem.wdPrintDocumentContent;
object pageType = Word.WdPrintOutPages.wdPrintAllPages;
object oTrue = true;
object oFalse = false;
Word.Document document = aDoc;
object nullobj = Missing.Value;
int dialogResult = wordApp.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFilePrint].Show(ref nullobj);
wordApp.Visible = false;
if (dialogResult == 1)
{
document.PrintOut(
ref oTrue, ref oFalse, ref range, ref missing, ref missing, ref missing,
ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oTrue,
ref missing, ref oFalse, ref missing, ref missing, ref missing, ref missing);
}
aDoc.Close(ref missing, ref missing, ref missing);
//File.Delete(tempPath);
MessageBox.Show("File created.");
List<int> processesaftergen = getRunningProcesses();
killProcesses(processesbeforegen, processesaftergen);
我遇到的一些问题 -
我也尝试过使用docx库。但据我所知,它不提供打印功能。