0

我有一个应用程序,用户从列表中选择他们想要为其打印奖励文件的所有学生。

我有一个模板 .doc 文件,其中包含我从代码中填充的 3 个文本框。我可以填充文件并将其显示在单个学生的打印预览中,但根据选择的学生数量,我想创建一个包含许多页面的大型文档,然后在打印和一次打印之前在打印预览中看到。

以下是我尝试将我的工作代码转换为从模板创建的单个 Word 文档以显示在打印预览中。有任何想法吗?

        public void AddStudentToDocument(IEnumerable<StudentToPrint> studentsToPrint )
    {
        _Application oWordApp = new Application();

        string folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

        string specificFolder = Path.Combine(folder, "FoothillsAcademy");
        string fileLocation = Path.Combine(specificFolder, "CertTemplate.doc");

        if (File.Exists(fileLocation))
        {
            var oWordDoc = oWordApp.Documents.Open(fileLocation);
            oWordDoc.Activate();
            oWordApp.Selection.TypeParagraph();

            foreach (var studentToPrint in studentsToPrint)
            {

            _Document oDoc = oWordApp.Documents.Add();
            Selection oSelection = oWordApp.Selection;

            string docText = oWordDoc.Content.Text;

            if (docText != null)
            {
                int boxNumber = 1;
                foreach (Shape shape in oWordApp.ActiveDocument.Shapes)
                {
                    if (shape.Type == Microsoft.Office.Core.MsoShapeType.msoTextBox)
                    {
                        if (boxNumber == 1)
                        {
                            shape.TextFrame.TextRange.Text = studentToPrint.StudentName;
                        }
                        if (boxNumber == 2)
                        {
                            shape.TextFrame.TextRange.Text = studentToPrint.Rank;
                        }
                        if (boxNumber == 3)
                        {
                            shape.TextFrame.TextRange.Text = studentToPrint.DateAcheved;
                        }

                        boxNumber++;
                    }
                }

                _Document oCurrentDocument = oWordApp.Documents.Add(oWordDoc);
                copyPageSetup(oCurrentDocument.PageSetup, oDoc.Sections.Last.PageSetup);
                oCurrentDocument.Range().Copy();
                oSelection.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting);
                //if (!Object.ReferenceEquals(oWordDoc.Content, oWordDoc.Last()))
                    oSelection.InsertBreak(WdBreakType.wdSectionBreakNextPage);
            }

            oWordApp.Visible = true;
            oWordApp.ShowStartupDialog = true;
            oWordApp.ActiveDocument.PrintPreview();

            }
        }
    }

private void copyPageSetup(PageSetup source, PageSetup target)
        {
            target.PaperSize = source.PaperSize;

            if (!source.Orientation.Equals(target.Orientation))
                target.TogglePortrait();

            target.TopMargin = source.TopMargin;
            target.BottomMargin = source.BottomMargin;
            target.RightMargin = source.RightMargin;
            target.LeftMargin = source.LeftMargin;
            target.FooterDistance = source.FooterDistance;
            target.HeaderDistance = source.HeaderDistance;
            target.LayoutMode = source.LayoutMode;
        }

 public class StudentToPrint
    {
        public string StudentName { get; set; }
        public string Rank { get; set; }
        public string DateAcheved { get; set; }
    }

目前,我正在使用下面添加的 StudentToPrint 集合对此进行测试。根据下面的数据,我希望看到 3 名学生中的每人都有 3 份个性化证书。每个证书都在自己的页面上。

List<StudentToPrint> listOfStudents = new List<StudentToPrint>
                    {
                        new StudentToPrint
                            {
                                DateAcheved = DateTime.Now.ToShortDateString(),
                                Rank = "5th Degree",
                                StudentName = "Scott LaFoy"
                            },
                            new StudentToPrint
                            {
                                DateAcheved = DateTime.Now.ToShortDateString(),
                                Rank = "3rd Degree",
                                StudentName = "John Doe"
                            },
                            new StudentToPrint
                            {
                                DateAcheved = DateTime.Now.ToShortDateString(),
                                Rank = "2nd Degree",
                                StudentName = "Jane Doe"
                            }
                    };

该模板是一个包含 3 个文本框的 word 文档。使用文本框,我可以为它们中的每一个设置不同的字体和位置,并使背景透明,以便模板背景显示出来。我相信还有另一种方法可以做到这一点,但是关于这个话题的内容并不多。

4

0 回答 0