示例#1
0
        private void DisciplineNotification_MailMerge_MergeField(object sender, Aspose.Words.Reporting.MergeFieldEventArgs e)
        {
            #region MailMerge_MergeField
            if (e.FieldName == "懲戒明細")
            {
                object[]      objectValues = (object[])e.FieldValue;
                List <string> eachStudentDisciplineDetail = (List <string>)objectValues[0];

                Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(e.Document);

                builder.MoveToField(e.Field, false);
                builder.StartTable();
                builder.CellFormat.ClearFormatting();
                builder.CellFormat.Borders.ClearFormatting();
                builder.CellFormat.VerticalAlignment = Aspose.Words.CellVerticalAlignment.Center;
                builder.CellFormat.LeftPadding       = 3.0;
                builder.RowFormat.LeftIndent         = 0.0;
                builder.RowFormat.Height             = 15.0;

                int rowNumber = 5;
                if (eachStudentDisciplineDetail.Count > rowNumber * 2)
                {
                    rowNumber  = eachStudentDisciplineDetail.Count / 2;
                    rowNumber += eachStudentDisciplineDetail.Count % 2;
                }

                if (eachStudentDisciplineDetail.Count > rowNumber * 2)
                {
                    rowNumber += (eachStudentDisciplineDetail.Count - (rowNumber * 2)) / 2;
                    rowNumber += (eachStudentDisciplineDetail.Count - (rowNumber * 2)) % 2;
                }

                for (int j = 0; j < rowNumber; j++)
                {
                    builder.InsertCell();
                    builder.CellFormat.Borders.Right.LineStyle = Aspose.Words.LineStyle.Single;
                    builder.CellFormat.Borders.Right.Color     = Color.Black;
                    if (j < eachStudentDisciplineDetail.Count)
                    {
                        builder.Write(eachStudentDisciplineDetail[j]);
                    }
                    builder.InsertCell();
                    if (j + rowNumber < eachStudentDisciplineDetail.Count)
                    {
                        builder.Write(eachStudentDisciplineDetail[j + rowNumber]);
                    }
                    builder.EndRow();
                }

                builder.EndTable();

                e.Text = string.Empty;
            }
            #endregion
        }
        public Aspose.Words.Document GetDocument(string fileName, bool loadLicenseKey = true)
        {
            if (loadLicenseKey)
            {
                new Aspose.Words.License().SetLicense(GetLicenseKeyFilePath());
            }
            Aspose.Words.Document document = null;
            var dir = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(fileName));

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            using (var zip = new ZipFile(fileName)) {
                zip.ExtractAll(dir, ExtractExistingFileAction.OverwriteSilently);
            }
            var info    = new DirectoryInfo(dir);
            var ncxInfo = info.GetFiles("*.ncx", SearchOption.AllDirectories).FirstOrDefault();

            if (ncxInfo != null && ncxInfo.Exists)
            {
                var ncx = GetNcx(ncxInfo);
                if (ncx != null && ncx.Map != null && ncx.Map.Points != null && ncx.Map.Points.Count > 0)
                {
                    var html  = @"<?xml version=""1.0"" encoding=""utf-8""?><html xmlns=""http://www.w3.org/1999/xhtml""><head><title>Tekst</title><style></style></head><body></body></html>";
                    var xhtml = XElement.Parse(html);

                    var baseDirectory = "";
                    var first         = true;
                    foreach (var point in ncx.Map.Points.OrderBy(x => x.Order))
                    {
                        PopulatePoints(ncxInfo, xhtml, ref baseDirectory, ref first, point);
                    }

                    xhtml = RenumerateFootnotes(xhtml);
                    xhtml = RepairLinks(xhtml);
                    xhtml = RepairImages(xhtml);
                    xhtml = RepairChapters(xhtml);
                    xhtml = RepairChapters(xhtml, "numerrozdz", "nanieparzytsa");
                    xhtml = RepairChapters(xhtml, "numerrozdz1", "nanieparzytsa");

                    var xhtmlPath = Path.Combine(ncxInfo.DirectoryName, "TempFile.html");
                    xhtml.Save(xhtmlPath);

                    var options = new Aspose.Words.Loading.LoadOptions {
                        LoadFormat = Aspose.Words.LoadFormat.Html,
                        BaseUri    = baseDirectory
                    };
                    document = new Aspose.Words.Document(xhtmlPath, options);
                    document.Styles.DefaultFont.LocaleId   = (int)Aspose.Words.Loading.EditingLanguage.Polish;
                    document.Styles.DefaultFont.LocaleIdBi = (int)Aspose.Words.Loading.EditingLanguage.Polish;

                    // footnotes
                    var builder = new Aspose.Words.DocumentBuilder(document);
                    var list    = new List <Aspose.Words.Fields.Field>();
                    var list2   = new List <Aspose.Words.Node>();
                    foreach (var field in document.Range.Fields)
                    {
                        if (field.Type == Aspose.Words.Fields.FieldType.FieldHyperlink)
                        {
                            var link = field as Aspose.Words.Fields.FieldHyperlink;
                            if (Regex.IsMatch(link.Result, @"^\[[0-9]+\]"))
                            {
                                var footnotePar = FindParagraph(document, link.Result);
                                if (footnotePar != null)
                                {
                                    list2.Add(footnotePar);
                                    var footnoteText = footnotePar.ToString(Aspose.Words.SaveFormat.Text).Replace(link.Result, "");
                                    builder.MoveToField(field, true);
                                    var f = builder.InsertFootnote(Aspose.Words.Notes.FootnoteType.Footnote, footnoteText.Trim());
                                    f.Font.Size      = 12;
                                    f.Font.Color     = System.Drawing.Color.Black;
                                    f.Font.Bold      = true;
                                    f.Font.Underline = Aspose.Words.Underline.None;
                                    list.Add(field);
                                }
                            }
                        }
                    }

                    if (list.Count > 0)
                    {
                        var _list  = list.ToArray();
                        var length = _list.Length;
                        for (int i = 0; i < length; i++)
                        {
                            _list[i].Remove();
                        }
                    }
                    if (list2.Count > 0)
                    {
                        var _list  = list2.ToArray();
                        var length = _list.Length;
                        for (int i = 0; i < length; i++)
                        {
                            try {
                                (_list[i] as Aspose.Words.Paragraph).Remove();
                            }
                            catch { }
                        }
                    }

                    // headings
                    var chapterTitleStyle = document.Styles.Where(x => x.Name == "chapter_title").FirstOrDefault();
                    if (chapterTitleStyle != null)
                    {
                        chapterTitleStyle.BaseStyleName = document.Styles[Aspose.Words.StyleIdentifier.Heading1].Name;
                    }
                    var subTitleStyle = document.Styles.Where(x => x.Name == "subtitle1" || x.Name == "subtitle").FirstOrDefault();
                    if (subTitleStyle != null)
                    {
                        subTitleStyle.BaseStyleName = document.Styles[Aspose.Words.StyleIdentifier.Heading2].Name;
                    }
                    var subTitle2Style = document.Styles.Where(x => x.Name == "subtitle2").FirstOrDefault();
                    if (subTitle2Style != null)
                    {
                        subTitle2Style.BaseStyleName = document.Styles[Aspose.Words.StyleIdentifier.Heading3].Name;
                    }
                }
            }

            Directory.Delete(dir, true);

            return(document);
        }