0

我正在尝试在 ac# 应用程序中为游戏打印记分卡。我尝试遵循的方法是 - 使用模板 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);

我遇到的一些问题——

  1. 文档在其他进程中打开。
  2. 模板本身被修改。
  3. 打印无法完美运行。

我也尝试过使用 docx 库。但据我所知,它不提供打印功能。

4

0 回答 0