public void LoadSourceDocX(string filename) { sourceDocX?.Dispose( ); targetDocX?.Dispose( ); sourceDocX = DocX.Load(filename); targetDocX = sourceDocX.Copy( ); }
/// <summary> /// Crée un document (.docx) à partir d'une templace avec des tokens (mot remplacé, mot remplaçant) /// </summary> /// <param name="dictionnaireTokens">Dictionnaire des mots (remplacé, remplaçant) </param> /// <param name="template">Le doc word template ou aura lieu le remplacement</param> /// <returns>Le chemin complet du document modifié (.docx)</returns> private DocX Generer(Dictionary <String, String> dictionnaireTokens, DocX template) { DocX outputDoc = template.Copy(); for (int i = 0; i < dictionnaireTokens.Count; i++) { outputDoc.ReplaceText(dictionnaireTokens.Keys.ElementAt(i), dictionnaireTokens.Values.ElementAt(i)); } return(outputDoc); }
public override void ExecuteResult(ControllerContext context) { var response = context.HttpContext.Response; if (Meetingid.HasValue) { meeting = DbUtil.Db.Meetings.Single(mt => mt.MeetingId == Meetingid); Debug.Assert(meeting.MeetingDate != null, "meeting.MeetingDate != null"); NewMeetingInfo = new NewMeetingInfo { MeetingDate = meeting.MeetingDate.Value }; } var list1 = NewMeetingInfo.ByGroup ? ReportList2().ToList() : ReportList().ToList(); if (!list1.Any()) { response.Write("no data found"); return; } var bytes = RollsheetTemplate() ?? Resource1.DocxRollsheet; var ms = new MemoryStream(bytes); docx = DocX.Load(ms); replacements = new EmailReplacements(DbUtil.Db, docx); var sources = new List <Source>(); foreach (var o in list1) { curr = docx.Copy(); foreach (var p in curr.Headers.odd.Paragraphs) { DoHeaderFooterParagraphReplacments(p, o); } foreach (var p in curr.Footers.odd.Paragraphs) { DoHeaderFooterParagraphReplacments(p, o); } var tbl = curr.Tables[0]; var emptyrow = tbl.InsertRow(); tbl.Rows.Add(emptyrow); tbl.RemoveRow(0); if (meeting != null) { var q = from at in meeting.Attends where at.AttendanceFlag || AttendCommitmentCode.committed.Contains(at.Commitment ?? 0) orderby at.Person.LastName, at.Person.FamilyId, at.Person.Name2 select new RollsheetPersonInfo() { Person = at.Person, MemberTypeCode = at.MemberType.Code, }; foreach (var m in q) { AddRowWithReplacements(tbl, m, meeting.OrganizationId); } } else if (OrgSearchModel != null) { var q = from om in DbUtil.Db.OrganizationMembers where om.OrganizationId == o.OrgId join m in DbUtil.Db.OrgPeople(o.OrgId, o.Groups) on om.PeopleId equals m.PeopleId where om.EnrollmentDate <= Util.Now orderby om.Person.LastName, om.Person.FamilyId, om.Person.Name2 let p = om.Person let useAltName = NewMeetingInfo.UseAltNames && p.AltName != null && p.AltName.Length > 0 select new RollsheetPersonInfo() { Person = p, MemberTypeCode = om.MemberType.Code, UseAltName = useAltName, Highlight = om.OrgMemMemTags.Any(mm => mm.MemberTag.Name == NewMeetingInfo.HighlightGroup) ? NewMeetingInfo.HighlightGroup : "" }; foreach (var m in q) { AddRowWithReplacements(tbl, m, o.OrgId); } } else if (Filter?.GroupSelect == GroupSelectCode.Member) { var q = from om in DbUtil.Db.OrganizationMembers where om.OrganizationId == Filter.Id join m in DbUtil.Db.OrgFilterPeople(QueryId, null) on om.PeopleId equals m.PeopleId where om.EnrollmentDate <= Util.Now where NewMeetingInfo.ByGroup == false || m.Groups.Contains((char)10 + o.Groups + (char)10) orderby om.Person.LastName, om.Person.FamilyId, om.Person.Name2 let p = om.Person let useAltName = NewMeetingInfo.UseAltNames && p.AltName != null && p.AltName.Length > 0 select new RollsheetPersonInfo() { Person = p, MemberTypeCode = om.MemberType.Code, UseAltName = useAltName, Highlight = om.OrgMemMemTags.Any(mm => mm.MemberTag.Name == NewMeetingInfo.HighlightGroup) ? NewMeetingInfo.HighlightGroup : "" }; foreach (var m in q) { AddRowWithReplacements(tbl, m, o.OrgId); } } else { var q = from m in DbUtil.Db.OrgFilterPeople(QueryId, null) orderby m.Name2 let p = DbUtil.Db.People.Single(pp => pp.PeopleId == m.PeopleId) let om = p.OrganizationMembers.SingleOrDefault(mm => mm.OrganizationId == Filter.Id) let useAltName = NewMeetingInfo.UseAltNames && p.AltName != null && p.AltName.Length > 0 select new RollsheetPersonInfo { Person = p, MemberTypeCode = om == null ? "Guest" : om.MemberType.Code, UseAltName = useAltName, Highlight = om.OrgMemMemTags.Any(mm => mm.MemberTag.Name == NewMeetingInfo.HighlightGroup) ? NewMeetingInfo.HighlightGroup : "" }; foreach (var m in q) { AddRowWithReplacements(tbl, m, o.OrgId); } } if ((OrgSearchModel != null && NewMeetingInfo.ByGroup == false) || (Filter != null && Filter.GroupSelect == GroupSelectCode.Member && meeting == null && !Filter.SgFilter.HasValue() && !Filter.NameFilter.HasValue() && !Filter.FilterIndividuals == true && !Filter.FilterTag == true && NewMeetingInfo.ByGroup == false)) { var q = from vp in DbUtil.Db.OrgVisitorsAsOfDate(o.OrgId, NewMeetingInfo.MeetingDate, NoCurrentMembers: true) let p = DbUtil.Db.People.Single(pp => pp.PeopleId == vp.PeopleId) orderby p.LastName, p.FamilyId, p.PreferredName select new RollsheetPersonInfo { Person = p, MemberTypeCode = vp.VisitorType }; foreach (var m in q) { AddRowWithReplacements(tbl, m, o.OrgId); } } curr.Tables[0].RemoveRow(0); { var memStream = new MemoryStream(); curr.SaveAs(memStream); memStream.Position = 0; var wmlDocument = new WmlDocument(null, memStream); sources.Add(new Source(wmlDocument, keepSections: true)); } } context.HttpContext.Response.Clear(); context.HttpContext.Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; context.HttpContext.Response.AddHeader("Content-Disposition", $"attachment;filename=rollsheet.docx"); var finaldoc = DocumentBuilder.BuildDocument(sources); context.HttpContext.Response.OutputStream.Write(finaldoc.DocumentByteArray, 0, finaldoc.DocumentByteArray.Length); }
//實例化模板 public void NewPartDocument() { this.partDocument = partDocumentModel.Copy(); }
static void Main(string[] args) { DocX sourceDoc = DocX.Load(@"E:/temp.docx"); string text = sourceDoc.Text; Console.WriteLine("Document size is {0}", text.Length); string encryptedText = "Hello world"; byte[] encryptedBytes = Encoding.ASCII.GetBytes(encryptedText); bool[] encryptedBits = Utils.ConvertToBits(encryptedBytes); if (encryptedBits.Length > text.Length) { throw new Exception("Encrypted text is larger than container"); } ContainerElement[] container = new ContainerElement[text.Length]; var shuffle = Utils.GetShuffled(container.Length); for (var i = 0; i < encryptedBits.Length; i++) { container[shuffle[i]] = encryptedBits[i] ? ContainerElement.Pos : ContainerElement.Neg; } Console.WriteLine("Start encrypting"); //DocX resultDoc = DocX.Create(@"E:/tempRes.docx"); DocX resultDoc = sourceDoc.Copy( ); foreach (var a in resultDoc.Paragraphs) { resultDoc.RemoveParagraph(a); } var lastContainerIndex = 0; foreach (var sourceParagraph in sourceDoc.Paragraphs) { Paragraph newParagraph = resultDoc.InsertParagraph( ); foreach (var paragraphPart in sourceParagraph.MagicText) { Paragraph append(string textPart) { if (paragraphPart.formatting == null) { return(newParagraph.Append(textPart)); } return(newParagraph.Append(textPart, paragraphPart.formatting)); } ContainerElement lastUsedElement = ContainerElement.Any; int firstCharIndex = 0; for (var j = 0; j < paragraphPart.text.Length; j++) { var containerIndex = lastContainerIndex + j; if (container[containerIndex] != ContainerElement.Any) { if (lastUsedElement != ContainerElement.Any) { if (lastUsedElement != container[containerIndex]) { append(paragraphPart.text.Substring(firstCharIndex, j - firstCharIndex - 1)).Spacing(GenerateSpacing(paragraphPart, container[containerIndex] == ContainerElement.Neg)); firstCharIndex = j; } } lastUsedElement = container[containerIndex]; } } if (firstCharIndex < paragraphPart.text.Length) { append(paragraphPart.text.Substring(firstCharIndex, paragraphPart.text.Length - firstCharIndex)).Spacing(GenerateSpacing(paragraphPart, lastUsedElement == ContainerElement.Neg)); } lastContainerIndex += paragraphPart.text.Length; } newParagraph.StyleName = sourceParagraph.StyleName; } Console.WriteLine("Saving"); resultDoc.SaveAs(@"E:/tempRes.docx"); Console.WriteLine("Press any key to exit..."); #if DEBUG Console.ReadKey( ); #endif }