public string NewDoc(string pathTemplate, string pathFile, string lastName, string firstName, string middleName, string birthDate, int loanSum, string inputImage) { if (!File.Exists(pathTemplate)) { return("Папка не найдена, обратитесь к администратору или выберите другую"); } string imagesFolder = AppDomain.CurrentDomain.BaseDirectory + "Resources\\Images\\"; Document document = new Document(); document.LoadFromFile(pathTemplate); TextSelection[] selections = document.FindAllString("[image]", true, true); int index = 0; TextRange range = null; foreach (TextSelection selection in selections) { DocPicture pic = new DocPicture(document); try { pic.LoadImage(Image.FromFile(imagesFolder + inputImage)); } catch { System.Threading.Thread.Sleep(2000); pic.LoadImage(Image.FromFile(imagesFolder + inputImage)); } pic.Width = 250; pic.Height = 268; range = selection.GetAsOneRange(); index = range.OwnerParagraph.ChildObjects.IndexOf(range); range.OwnerParagraph.ChildObjects.Insert(index, pic); range.OwnerParagraph.ChildObjects.Remove(range); } document.Replace("[lastName]", lastName, false, true); document.Replace("[firstName]", firstName, false, true); document.Replace("[middleName]", middleName, false, true); document.Replace("[birthDate]", birthDate.Remove(birthDate.IndexOf(" ")), false, true); document.Replace("[loanSum]", loanSum.ToString(), false, true); document.SaveToFile(pathFile, FileFormat.Docx); document.Close(); Deleter img = new Deleter(); img.DeleteFiles(imagesFolder); return(pathFile); }
private void button1_Click(object sender, EventArgs e) { //Load the document from disk. Document doc = new Document(); doc.LoadFromFile(@"..\..\..\..\..\..\Data\Comment.docx"); //get the first comment. Comment comment1 = doc.Comments[0]; //create a new comment and specify the author and content. Comment replyComment1 = new Comment(doc); replyComment1.Format.Author = "E-iceblue"; replyComment1.Body.AddParagraph().AppendText("Spire.Doc is a professional Word .NET library on operating Word documents."); //add the new comment as a reply to the selected comment. comment1.ReplyToComment(replyComment1); DocPicture docPicture = new DocPicture(doc); Image img = Image.FromFile(@"..\..\..\..\..\..\Data\logo.png"); docPicture.LoadImage(img); //insert a picture in the comment replyComment1.Body.Paragraphs[0].ChildObjects.Add(docPicture); //Save the document. doc.SaveToFile("ReplyToComment.docx", FileFormat.Docx); //Launch the Word file. WordDocViewer("ReplyToComment.docx"); }
private void button1_Click(object sender, EventArgs e) { //Load Document string input = @"..\..\..\..\..\..\Data\BlankTemplate.docx"; Document doc = new Document(); doc.LoadFromFile(input); Section section = doc.Sections[0]; //Add a paragraph Paragraph paragraph = section.AddParagraph(); //Load an image to a DocPicture object Image image = Image.FromFile(@"..\..\..\..\..\..\Data\Spire.Doc.png"); DocPicture picture = new DocPicture(doc); //Add an image hyperlink to the paragraph picture.LoadImage(image); paragraph.AppendHyperlink("https://www.e-iceblue.com/Introduce/word-for-net-introduce.html", picture, HyperlinkType.WebLink); //Save and launch document string output = "CreateImageHyperlink.docx"; doc.SaveToFile(output, FileFormat.Docx); Viewer(output); }
private void button1_Click(object sender, EventArgs e) { //Load the document string input = @"..\..\..\..\..\..\Data\CommentTemplate.docx"; Document doc = new Document(); doc.LoadFromFile(input); //Get the first paragraph and insert comment Paragraph paragraph = doc.Sections[0].Paragraphs[2]; Comment comment = paragraph.AppendComment("This is a comment."); comment.Format.Author = "E-iceblue"; //Load a picture DocPicture docPicture = new DocPicture(doc); Image img = Image.FromFile(@"..\..\..\..\..\..\Data\E-iceblue.png"); docPicture.LoadImage(img); //Insert the picture into the comment body comment.Body.AddParagraph().ChildObjects.Add(docPicture); //Save and launch string output = "InsertPictureIntoComment.docx"; doc.SaveToFile(output, FileFormat.Docx); Viewer(output); }
private void button1_Click(object sender, EventArgs e) { //Load Document string input = @"..\..\..\..\..\..\Data\Template.docx"; Document doc = new Document(); doc.LoadFromFile(input); //Load the image Image image = Image.FromFile(@"..\..\..\..\..\..\Data\E-iceblue.png"); //Find the string "E-iceblue" in the document TextSelection[] selections = doc.FindAllString("E-iceblue", true, true); int index = 0; TextRange range = null; //Remove the text and replace it with Image foreach (TextSelection selection in selections) { DocPicture pic = new DocPicture(doc); pic.LoadImage(image); range = selection.GetAsOneRange(); index = range.OwnerParagraph.ChildObjects.IndexOf(range); range.OwnerParagraph.ChildObjects.Insert(index, pic); range.OwnerParagraph.ChildObjects.Remove(range); } //Save and launch document string output = "ReplaceWithImage.docx"; doc.SaveToFile(output, FileFormat.Docx); Viewer(output); }
internal string method_8(byte[] A_0, ShapeObject A_1) { if (A_0 == null) { return(null); } DocPicture picture = new DocPicture(A_1.Document); picture.LoadImage(A_0); picture.documentObject_1 = A_1.Owner; return(this.method_9(picture)); }
private void button1_Click(object sender, EventArgs e) { Document doc = new Document(); doc.LoadFromFile(@"..\..\..\..\..\..\..\Data\ToEpub.doc"); DocPicture picture = new DocPicture(doc); picture.LoadImage(Image.FromFile(@"..\..\..\..\..\..\..\Data\Cover.png")); string result = "AddCoverImage.epub"; doc.SaveToEpub(result, picture); WordDocViewer(result); }
private void button1_Click(object sender, EventArgs e) { //Load Document string input = @"..\..\..\..\..\..\Data\BlankTemplate.docx"; Document doc = new Document(); doc.LoadFromFile(input); Section section = doc.Sections[0]; Paragraph paragraph = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph(); paragraph.AppendText("The sample demonstrates how to insert an image into a document."); paragraph.ApplyStyle(BuiltinStyle.Heading2); paragraph = section.AddParagraph(); paragraph.AppendText("The above is a picture."); //get original image Bitmap p = new Bitmap(Image.FromFile(@"..\..\..\..\..\..\Data\Word.png")); //rotate image and insert image to word document p.RotateFlip(RotateFlipType.Rotate90FlipX); //Create a picture DocPicture picture = new DocPicture(doc); picture.LoadImage(p); //set image's position picture.HorizontalPosition = 50.0F; picture.VerticalPosition = 60.0F; //set image's size picture.Width = 200; picture.Height = 200; //set textWrappingStyle with image; picture.TextWrappingStyle = TextWrappingStyle.Through; //Insert the picture at the beginning of the second paragraph paragraph.ChildObjects.Insert(0, picture); //Save and launch document string output = "InsertImageAtSpecifiedLocation.docx"; doc.SaveToFile(output, FileFormat.Docx); Viewer(output); }
private void button1_Click(object sender, EventArgs e) { //create a document Document doc = new Document(); //add a section Section sec = doc.AddSection(); //add a paragraph Paragraph par = sec.AddParagraph(); //load the image DocPicture picture = new DocPicture(doc); Image image = Image.FromFile(@"..\..\..\..\..\..\Data\excel.png"); picture.LoadImage(image); //insert the OLE DocOleObject obj = par.AppendOleObject(@"..\..\..\..\..\..\Data\example.xlsx", picture, OleObjectType.ExcelWorksheet); doc.SaveToFile("InsertOLE.docx", FileFormat.Docx2013); FileViewer("InsertOLE.docx"); }
private static void RepalcePicture(Document doc, TargetRect item, string picPath) { if (!File.Exists(picPath)) { throw new MyException("报告生成失败,未找到对应图片!"); } DocPicture pic = new DocPicture(doc); string ss = "Illustration" + item.id.ToString(); TextSelection selection = doc.FindString(ss, true, true); if (selection.Count < 1) { return; } pic.LoadImage(Image.FromFile(picPath)); pic.Width = 250; pic.Height = 240; TextRange range = selection.GetAsOneRange(); int index = range.OwnerParagraph.ChildObjects.IndexOf(range); range.OwnerParagraph.ChildObjects.Insert(index, pic); range.OwnerParagraph.ChildObjects.Remove(range); }
private void button1_Click(object sender, EventArgs e) { //Load Document string input = @"..\..\..\..\..\..\Data\ImageTemplate.docx"; Document doc = new Document(); doc.LoadFromFile(input); //Get all pictures in the Word document List <DocumentObject> pictures = new List <DocumentObject>(); foreach (Section sec in doc.Sections) { foreach (Paragraph para in sec.Paragraphs) { foreach (DocumentObject docObj in para.ChildObjects) { if (docObj.DocumentObjectType == DocumentObjectType.Picture) { pictures.Add(docObj); } } } } //Replace the first picture with a new image file DocPicture picture = pictures[0] as DocPicture; picture.LoadImage(Image.FromFile(@"..\..\..\..\..\..\Data\E-iceblue.png")); //Save and launch document string output = "ReplaceWithNewImage.docx"; doc.SaveToFile(output, FileFormat.Docx); Viewer(output); }
private void button1_Click(object sender, EventArgs e) { //Creat a new word document. Document document = new Document(); Section section = document.AddSection(); Paragraph paragraph = section.AddParagraph(); TextRange txtRange = paragraph.AppendText("The following example shows how to add content controls in a Word document."); paragraph = section.AddParagraph(); //Add Combo Box Content Control. paragraph = section.AddParagraph(); txtRange = paragraph.AppendText("Add Combo Box Content Control: "); txtRange.CharacterFormat.Italic = true; StructureDocumentTagInline sd = new StructureDocumentTagInline(document); paragraph.ChildObjects.Add(sd); sd.SDTProperties.SDTType = SdtType.ComboBox; SdtComboBox cb = new SdtComboBox(); cb.ListItems.Add(new SdtListItem("Spire.Doc")); cb.ListItems.Add(new SdtListItem("Spire.XLS")); cb.ListItems.Add(new SdtListItem("Spire.PDF")); sd.SDTProperties.ControlProperties = cb; TextRange rt = new TextRange(document); rt.Text = cb.ListItems[0].DisplayText; sd.SDTContent.ChildObjects.Add(rt); section.AddParagraph(); //Add Text Content Control. paragraph = section.AddParagraph(); txtRange = paragraph.AppendText("Add Text Content Control: "); txtRange.CharacterFormat.Italic = true; sd = new StructureDocumentTagInline(document); paragraph.ChildObjects.Add(sd); sd.SDTProperties.SDTType = SdtType.Text; SdtText text = new SdtText(true); text.IsMultiline = true; sd.SDTProperties.ControlProperties = text; rt = new TextRange(document); rt.Text = "Text"; sd.SDTContent.ChildObjects.Add(rt); section.AddParagraph(); //Add Picture Content Control. paragraph = section.AddParagraph(); txtRange = paragraph.AppendText("Add Picture Content Control: "); txtRange.CharacterFormat.Italic = true; sd = new StructureDocumentTagInline(document); paragraph.ChildObjects.Add(sd); sd.SDTProperties.SDTType = SdtType.Picture; DocPicture pic = new DocPicture(document); pic.Width = 10; pic.Height = 10; pic.LoadImage(Image.FromFile(@"..\..\..\..\..\..\Data\logo.png")); sd.SDTContent.ChildObjects.Add(pic); section.AddParagraph(); //Add Date Picker Content Control. paragraph = section.AddParagraph(); txtRange = paragraph.AppendText("Add Date Picker Content Control: "); txtRange.CharacterFormat.Italic = true; sd = new StructureDocumentTagInline(document); paragraph.ChildObjects.Add(sd); sd.SDTProperties.SDTType = SdtType.DatePicker; SdtDate date = new SdtDate(); date.CalendarType = CalendarType.Default; date.DateFormat = "yyyy.MM.dd"; date.FullDate = DateTime.Now; sd.SDTProperties.ControlProperties = date; rt = new TextRange(document); rt.Text = "1990.02.08"; sd.SDTContent.ChildObjects.Add(rt); section.AddParagraph(); //Add Drop-Down List Content Control. paragraph = section.AddParagraph(); txtRange = paragraph.AppendText("Add Drop-Down List Content Control: "); txtRange.CharacterFormat.Italic = true; sd = new StructureDocumentTagInline(document); paragraph.ChildObjects.Add(sd); sd.SDTProperties.SDTType = SdtType.DropDownList; SdtDropDownList sddl = new SdtDropDownList(); sddl.ListItems.Add(new SdtListItem("Harry")); sddl.ListItems.Add(new SdtListItem("Jerry")); sd.SDTProperties.ControlProperties = sddl; rt = new TextRange(document); rt.Text = sddl.ListItems[0].DisplayText; sd.SDTContent.ChildObjects.Add(rt); //Save the document. string resultfile = "Output.docx"; document.SaveToFile(resultfile, FileFormat.Docx); //Launch the Word file. FileViewer(resultfile); }
/// <summary> /// 透過既有的套印檔匯出 Word 文件 (以「取代文字」方式套印) /// </summary> /// <param name="result">回傳: 執行結果</param> /// <param name="msg">回傳: 訊息</param> /// <returns>串流資訊</returns> public byte[] ExportResumeByDocx_ReplaceText(out bool result, out string msg) { result = true; msg = ""; MemoryStream ms = new MemoryStream(); try { Spire.Doc.Document document = new Spire.Doc.Document(); //載入套印檔 //注意: 實際運作時,若同一時間有兩位以上使用者同時進行套印,會產生「無法開啟已開啟檔案」的錯誤 //建議實作時,一個使用者執行匯出動作時先複製一個套印檔,完成套印後再將複製的檔案刪除,即可避開錯誤 document.LoadFromFile(HttpContext.Current.Server.MapPath("~/App_Data/MyResumeSample.docx")); #region 定義樣式 //定義樣式 BasicStyle: 一般段落文字 ParagraphStyle style = new ParagraphStyle(document) { Name = "Basic" }; //style.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Justify; style.CharacterFormat.FontName = "標楷體"; style.CharacterFormat.FontSize = 12; document.Styles.Add(style); #endregion //取得要套印的內容 Resume model = new Resume(); #region 套印內容 document.Replace("{$Name$}", string.IsNullOrEmpty(model.Name) ? "" : model.Name, false, true); document.Replace("{$Gender$}", string.IsNullOrEmpty(model.Gender) ? "" : model.Gender, false, true); document.Replace("{$Email$}", string.IsNullOrEmpty(model.Email) ? "" : model.Email, false, true); document.Replace("{$Address$}", string.IsNullOrEmpty(model.Address) ? "" : model.Address, false, true); document.Replace("{$Phone$}", string.IsNullOrEmpty(model.Phone) ? "" : model.Phone, false, true); document.Replace("{$Mobile$}", string.IsNullOrEmpty(model.Mobile) ? "" : model.Mobile, false, true); //包含 HTML 字串需放置在 paragraph 內, //因此套印檔中的 {$Description1$} 及 {$Description2$} 需透過「以 paragraph 取代文字」方式替代 //Replace {$Description1$} with paragraph TextSelection selection = document.FindString("{$Description1$}", false, true); TextRange range = selection.GetAsOneRange(); Spire.Doc.Documents.Paragraph paragraph = range.OwnerParagraph; paragraph.ApplyStyle("Basic"); paragraph.Replace("{$Description1$}", "", false, false); paragraph.AppendHTML(string.IsNullOrEmpty(model.Description1) ? "" : HttpUtility.HtmlDecode(model.Description1)); //Replace {$Description2$} with paragraph selection = document.FindString("{$Description2$}", false, true); range = selection.GetAsOneRange(); paragraph = range.OwnerParagraph; paragraph.ApplyStyle("Basic"); paragraph.Replace("{$Description2$}", "", false, false); paragraph.AppendHTML(string.IsNullOrEmpty(model.Description2) ? "" : HttpUtility.HtmlDecode(model.Description2)); //Replace {$Img$} with Image DocPicture pic = new DocPicture(document); pic.LoadImage(Image.FromFile(HttpContext.Current.Server.MapPath("~/App_Data/Penguins.jpg"))); selection = document.FindString("{$Img$}", false, true); range = selection.GetAsOneRange(); range.OwnerParagraph.ChildObjects.Insert(0, pic); range.OwnerParagraph.ChildObjects.Remove(range); #endregion #region 動態新增表格 if (model.JobHistory.Count > 0) { Spire.Doc.Section s = document.AddSection(); Spire.Doc.Table table = s.AddTable(true); string[] Header = { "序號", "任職公司", "職稱", "開始時間", "結束時間" }; //Add Cells table.ResetCells(model.JobHistory.Count + 1, Header.Length); //Header Row TableRow FRow = table.Rows[0]; FRow.IsHeader = true; for (int i = 0; i < Header.Length; i++) { Spire.Doc.Documents.Paragraph p = FRow.Cells[i].AddParagraph(); FRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle; p.Format.HorizontalAlignment = HorizontalAlignment.Center; TextRange TR = p.AppendText(Header[i]); TR.CharacterFormat.Bold = true; } //Data Row model.JobHistory = model.JobHistory.OrderBy(x => x.StartDT).ToList(); for (int r = 0; r < model.JobHistory.Count; r++) { TableRow DataRow = table.Rows[r + 1]; string[] data = new string[] { (r + 1).ToString(), model.JobHistory[r].CompanyName, model.JobHistory[r].JobTitle, (model.JobHistory[r].StartDT.HasValue ? model.JobHistory[r].StartDT.Value.ToShortDateString() : ""), (model.JobHistory[r].EndDT.HasValue ? model.JobHistory[r].EndDT.Value.ToShortDateString() : "") }; //Columns. for (int c = 0; c < data.Length; c++) { //Cell Alignment DataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle; //Fill Data in Rows Spire.Doc.Documents.Paragraph p2 = DataRow.Cells[c].AddParagraph(); TextRange TR2 = p2.AppendText(data[c]); //Format Cells p2.Format.HorizontalAlignment = HorizontalAlignment.Center; } } //Replace text with Table TextSelection selectionTable = document.FindString("{$JobHistory$}", true, true); TextRange rangeTable = selectionTable.GetAsOneRange(); Spire.Doc.Documents.Paragraph paragraphTable = rangeTable.OwnerParagraph; Body body = paragraphTable.OwnerTextBody; int index = body.ChildObjects.IndexOf(paragraphTable); body.ChildObjects.Remove(paragraphTable); body.ChildObjects.Insert(index, table); } #endregion #region 套用樣式 //套用文章段落樣式 for (int s = 0; s < document.Sections.Count; s++) { Spire.Doc.Section section = document.Sections[s]; //套用文章段落樣式 for (int p = 0; p < section.Paragraphs.Count; p++) { Spire.Doc.Documents.Paragraph pgh = section.Paragraphs[p]; pgh.ApplyStyle("Basic"); pgh.Format.BeforeSpacing = 12; } //套用表格樣式 for (int t = 0; t < document.Sections[s].Tables.Count; t++) { Spire.Doc.Table table = (Spire.Doc.Table)document.Sections[s].Tables[t]; table.PreferredWidth = new PreferredWidth(WidthType.Percentage, 100); table.TableFormat.IsAutoResized = true; //set table border //table.TableFormat.Borders.Right.BorderType = Spire.Doc.Documents.BorderStyle.Thick; //table.TableFormat.Borders.Left.BorderType = Spire.Doc.Documents.BorderStyle.Thick; //table.TableFormat.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.Thick; //table.TableFormat.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.Thick; //table.TableFormat.Borders.Horizontal.BorderType = Spire.Doc.Documents.BorderStyle.Thick; //table.TableFormat.Borders.Vertical.BorderType = Spire.Doc.Documents.BorderStyle.Thick; for (int tr = 0; tr < table.Rows.Count; tr++) { for (int td = 0; td < table.Rows[tr].Cells.Count; td++) { for (int t_ph = 0; t_ph < table.Rows[tr].Cells[td].Paragraphs.Count; t_ph++) { table.Rows[tr].Cells[td].Paragraphs[t_ph].ApplyStyle("Basic"); } } } } } #endregion //匯出 document.SaveToStream(ms, FileFormat.Docx); } catch (Exception ex) { result = false; msg = ex.Message; } if (result) { return(ms.ToArray()); } else { return(null); } }