/// <summary> /// Find Text and replace it with a Picture using ContentRange. /// </summary> /// <remarks> /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/find-replace-content-net-csharp-vb.php /// </remarks> public static void FindTextAndReplaceImage() { // Path to a loadable document. string loadPath = @"..\..\Critique_signature.docx"; string pictPath = @"..\..\Smile.png"; // Load a document intoDocumentCore. DocumentCore dc = DocumentCore.Load(loadPath); //Find "<signature>" Text and Replace everywhere with the "Smile.png" // Please note, Reverse() makes sure that action replace not affects to Find(). Regex regex = new Regex(@"<signature>", RegexOptions.IgnoreCase); Picture picture = new Picture(dc, InlineLayout.Inline(new Size(50, 50)), pictPath); foreach (ContentRange item in dc.Content.Find(regex).Reverse()) { item.Replace(picture.Content); } // Save our document into PDF format. string savePath = @"..\..\Replaced_signature.pdf"; dc.Save(savePath, new PdfSaveOptions()); // Open the result for demonstration purposes. System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(loadPath) { UseShellExecute = true }); System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(savePath) { UseShellExecute = true }); }
/// <summary> /// Inserting a Rich text content control. /// </summary> /// <remarks> /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/content-controls-insert-rich-text-net-csharp-vb.php /// </remarks> static void InsertRichText() { // Let's create a simple document. DocumentCore dc = new DocumentCore(); // Create a rich text content control. BlockContentControl rt = new BlockContentControl(dc, ContentControlType.RichText); dc.Sections.Add(new Section(dc, rt)); // Add the content control properties. rt.Properties.Title = "Rich text content control."; rt.Properties.Color = Color.Blue; rt.Properties.LockDeleting = true; rt.Properties.LockEditing = true; rt.Document.DefaultCharacterFormat.FontColor = Color.Orange; // Add new paragraphs with formatted text. rt.Blocks.Add(new Paragraph(dc, "Line 1")); rt.Blocks.Add(new Paragraph(dc, "Line 2")); rt.Blocks.Add(new Paragraph(dc, "Line 3")); // Add a picture and shape to the block. string pictPath = @"..\..\banner_sautinsoft.jpg"; Picture pict = new Picture(dc, InlineLayout.Inline(new Size(400, 100)), pictPath); Shape shp = new Shape(dc, Layout.Inline(new Size(4, 1, LengthUnit.Centimeter))); Paragraph im = new Paragraph(dc); rt.Blocks.Add(im); im.Inlines.Add(pict); Paragraph sh = new Paragraph(dc); rt.Blocks.Add(sh); sh.Inlines.Add(shp); rt.Blocks.Add(new Paragraph(dc, "Line 4")); // Save our document into DOCX format. string resultPath = @"result.docx"; dc.Save(resultPath, new DocxSaveOptions()); // Open the result for demonstration purposes. System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultPath) { UseShellExecute = true }); }
/// <summary> /// How to add pictures into a document. /// </summary> /// <remarks> /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/add-pictures.php /// </remarks> public static void AddPictures() { string documentPath = @"Pictures.docx"; string pictPath = @"..\..\image1.jpg"; // Let's create a simple document. DocumentCore dc = new DocumentCore(); // Add a new section. Section s = new Section(dc); dc.Sections.Add(s); // 1. Picture with InlineLayout: // Create a new paragraph with picture. Paragraph par = new Paragraph(dc); s.Blocks.Add(par); par.ParagraphFormat.Alignment = HorizontalAlignment.Left; // Add some text content. par.Content.End.Insert("Shrek and Donkey ", new CharacterFormat() { FontName = "Calibri", Size = 16.0, FontColor = Color.Black }); // Our picture has InlineLayout - it doesn't have positioning by coordinates // and located as flowing content together with text (Run and other Inline elements). Picture pict1 = new Picture(dc, InlineLayout.Inline(new Size(100, 100)), pictPath); // Add picture to the paragraph. par.Inlines.Add(pict1); // Add some text content. par.Content.End.Insert(" arrive at Farquaad's palace in Duloc, where they end up in a tournament.", new CharacterFormat() { FontName = "Calibri", Size = 16.0, FontColor = Color.Black }); // 2. Picture with FloatingLayout: // Floating layout means that the Picture (or Shape) is positioned by coordinates. Picture pict2 = new Picture(dc, pictPath); pict2.Layout = FloatingLayout.Floating( new HorizontalPosition(50, LengthUnit.Millimeter, HorizontalPositionAnchor.Page), new VerticalPosition(70, LengthUnit.Millimeter, VerticalPositionAnchor.TopMargin), new Size(LengthUnitConverter.Convert(10, LengthUnit.Centimeter, LengthUnit.Point), LengthUnitConverter.Convert(10, LengthUnit.Centimeter, LengthUnit.Point)) ); // Set the wrapping style. (pict2.Layout as FloatingLayout).WrappingStyle = WrappingStyle.Square; // Add our picture into the section. s.Content.End.Insert(pict2.Content); // Save our document into DOCX format. dc.Save(documentPath, new DocxSaveOptions()); // Open the result for demonstration purposes. System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(documentPath) { UseShellExecute = true }); }