public override void ExecuteResult(ControllerContext context) { var ctl = new MailingController { UseTitles = titles ?? false, UseMailFlags = useMailFlags ?? false }; var Response = context.HttpContext.Response; IEnumerable <MailingController.MailingInfo> q = null; switch (format) { case "Individual": q = ctl.FetchIndividualList(sort, id); break; case "GroupAddress": q = ctl.GroupByAddress(sort, id); break; case "Family": case "FamilyMembers": q = ctl.FetchFamilyList(sort, id); break; case "ParentsOf": q = ctl.FetchParentsOfList(sort, id); break; case "CouplesEither": q = ctl.FetchCouplesEitherList(sort, id); break; case "CouplesBoth": q = ctl.FetchCouplesBothList(sort, id); break; default: Response.Write("unknown format"); return; } if (!q.Any()) { Response.Write("no data found"); return; } using (var ms = new MemoryStream()) { var dd = DocX.Create("ttt.docx"); dd.MarginLeft = 30; dd.MarginRight = 24; dd.MarginTop = 48; dd.MarginBottom = 30; dd.PageHeight = 1056; dd.PageWidth = 816; var col = 0; var row = 0; Table tt = null; foreach (var p in q) { if (tt == null || col == 0 && row == 0) { tt = dd.InsertTable(10, 5); foreach (var rr in tt.Rows) { for (var i = 0; i < 5; i++) { rr.Cells[i].VerticalAlignment = VerticalAlignment.Center; rr.Height = 96.0; rr.Cells[i].Width = i % 2 == 0 ? 252.4667 : 11.4; if (i % 2 == 0) { rr.Cells[i].MarginLeft = 30; } } } } if (skip > 0) { row = skip / 3; col = skip % 3; if (col > 0) { col++; } if (col > 2) { col++; } } var c = tt.Rows[row].Cells[col]; if (format == "GroupAddress") { c.Paragraphs[0].InsertText(p.LabelName + " " + p.LastName); } else if ((format == "CouplesEither" || format == "CouplesBoth") && p.CoupleName.HasValue()) { c.Paragraphs[0].InsertText(p.CoupleName); } else { c.Paragraphs[0].InsertText(p.LabelName); } if (p.MailingAddress.HasValue()) { c.InsertParagraph(p.MailingAddress.Trim()); } else { c.InsertParagraph(p.Address); if (p.Address2.HasValue()) { c.InsertParagraph(p.Address2); } c.InsertParagraph(p.CSZ); } col += 2; if (col == 6) { row++; col = 0; if (row == 10) { row = 0; } } } dd.SaveAs(ms); Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; Response.AddHeader("content-disposition", "filename=avery-labels.docx"); Response.AddHeader("content-length", ms.Length.ToString()); Response.BinaryWrite(ms.ToArray()); Response.End(); } }
public override void ExecuteResult(ControllerContext context) { var ctl = new MailingController { UseTitles = titles ?? false, UseMailFlags = useMailFlags ?? false }; var Response = context.HttpContext.Response; IEnumerable <MailingController.MailingInfo> q = null; switch (format) { case "Individual": q = ctl.FetchIndividualList(sort, id); break; case "GroupAddress": q = ctl.GroupByAddress(sort, id); break; case "Family": case "FamilyMembers": q = ctl.FetchFamilyList(sort, id); break; case "ParentsOf": q = ctl.FetchParentsOfList(sort, id); break; case "CouplesEither": q = ctl.FetchCouplesEitherList(sort, id); break; case "CouplesBoth": q = ctl.FetchCouplesBothList(sort, id); break; default: Response.Write("unknown format"); return; } if (!q.Any()) { Response.Write("no data found"); return; } Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "filename=foo.pdf"); var document = new Document(PageSize.LETTER); document.SetMargins(50f, 36f, 32f, 36f); var w = PdfWriter.GetInstance(document, Response.OutputStream); document.Open(); dc = w.DirectContent; var cols = new float[] { W, W, W - 25f }; var t = new PdfPTable(cols); t.SetTotalWidth(cols); t.HorizontalAlignment = Element.ALIGN_CENTER; t.LockedWidth = true; t.DefaultCell.Border = PdfPCell.NO_BORDER; t.DefaultCell.FixedHeight = H; t.DefaultCell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; t.DefaultCell.PaddingLeft = 8f; t.DefaultCell.PaddingRight = 8f; t.DefaultCell.SetLeading(2.0f, 1f); if (skip > 0) { var blankCell = new PdfPCell(t.DefaultCell); for (int iX = 0; iX < skip; iX++) { t.AddCell(blankCell); } } foreach (var m in q) { var c = new PdfPCell(t.DefaultCell); var ph = new Paragraph(); if (format == "GroupAddress") { ph.AddLine(m.LabelName + " " + m.LastName, font); } else if ((format == "CouplesEither" || format == "CouplesBoth") && m.CoupleName.HasValue()) { ph.AddLine(m.CoupleName, font); } else { ph.AddLine(m.LabelName, font); } if (m.MailingAddress.HasValue()) { ph.AddLine(m.MailingAddress.Trim(), font); } else { ph.AddLine(m.Address, font); ph.AddLine(m.Address2, font); ph.AddLine(m.CSZ, font); } c.AddElement(ph); if (usephone) { var phone = Util.PickFirst(m.CellPhone.FmtFone("C "), m.HomePhone.FmtFone("H ")); var p = new Paragraph(); c.PaddingRight = 7f; p.Alignment = Element.ALIGN_RIGHT; p.Add(new Chunk(phone, smfont)); p.ExtraParagraphSpace = 0f; c.AddElement(p); } t.AddCell(c); } t.CompleteRow(); document.Add(t); document.Close(); }