private void SetProjectTextWithUrl(string projectName, string title) { Object styleTitle = WdBuiltinStyle.wdStyleTitle; var pt = OfficeHelper.CreateParagraphRange(ref document); pt.Text = projectName + " " + title; pt.set_Style(ref styleTitle); pt.SetRange(pt.Characters.First.Start, pt.Characters.First.Start + projectName.Length); pt.Select(); var link = (tfsManager.tfsVersion == TfsVersion.Tfs2011 ? string.Format("{0}/{1}", TfsManager.Instance.collection.Uri, projectName) : string.Format("{0}/web/", TfsManager.Instance.GetTfsUrl())); pt.Hyperlinks.Add(pt, link); document.Content.InsertParagraphAfter(); var RevisionPar = OfficeHelper.CreateParagraphRange(ref document); if (revisionDate != null) { RevisionPar.Text = string.Format("{0}: {1}", WordToTFS.Properties.Resources.QueryReport_RevisionDate_Text, revisionDate.Value.ToShortDateString()); RevisionPar.Font.Name = "Calibri"; RevisionPar.Font.Size = 11; RevisionPar.Font.Color = WdColor.wdColorBlueGray; RevisionPar.Font.Italic = -1; document.Content.InsertParagraphAfter(); } }
/// <summary> /// Set Matrix Title /// </summary> /// <param name="projectName"> /// </param> /// <param name="title"> /// </param> private void SetMatrixTitle(string projectName, string title) { object styleTitle = WdBuiltinStyle.wdStyleTitle; Range parRange = OfficeHelper.CreateParagraphRange(ref document); parRange.Text = projectName + " " + title; parRange.set_Style(ref styleTitle); parRange.SetRange(parRange.Characters.First.Start, parRange.Characters.First.Start + projectName.Length); parRange.Select(); switch (tfsManager.tfsVersion) { case TfsVersion.Tfs2011: { parRange.Hyperlinks.Add(parRange, string.Format("{0}/{1}", tfsManager.collection.Name, projectName)); } break; case TfsVersion.Tfs2010: { parRange.Hyperlinks.Add(parRange, string.Format("{0}/web/Index.aspx?pguid={1}", tfsManager.GetTfsUrl(), tfsManager.GetProject(projectName).Guid)); } break; } document.Content.InsertParagraphAfter(); string DateFrom = string.Empty; string DateTo = string.Empty; if (matrixReportData.DateFrom != null) { DateFrom = String.Format("{0}: {1}", WordToTFS.Properties.Resources.MatrixReport_Date_From, matrixReportData.DateFrom.Value.ToShortDateString()); } if (matrixReportData.DateTo != null) { DateTo = String.Format("{0}: {1}", WordToTFS.Properties.Resources.MatrixReport_Date_To, matrixReportData.DateTo.Value.ToShortDateString()); } else { DateTo = String.Format("{0}: {1}", WordToTFS.Properties.Resources.MatrixReport_Date_To, DateTime.Now.ToShortDateString()); } if (matrixReportData.DateFrom == null && matrixReportData.DateTo == null) { DateFrom = String.Format("{0}: {1}", WordToTFS.Properties.Resources.MatrixReport_Date, DateTime.Now.ToShortDateString()); DateTo = string.Empty; } Range addInform = OfficeHelper.CreateParagraphRange(ref document); addInform.Font.Name = "Calibri"; addInform.Font.Size = 11; addInform.Font.Color = WdColor.wdColorBlueGray; addInform.Font.Italic = -1; addInform.Text = String.Format("{0}: {1} {2} {3}", WordToTFS.Properties.Resources.MatrixReport_Link_Type, matrixReportData.RelatedSelectedItem, DateFrom, DateTo); addInform.Select(); document.Content.InsertParagraphAfter(); }
private void WriteReport(Action <string> descriptionText, List <string> metaDataSelectedItems, List <string> bodySelectedItems, bool includeAttachments, WorkItem onDateItem, int hierarchyLevel = -1) { WdBuiltinStyle style = hierarchyLevel != -1 ? ParseEnum <WdBuiltinStyle>(string.Format("wdStyleHeading{0}", hierarchyLevel)) : WdBuiltinStyle.wdStyleHeading1; OfficeHelper.SetText(ref document, string.Format("{0} ", onDateItem.Title), style); WriteReportMetadata(metaDataSelectedItems, onDateItem.Fields); WriteReportBodyData(bodySelectedItems, onDateItem.Fields, descriptionText); if (includeAttachments) { InsertAttachment(onDateItem.Attachments); } }
private void SetMetadataText(string text) { Range par = OfficeHelper.CreateParagraphRange(ref document); par.Font.Name = "Calibri"; par.Font.Size = 11; par.Font.Color = WdColor.wdColorBlueGray; par.Font.Italic = -1; par.Text = text; par.ParagraphFormat.SpaceAfter = 0; par.ParagraphFormat.SpaceBefore = 0; document.Content.InsertParagraphAfter(); }
/// <summary> /// Draw Table /// </summary> /// <param name="projectName"> /// </param> /// <param name="workItemsInRow"> /// </param> /// <param name="workItemsInColumn"> /// </param> private void DrawTable(string projectName, List <WorkItem> workItemsInRow, List <WorkItem> workItemsInColumn) { const int TITLE_ROW_INDEX = 1; const int HEADER_ROW_INDEX = 2; const int VERTICAL_STATUS_ROW_INDEX = 3; const int DESCRIPTION_ROW_INDEX = 4; const int PROJECT_COLUMN_INDEX = 1; const int HORIZONTAL_STATUS_COLUMN_INDEX = 2; const int ROW_SHIFT = 5; const int COL_SHIFT = 3; // for additional info see here: "http://msdn.microsoft.com/en-us/library/office/aa537149(v=office.11).aspx" object objDefaultBehaviorWord8 = WdDefaultTableBehavior.wdWord8TableBehavior; object objAutoFitFixed = WdAutoFitBehavior.wdAutoFitFixed; var parRange = OfficeHelper.CreateParagraphRange(ref document); parRange.Text = BuildDataString(workItemsInRow, workItemsInColumn); try { Table table = parRange.ConvertToTable(AutoFitBehavior: objAutoFitFixed, DefaultTableBehavior: objDefaultBehaviorWord8); table.set_Style(WdBuiltinStyle.wdStyleTableLightGrid); table.Select(); Selection sel = document.Application.Selection; if (sel != null) { sel.Font.Bold = 0; sel.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; sel.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; } // style titles sel = SelectCells(table, 1, 3, 1, workItemsInColumn.Count + 2); // 2 cells is a title for items in rows. if (sel != null) { sel.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft; sel.Font.Size = sel.Range.Font.Size - 1; sel.Font.Bold = 1; sel.Orientation = WdTextOrientation.wdTextOrientationUpward; sel.Cells.Height = 100; sel.EscapeKey(); } // Set workItems types as bold sel = SelectCells(table, 1, 1, 4, 1); if (sel != null) { sel.Font.Bold = 1; sel.EscapeKey(); } // Add hyperlinks to work items in column Parallel.For(0, workItemsInColumn.Count, i => table.Columns[i + COL_SHIFT].Cells[2].Range.Hyperlinks.Add(table.Columns[i + COL_SHIFT].Cells[2].Range, tfsManager.GetWorkItemLink(workItemsInColumn[i].Id, projectName))); // Add hyperlinks to work items in row Parallel.For(0, workItemsInRow.Count, i => table.Rows[i + ROW_SHIFT].Cells[1].Range.Hyperlinks.Add(table.Rows[i + ROW_SHIFT].Cells[1].Range, tfsManager.GetWorkItemLink(workItemsInRow[i].Id, projectName))); table.Rows[DESCRIPTION_ROW_INDEX].Cells[PROJECT_COLUMN_INDEX].Merge(table.Rows[DESCRIPTION_ROW_INDEX].Cells[PROJECT_COLUMN_INDEX + 1]); table.Rows[2].Cells[1].Merge(table.Rows[2].Cells[2]); table.Rows[1].Cells[1].Merge(table.Rows[1].Cells[2]); table.Rows[1].Cells[1].Merge(table.Rows[2].Cells[1]); table.Cell(3, 1).Merge(table.Cell(3, 2)); table.Cell(3, 1).Merge(table.Cell(1, 1)); //// Merge rows //table.Rows[4].Cells[1].Merge(table.Rows[4].Cells[2]); //sel = SelectCells(table, 1, 1, 3, 2); //if (sel != null) //{ // sel.Cells.Merge(); //} document.Content.InsertParagraphAfter(); } catch (Exception ex) { } }
private void InsertFile(string path, string originalFileName) { object missing = Type.Missing; object pathObj = path; object classType = "pngfile"; object iconLabel = originalFileName; object iconFilePath = null; var fi = new FileInfo(path); classType = OfficeHelper.GetFileType(fi, false); Paragraph attachmentsParagraph = document.Paragraphs.Add(); attachmentsParagraph.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft; document.Content.InsertParagraphAfter(); object oTrue = -1; //MsoTriState.msoTrue; object oFalse = 0; //MsoTriState.msoFalse; object iconIndexObj = missing; try { string iconPath; int iconIndex; OfficeHelper.ExtractIcon(Path.GetExtension(path), out iconPath, out iconIndex); iconFilePath = Environment.ExpandEnvironmentVariables(iconPath); iconIndexObj = iconIndex; } catch (Exception ex) { MessageBox.Show(ex.Message, ResourceHelper.GetResourceString("ERROR_TEXT"), MessageBoxButton.OK, MessageBoxImage.Error); } if (iconFilePath == null) { try { Icon objectIcon = Icon.ExtractAssociatedIcon(path); iconFilePath = String.Format("{0}.ico", path.Replace(".", "")); using (var iconStream = new FileStream((string)iconFilePath, FileMode.Create)) { objectIcon.Save(iconStream); } } catch (Exception ex) { MessageBox.Show(ex.Message, ResourceHelper.GetResourceString("ERROR_TEXT"), MessageBoxButton.OK, MessageBoxImage.Error); } } try { attachmentsParagraph.Range.InlineShapes.AddOLEObject(ref classType, ref pathObj, ref oFalse, ref oTrue, ref iconFilePath, ref iconIndexObj, ref iconLabel, ref missing); } catch (Exception ex) { MessageBox.Show(ex.Message, ResourceHelper.GetResourceString("ERROR_TEXT"), MessageBoxButton.OK, MessageBoxImage.Error); } }
private void InsertAttachment(AttachmentCollection attachments) { if (attachments.Count != 0) { //SetMetadataText("\tAttachments:"); SetMetadataText("\t" + ResourceHelper.GetResourceString("WORD_REPORT_ATTACHMENTS")); var webClient = new TfsWebClient(tfsManager.collection); string dir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); Directory.CreateDirectory(dir); string zipFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N") + ".zip"); // AttachmentsParagraph = null; try { string filePath = string.Empty; foreach (Attachment attachment in attachments) { string fileName = attachment.Name; filePath = Path.Combine(dir, attachment.Name); //process attachments with same names var counter = 0; while (File.Exists(filePath) && counter < 100) //up to 100 items { counter++; if (counter == 1) { filePath = filePath.Insert(filePath.LastIndexOf(".", StringComparison.Ordinal), string.Format("({0})", counter)); } else { filePath = filePath.Replace(string.Format("({0}).", counter - 1), string.Format("({0}).", counter)); } } try { webClient.DownloadFile(attachment.Uri, filePath); } catch (Exception ex) { MessageBox.Show(ex.Message, ResourceHelper.GetResourceString("ERROR_TEXT"), MessageBoxButton.OK, MessageBoxImage.Error); } } } catch (Exception ex) { MessageBox.Show(ex.Message, ResourceHelper.GetResourceString("ERROR_TEXT"), MessageBoxButton.OK, MessageBoxImage.Error); } finally { OfficeHelper.CompressDirectory(dir, zipFileName); InsertFile(zipFileName, "Attachments.zip"); File.Delete(zipFileName); Directory.Delete(dir, true); } } }