Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用word interop从模板创建多个文档以进行打印预览_C#_.net_Office Interop - Fatal编程技术网

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

C# 使用word interop从模板创建多个文档以进行打印预览,c#,.net,office-interop,C#,.net,Office Interop,我有一个应用程序,用户可以从列表中选择他们想要打印奖励文档的所有学生 我有一个template.doc文件,其中包含我从代码填充的3个文本框。我可以为单个学生填充文件并在打印预览中显示它,但根据选择的学生数量,我希望创建一个包含许多页面的大型文档,在打印之前我可以在打印预览中看到这些页面,然后一次性打印 以下是我尝试将从模板创建的单个word文档的工作代码转换为在打印预览中显示的代码。有什么想法吗 public void AddStudentToDocument(IEnumera

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

我有一个template.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; }
    }
public void AddStudentToDocument(IEnumerable studentsToPrint)
{
_应用程序oWordApp=新应用程序();
string folder=Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string specificFolder=Path.Combine(文件夹“foothillsaccademy”);
字符串fileLocation=Path.Combine(specificFolder,“CertTemplate.doc”);
if(File.Exists(fileLocation))
{
var oWordDoc=oWordApp.Documents.Open(fileLocation);
oWordDoc.Activate();
oWordApp.Selection.type段落();
foreach(变量studentToPrint in studentsToPrint)
{
_Document oDoc=oWordApp.Documents.Add();
选择oSelection=oWordApp.Selection;
字符串docText=oWordDoc.Content.Text;
如果(docText!=null)
{
int-boxNumber=1;
foreach(oWordApp.ActiveDocument.Shapes中的形状)
{
if(shape.Type==Microsoft.Office.Core.MsoShapeType.msoTextBox)
{
如果(boxNumber==1)
{
shape.TextFrame.TextRange.Text=studentToPrint.StudentName;
}
如果(boxNumber==2)
{
shape.TextFrame.TextRange.Text=studentToPrint.Rank;
}
如果(boxNumber==3)
{
shape.TextFrame.TextRange.Text=studentToPrint.dateacheed;
}
boxNumber++;
}
}
_Document oCurrentDocument=oWordApp.Documents.Add(oWordDoc);
copyPageSetup(oCurrentDocument.PageSetup、oDoc.Sections.Last.PageSetup);
oCurrentDocument.Range().Copy();
oSelection.PasteAndFormat(WdRecoveryType.WDFormatOriginalFormat);
//如果(!Object.ReferenceEquals(oWordDoc.Content,oWordDoc.Last()))
oSelection.InsertBreak(WdBreakType.wdSectionBreakNextPage);
}
oWordApp.Visible=true;
oWordApp.ShowStartupDialog=true;
oWordApp.ActiveDocument.PrintPreview();
}
}
}
专用void copyPageSetup(页面设置源、页面设置目标)
{
target.PaperSize=source.PaperSize;
如果(!source.Orientation.Equals(target.Orientation))
target.toggleGrative();
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;
}
公共班级学生打印
{
公共字符串StudentName{get;set;}
公共字符串秩{get;set;}
公共字符串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"
                            }
                    };
学生列表=新列表
{
新学生印刷
{
DateAcheved=DateTime.Now.ToSortDateString(),
Rank=“5度”,
StudentName=“Scott LaFoy”
},
新学生印刷
{
DateAcheved=DateTime.Now.ToSortDateString(),
Rank=“3度”,
StudentName=“约翰·多伊”
},
新学生印刷
{
DateAcheved=DateTime.Now.ToSortDateString(),
Rank=“2度”,
StudentName=“Jane Doe”
}
};

模板是一个有3个文本框的word文档。通过使用文本框,我可以为每个模板设置不同的字体和位置,并使背景透明,以便模板背景显示出来。我相信还有其他方法可以做到这一点,但关于这个话题的讨论并不多

我知道这是可能的,也不应该这么难。有谁知道一个包装器可能更容易使用吗?我将向您解释这里的问题是什么:您来自一个完全错误的方法。我试图修复一些小问题,但错误的结果不断出现(因为您的整个方法都是错误的)。我不想重做您的全部代码(这不是它的目的),作为回报,我必须在不太合理的条件下工作(纠正不应该存在的问题,因为这些问题是由基本的错误实现引起的)。我想我对这类问题的回答很糟糕,但我总是在回答