public static Document CreateBills(ICollection<WordStatementInfo> statements, IProgressReporter progress, bool duplexMode)
        {
            if (statements == null) throw new ArgumentNullException("statements");

            progress = progress ?? new EmptyProgressReporter();

            progress.Caption = "Creating document";

            Dictionary<StatementKind, Range> sourceRanges = new Dictionary<StatementKind, Range>();
            try {
                foreach (var kind in statements.Select(s => s.Kind).Distinct()) {
                    var sd = Word.Documents.Open(
                        FileName: Path.Combine(WordExport.TemplateFolder, kind.ToString() + ".docx"),
                        ReadOnly: true,
                        AddToRecentFiles: false
                    );
                    // Fix Word 2013 bug
                    // http://blogs.msmvps.com/wordmeister/2013/02/22/word2013bug-not-available-for-reading/
                    sd.ActiveWindow.View.Type = WdViewType.wdPrintView;
                    sourceRanges.Add(kind, sd.Range());
                }

                Document doc = Word.Documents.Add();
                doc.ShowGrammaticalErrors = doc.ShowSpellingErrors = false;
                Range range = doc.Range();

                bool firstPage = true;
                using (new ClipboardScope()) {
                    var populator = new StatementPopulator();

                    progress.Maximum = statements.Count;
                    int i = 0;
                    foreach (var info in statements) {
                        if (progress.WasCanceled) return null;
                        progress.Progress = i;

                        progress.Caption = "Creating " + info.Kind.ToString().ToLower(Culture) + " for " + info.Person.VeryFullName;

                        if (firstPage)
                            firstPage = false;
                        else
                            range.BreakPage(forceOddPage: duplexMode);

                        sourceRanges[info.Kind].Copy();
                        range.Paste();

                        populator.Populate(range, info);
                        foreach (Shape shape in range.ShapeRange)
                            populator.Populate(shape.TextFrame.TextRange, info);

                        i++;
                    }
                }
                Word.Activate();
                doc.Activate();
                return doc;
            } finally {
                foreach (var sd in sourceRanges.Values) sd.Document.CloseDoc();
            }
        }
示例#2
0
        public static Document CreateBills(ICollection <WordStatementInfo> statements, IProgressReporter progress, bool duplexMode)
        {
            if (statements == null)
            {
                throw new ArgumentNullException("statements");
            }

            progress = progress ?? new EmptyProgressReporter();

            progress.Caption = "Creating document";

            Dictionary <StatementKind, Range> sourceRanges = new Dictionary <StatementKind, Range>();

            try {
                foreach (var kind in statements.Select(s => s.Kind).Distinct())
                {
                    Document sd = OpenTemplate(kind);
                    // Fix Word 2013 bug
                    // http://blogs.msmvps.com/wordmeister/2013/02/22/word2013bug-not-available-for-reading/
                    sd.ActiveWindow.View.Type = WdViewType.wdPrintView;
                    sourceRanges.Add(kind, sd.Range());
                }

                Document doc = Word.Documents.Add();
                doc.ShowGrammaticalErrors = doc.ShowSpellingErrors = false;
                Range range = doc.Range();

                bool firstPage = true;
                using (new ClipboardScope())
                    using (var populator = new StatementPopulator()) {
                        progress.Maximum = statements.Count;
                        int i = 0;
                        foreach (var info in statements)
                        {
                            if (progress.WasCanceled)
                            {
                                return(null);
                            }
                            progress.Progress = i;

                            progress.Caption = "Creating " + info.Kind.ToString().ToLower(Culture) + " for " + info.Person.VeryFullName;

                            if (firstPage)
                            {
                                firstPage = false;
                            }
                            else
                            {
                                range.BreakPage(forceOddPage: duplexMode);
                            }

                            sourceRanges[info.Kind].Copy();
                            range.Paste();

                            populator.Populate(range, info);
                            foreach (Shape shape in range.ShapeRange)
                            {
                                populator.Populate(shape.TextFrame.TextRange, info);
                            }

                            i++;
                        }
                    }
                Word.Activate();
                doc.Activate();
                return(doc);
            } finally {
                foreach (var sd in sourceRanges.Values)
                {
                    sd.Document.CloseDoc();
                }
            }
        }