使用word interop从模板创建多个文档以进行打印预览

时间:2013-10-22 03:16:18

标签: c# .net office-interop

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

我有一个模板.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; }
    }

目前我正在使用下面添加的StudentsToPrint集合对此进行测试。根据以下数据,我希望看到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文档。使用文本框可以让我为每个文本框设置不同的字体和位置,以及使背景透明,以便显示模板背景。我确信还有另一种方法可以做到这一点,但这方面的内容并不多。

0 个答案:

没有答案