.NET (1270)
Children categories
How to save Chart and table on Presentation Slide as image in C#
2015-06-02 07:40:40 Written by support iceblueSpire.Presentation enables developers to export the whole presentation slides with table, chart and shape to image. And it also supports to export the single element in the presentation slides, such as chart, table and shape into image. This article will show you how to save chart and table on Presentation Slide as image in C# and please check the steps as below:
Step 1: Create a presentation document and load from file.
Presentation ppt = new Presentation(); ppt.LoadFromFile("sample.pptx");
Step 2: Traverse every shape in the slide. Call ShapeList.SaveAsImage(int shapeIndex) to save table and chart as image.
//Save table as image in .Png format Image image = ppt.Slides[0].Shapes.SaveAsImage(4); image.Save("table.png", System.Drawing.Imaging.ImageFormat.Png); //Save chart as image in .Jpeg format image = ppt.Slides[1].Shapes.SaveAsImage(3); image.Save("chart.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
You can use Shapes.SaveAsEMF method to save the chart and table to image in EMF format.
Effective screenshot:
Save Presentation table as image:
Save Presentation chart as image:
Full codes:
using Spire.Presentation; using System.Drawing; namespace SaveTableChartasImage { class Program { static void Main(string[] args) { Presentation ppt = new Presentation(); ppt.LoadFromFile("sample.pptx"); Image image = ppt.Slides[0].Shapes.SaveAsImage(4); image.Save("table.png", System.Drawing.Imaging.ImageFormat.Png); image = ppt.Slides[1].Shapes.SaveAsImage(3); image.Save("chart.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); //ppt.Slides[1].Shapes.SaveAsEMF(3, "chart.emf"); } } }
C#/VB.NET: Change Security Permissions of PDF Documents
2022-09-29 07:38:00 Written by support iceblueWhen you protect your PDF documents with passwords you can optionally specify a set of permissions. The permissions determine how users can interact with the file. For example, you can apply permissions to your document to prohibit the user from printing or using cut and paste operations. This article demonstrates how to change security permissions of PDF documents using Spire.PDF for .NET in C# and VB.NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Change Security Permissions of a PDF Document
The following are the steps to apply security permissions to a PDF document using Spire.PDF for .NET.
- Create a PdfDocument object.
- Load a sample PDF file using PdfDocument.LoadFileFile() method.
- Specify open password and permission password. The open password can be set to empty so that the generated document will not require a password to open.
- Encrypt the document with the open password and permission password, and set the security permissions using PdfDocument.Security.Encypt() method. This method takes PdfPermissionsFlags enumeration as a parameter, which defines user access permissions for an encrypted document.
- Save the document to another PDF file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf; using Spire.Pdf.Security; namespace ChangeSecurityPermission { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument doc = new PdfDocument(); //Load a sample PDF file doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf"); //Specify open password string openPsd = string.Empty; //Specify permission password string permissionPsd = "e-iceblue"; //Encrypt the document with open password and permission password, and set the permissions and encryption key size doc.Security.Encrypt(openPsd, permissionPsd, PdfPermissionsFlags.FullQualityPrint, PdfEncryptionKeySize.Key128Bit); //Save the document to another PDF file doc.SaveToFile("SecurityPermissions.pdf"); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
How to split text into two columns and add line between them
2015-05-28 08:51:50 Written by support iceblueColumns are widely used to set page layout, for which could split text into two or more columns so that the text could flow from one column to the next on the same page. Using Spire.Doc, we could achieve this feature and add a line between columns at the same time. This article is going to introduce how to split text into two columns and add line between them.
Note: please download the latest version of Spire.Doc and add Spire.Doc .dll as reference of Visual Studio.
Step 1: Create a new document and load from file
Document document = new Document(); document.LoadFromFile("S.docx");
Step 2: Add a column to section one, set the width of columns and the spacing between columns. Here we set width as 100f and spacing as 20f.
document.Sections[0].AddColumn(100f, 20f);
Step 3: Add a line between the two columns
document.Sections[0].PageSetup.ColumnsLineBetween = true;
Step 4: Save the document and launch to see effects
document.SaveToFile("result.docx",FileFormat.docx2013); System.Diagnostics.Process.Start("result.docx");
Before adding the columns:
Effects:
Full Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Spire.Doc; namespace Column { class Program { static void Main(string[] args) { Document document = new Document(); document.LoadFromFile("S.docx"); document.Sections[0].AddColumn(100f, 20f); document.Sections[0].PageSetup.ColumnsLineBetween = true; document.SaveToFile("result.docx",FileFormat.docx2013); System.Diagnostics.Process.Start("result.docx"); } } }
Text Box has been widely used in word documents to present additional information to readers. As a powerful and easy to use .NET word component, Spire.Doc has powerful function to work with text box. We have already showed you how to insert text box to word document and extract Text from Text Boxes in Word document. In this article let's see how to remove text box from word document in C#.
Spire.Doc supports to remove the particular text box or clear all the text boxes from the word documents. We will show you the details as below. Firstly, check the original word document contains three text boxes on it.
Here comes to the steps of how to remove the text box from word document by using the class of TextBoxes.
Step 1: Create a new document and load from file.
Document Doc = new Document(); Doc.LoadFromFile("Sample.docx");
Step 2: Remove the first text box by using the class of TextBoxes.
Doc.TextBoxes.RemoveAt(0);
Step 3: Save and launch the file.
Doc.SaveToFile("result.docx", FileFormat.Docx); System.Diagnostics.Process.Start("result.docx");
Effective screenshot:
If you want to clear all the text boxes at one time, the following code snippet will fulfill it.
Screenshot after clear all the text boxes:
Doc.TextBoxes.Clear();
Full codes:
namespace Removetextbox { class Program { static void Main(string[] args) { Document Doc = new Document(); Doc.LoadFromFile("Sample.docx"); Doc.TextBoxes.RemoveAt(0); //Doc.TextBoxes.Clear(); Doc.SaveToFile("result.docx", FileFormat.Docx); System.Diagnostics.Process.Start("result.docx"); } } }
Grid also offers more flexible resizing behavior than Table and lighter weight then a Table. It derives from the Panel element which is best used inside of forms. This article is mainly talk about how to merge cells in grid via Spire.PDF.
Prerequisite:
- Download Spire.PDF for .NET (or Spire.Office for .NET) and install it on your system.
- Add Spire.PDF.dll as reference in the downloaded Bin folder thought the below path: "..\Spire.PDF\Bin\NET4.0\ Spire.PDF.dll".
- Check the codes as below in C#:
Here are the detail steps:
Step 1: Create a new PDF document and add a new page.
PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add();
Step 2: Add a new grid with 5 columns and 2 rows, and set height and width.
PdfGrid grid = new PdfGrid(); grid.Columns.Add(5); float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1); for (int j = 0; j < grid.Columns.Count;j++) { grid.Columns[j].Width = width * 0.20f; } PdfGridRow row0 = grid.Rows.Add(); PdfGridRow row1 = grid.Rows.Add(); float height = 20.0f; for (int i = 0; i < grid.Rows.Count; i++) { grid.Rows[i].Height = height; }
Step 3: Draw the current grid.
grid.Draw(page, new PointF(0, 50));
Step 4: Set the font of the grid and fill some content in the cells. Use RowSpan and ColumnSpan to merge certain cells vertically and horizontally.
row0.Style.Font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold), true); row1.Style.Font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Italic), true); row0.Cells[0].Value = "Corporation"; row0.Cells[0].RowSpan = 2; row0.Cells[1].Value = "B&K Undersea Photo"; row0.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row0.Cells[1].ColumnSpan = 3; row0.Cells[4].Value = "World"; row0.Cells[4].Style.Font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold | FontStyle.Italic), true); row0.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row0.Cells[4].Style.BackgroundBrush = PdfBrushes.LightGreen; row1.Cells[1].Value = "Diving International Unlimited"; row1.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row1.Cells[1].ColumnSpan = 4;
Step 5: Draw the new grid and save the file, the review it.
grid.Draw(page, new PointF(0, 100)); doc.SaveToFile("result.pdf"); System.Diagnostics.Process.Start("result.pdf");
Following is the result screenshot:
Full Code:
using Spire.Pdf; using Spire.Pdf.Graphics; using Spire.Pdf.Grid; using System.Drawing; namespace MergeCells { class Program { static void Main(string[] args) { PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add(); PdfGrid grid = new PdfGrid(); grid.Columns.Add(5); float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1); for (int j = 0; j < grid.Columns.Count; j++) { grid.Columns[j].Width = width * 0.20f; } PdfGridRow row0 = grid.Rows.Add(); PdfGridRow row1 = grid.Rows.Add(); float height = 20.0f; for (int i = 0; i < grid.Rows.Count; i++) { grid.Rows[i].Height = height; } grid.Draw(page, new PointF(0, 50)); row0.Style.Font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold), true); row1.Style.Font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Italic), true); row0.Cells[0].Value = "Corporation"; row0.Cells[0].RowSpan = 2; row0.Cells[1].Value = "B&K Undersea Photo"; row0.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row0.Cells[1].ColumnSpan = 3; row0.Cells[4].Value = "World"; row0.Cells[4].Style.Font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold | FontStyle.Italic), true); row0.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row0.Cells[4].Style.BackgroundBrush = PdfBrushes.LightGreen; row1.Cells[1].Value = "Diving International Unlimited"; row1.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row1.Cells[1].ColumnSpan = 4; grid.Draw(page, new PointF(0, 100)); doc.SaveToFile("result.pdf"); System.Diagnostics.Process.Start("result.pdf"); } } }
Set position of table in Word Document as outside via Spire.Doc
2015-05-22 08:57:03 Written by support iceblueTable in word document can make your data more logical and uncluttered, this article is talk about set absolute position of table in word document via Spire.Doc. Here try to realize placing a table on the right of an image on header.
Here are the steps:
Step 1: Create a new word document and add new section.
Document doc = new Document(); Section sec = doc.AddSection();
Step 2: Create header on Section[0].
HeaderFooter header = doc.Sections[0].HeadersFooters.Header;
Step 3: Add new paragraph on header and set HorizontalAlignment of the paragraph as left.
Paragraph paragraph = header.AddParagraph(); paragraph.Format.HorizontalAlignment = HorizontalAlignment.Left;
Step 4: Load an image for the paragraph.
DocPicture headerimage = paragraph.AppendPicture(Image.FromFile(@"1.png"));
Step 5: Add a table of 4 rows and 2 columns.
Table table = header.AddTable(); table.ResetCells(4, 2);
Step 6: Set the position of the table to the right of the image. Set WrapTextAround is true, HorizPositionAbs is outside, VertRelationTo is margin, and VertPosition is 43 to fit the height of the image.
table.TableFormat.WrapTextAround = true; table.TableFormat.Positioning.HorizPositionAbs = HorizontalPosition.Outside; table.TableFormat.Positioning.VertRelationTo = VerticalRelation.Margin; table.TableFormat.Positioning.VertPosition = 43;
Step 7: Then add contents for the table, first column alignment set as left ,second column alignment set as right.
String[][] data = { new string[] {"Spire.Doc.left","Spire XLS.right"}, new string[] {"Spire.Presentatio.left","Spire.PDF.right"}, new string[] {"Spire.DataExport.left","Spire.PDFViewe.right"}, new string []{"Spire.DocViewer.left","Spire.BarCode.right"} }; for (int r = 0; r < 4; r++) { TableRow dataRow = table.Rows[r]; for (int c = 0; c < 2; c++) { if (c == 0) { Paragraph par = dataRow.Cells[c].AddParagraph(); par.AppendText(data[r][c]); par.Format.HorizontalAlignment = HorizontalAlignment.Left; dataRow.Cells[c].Width = 180; } else { Paragraph par = dataRow.Cells[c].AddParagraph(); par.AppendText(data[r][c]); par.Format.HorizontalAlignment = HorizontalAlignment.Right; dataRow.Cells[c].Width = 180; } } }
Step 8: Save the file and review it.
doc.SaveToFile("result.docx", FileFormat.Docx); System.Diagnostics.Process.Start("result.docx");
Here is the screenshot:
Full Code:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System; using System.Drawing; namespace SetPosition { class Program { static void Main(string[] args) { Document doc = new Document(); Section sec = doc.AddSection(); HeaderFooter header = doc.Sections[0].HeadersFooters.Header; Paragraph paragraph = header.AddParagraph(); paragraph.Format.HorizontalAlignment = HorizontalAlignment.Left; DocPicture headerimage = paragraph.AppendPicture(Image.FromFile(@"1.png")); Table table = header.AddTable(); table.ResetCells(4, 2); table.TableFormat.WrapTextAround = true; table.TableFormat.Positioning.HorizPositionAbs = HorizontalPosition.Outside; table.TableFormat.Positioning.VertRelationTo = VerticalRelation.Margin; table.TableFormat.Positioning.VertPosition = 43; String[][] data = { new string[] {"Spire.Doc.left","Spire XLS.right"}, new string[] {"Spire.Presentatio.left","Spire.PDF.right"}, new string[] {"Spire.DataExport.left","Spire.PDFViewe.right"}, new string []{"Spire.DocViewer.left","Spire.BarCode.right"} }; for (int r = 0; r < 4; r++) { TableRow dataRow = table.Rows[r]; for (int c = 0; c < 2; c++) { if (c == 0) { Paragraph par = dataRow.Cells[c].AddParagraph(); par.AppendText(data[r][c]); par.Format.HorizontalAlignment = HorizontalAlignment.Left; dataRow.Cells[c].Width = 180; } else { Paragraph par = dataRow.Cells[c].AddParagraph(); par.AppendText(data[r][c]); par.Format.HorizontalAlignment = HorizontalAlignment.Right; dataRow.Cells[c].Width = 180; } } } doc.SaveToFile("result.docx",Spire.Doc.FileFormat.Docx); System.Diagnostics.Process.Start("result.docx"); } } }
How to Embed Excel Object into PowerPoint Slide in C#, VB.NET
2015-05-21 08:38:42 Written by support iceblueTo display Excel data in a PowerPoint presentation, we can copy and paste the data to a slide as a PowerPoint table, a worksheet object, a picture, or plain text. This article introduces how to insert an Excel sheet in PowerPoint as an OLE object in C# and VB.NET.
In this solution, you'll need to use Spire.XLS to load the Excel file and get the data, and then create OEL object using Spire.Presentation. So, please download Spire.Office and reference the related DLLs in your project.
Workbook book = new Workbook(); book.LoadFromFile(@"data.xlsx");
Step 2: Select the cell range from the first worksheet and save it as image.
Image image = book.Worksheets[0].ToImage(1, 1, 5, 4);
Step 3: Initialize a new instance of Presentation class, add the image to presentation.
Presentation ppt = new Presentation(); IImageData oleImage = ppt.Images.Append(image);
Step 4: Insert an OLE object to presentation based on the Excel data.
Rectangle rec = new Rectangle(60,60,image.Width,image.Height); using (MemoryStream ms = new MemoryStream()) { book.SaveToStream(ms); ms.Position = 0; Spire.Presentation.IOleObject oleObject = ppt.Slides[0].Shapes.AppendOleObject("excel", ms.ToArray(), rec); oleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage; oleObject.ProgId = "Excel.Sheet.12"; }
Step 5: Save the PowerPoint file with the specified format.
ppt.SaveToFile("InsertOle.pptx", Spire.Presentation.FileFormat.Pptx2007);
Output:
Entire Code:
using Spire.Presentation; using Spire.Presentation.Drawing; using Spire.Xls; using System.Drawing; using System.IO; namespace ExceltoPPT { class Program { static void Main(string[] args) { Workbook book = new Workbook(); book.LoadFromFile(@"data.xlsx"); Image image = book.Worksheets[0].ToImage(1, 1, 5, 4); Presentation ppt = new Presentation(); IImageData oleImage = ppt.Images.Append(image); Rectangle rec = new Rectangle(60, 60, image.Width, image.Height); using (MemoryStream ms = new MemoryStream()) { book.SaveToStream(ms); ms.Position = 0; Spire.Presentation.IOleObject oleObject = ppt.Slides[0].Shapes.AppendOleObject("excel", ms.ToArray(), rec); oleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage; oleObject.ProgId = "Excel.Sheet.12"; } ppt.SaveToFile("InsertOle.pptx", Spire.Presentation.FileFormat.Pptx2007); } } }
Imports Spire.Presentation Imports Spire.Presentation.Drawing Imports Spire.Xls Imports System.Drawing Imports System.IO Namespace ExceltoPPT Class Program Private Shared Sub Main(args As String()) Dim book As New Workbook() book.LoadFromFile("data.xlsx") Dim image As Image = book.Worksheets(0).ToImage(1, 1, 5, 4) Dim ppt As New Presentation() Dim oleImage As IImageData = ppt.Images.Append(image) Dim rec As New Rectangle(60, 60, image.Width, image.Height) Using ms As New MemoryStream() book.SaveToStream(ms) ms.Position = 0 Dim oleObject As Spire.Presentation.IOleObject = ppt.Slides(0).Shapes.AppendOleObject("excel", ms.ToArray(), rec) oleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage oleObject.ProgId = "Excel.Sheet.12" End Using ppt.SaveToFile("InsertOle.pptx", Spire.Presentation.FileFormat.Pptx2007) End Sub End Class End Namespace
A trendline is used to show the trend of data series and predict future tendency. Totally, there are six different types: linear trendline, logarithmic trendline, polynomial trendline, power trendline, exponential trendline, and moving average trendline. We may choose different trendline based on the data type we have for different trendline has different advantages to show trend.
For example, a linear trendline works well to show the increase or decrease of data at a steady rate while a logarithmic trendline is often used to show the rate of change in the data increases or decreases quickly and then levels out.
Spire.XLS supports to add all of those six trendlines in charts. This article is going to show how to add trendlines in visual studio.
Before adding trendlines
After adding trendlines
Step 1: Load the workbook and create a sheet
Workbook workbook = new Workbook(); workbook.LoadFromFile("S2.xlsx"); Worksheet sheet = workbook.Worksheets[0];
Step 2: Select targeted chart, add trendline and set its type
//select chart and set logarithmic trendline Chart chart = sheet.Charts[0]; chart.Series[0].TrendLines.Add(TrendLineType.Logarithmic); //select chart and set moving_average trendline Chart chart1 = sheet.Charts[1]; chart1.Series[0].TrendLines.Add(TrendLineType.Moving_Average); //select chart and set linear trendline Chart chart2 = sheet.Charts[2]; chart2.Series[0].TrendLines.Add(TrendLineType.Linear); //select chart and set exponential trendline Chart chart3 = sheet.Charts[3]; chart3.Series[0].TrendLines.Add(TrendLineType.Exponential);
Step 3: Save and launch the document
workbook.SaveToFile("S3.xlsx", ExcelVersion.Version2010); System.Diagnostics.Process.Start("S3.xlsx");
Full code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Spire.Xls; namespace TradeLine { class Program { static void Main(string[] args) { Workbook workbook = new Workbook(); workbook.LoadFromFile("S2.xlsx"); Worksheet sheet = workbook.Worksheets[0]; Chart chart = sheet.Charts[0]; chart.Series[0].TrendLines.Add(TrendLineType.Logarithmic); Chart chart1 = sheet.Charts[1]; chart1.Series[0].TrendLines.Add(TrendLineType.Moving_Average); Chart chart2 = sheet.Charts[2]; chart2.Series[0].TrendLines.Add(TrendLineType.Linear); Chart chart3 = sheet.Charts[3]; chart3.Series[0].TrendLines.Add(TrendLineType.Exponential); workbook.SaveToFile("S3.xlsx", ExcelVersion.Version2010); System.Diagnostics.Process.Start("S3.xlsx"); } } }
Please note that we cannot add a trendline to data series in a stacked, 3-D, radar, pie, surface, or doughnut chart because those types of chart don't support trendline.
How to add hyperlink to the existing text on presentation slides in C#
2015-05-18 08:15:52 Written by support iceblueWith the help of Spire.Presentation, developers can insert new hyperlink into PowerPoint Presentation and edit the existing hyperlinks in C# and VB.NET. This article will show you how to add hyperlink to the existing text on presentation slides.
Firstly check the screenshot of the original presentation slides.
Here comes to the steps of how to adding hyperlink to an existing text in Presentation slide:
Step 1: Create a new presentation document and load from file.
Presentation ppt = new Presentation(); ppt.LoadFromFile("Sample.pptx", FileFormat.Pptx2010);
Step 2: Find the text we want to add link to it.
IAutoShape shape = ppt.Slides[0].Shapes[0] as IAutoShape; TextParagraph tp = shape.TextFrame.TextRange.Paragraph; string temp = tp.Text;
Step 3: Adding the hyperlink to the existing text.
//split the original text string textToLink = "Spire.Presentation"; string[] strSplit = temp.Split(new string[] { "Spire.Presentation" }, StringSplitOptions.None); //Clear all text tp.TextRanges.Clear(); //Add new text TextRange tr = new TextRange(strSplit[0]); tp.TextRanges.Append(tr); //Add the hyperlink tr = new TextRange(textToLink); tr.ClickAction.Address = "http://www.e-iceblue.com/Introduce/presentation-for-net-introduce.html"; tp.TextRanges.Append(tr);
Step 4: Save the document to file.
ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);
Effective screenshot after adding the hyperlink to the existing text:
Full codes:
using Spire.Presentation; using System; namespace AddHyperlink { class Program { static void Main(string[] args) { Presentation ppt = new Presentation(); ppt.LoadFromFile("Sample.pptx", FileFormat.Pptx2010); IAutoShape shape = ppt.Slides[0].Shapes[0] as IAutoShape; TextParagraph tp = shape.TextFrame.TextRange.Paragraph; string temp = tp.Text; //split the original text string textToLink = "Spire.Presentation"; string[] strSplit = temp.Split(new string[] { "Spire.Presentation" }, StringSplitOptions.None); //Clear all text tp.TextRanges.Clear(); //Add new text TextRange tr = new TextRange(strSplit[0]); tp.TextRanges.Append(tr); //Add the hyperlink tr = new TextRange(textToLink); tr.ClickAction.Address = "http://www.e-iceblue.com/Introduce/presentation-for-net-introduce.html"; tp.TextRanges.Append(tr); ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010); } } }
Apply a border around characters or sentence in word document
2015-05-15 02:20:38 Written by support iceblueTo emphasize and beautify a set of characters or sentence, applying a border around the characters or sentence is a good option. Spire.Doc enables developers to achieve this feature in C#. And there are plenty of built-in border styles available, such as: Wave, Hairline, DotDash, DashSmallGap, DashLargeGap, DoubleWave, DashDotStroker, Emboss3D, Engrave3D, TwistedLines1 and so on. The following codes show how to achieve character border with some border styles mentioned above:
Note: Before start, please make sure that Visual Studio and Spire.Doc have been installed properly. We will use Spire.Doc .dll as reference.
Step 1: Load word document
Document doc = new Document(); Section section = doc.AddSection();
Step 2: Add the characters needed to apply a border and set the border style
//DashSmallGap Border Paragraph para = section.AddParagraph(); para.Format.HorizontalAlignment = HorizontalAlignment.Left; TextRange tr = para.AppendText("Spire.Doc for .Net"); tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.DashSmallGap; tr.CharacterFormat.Border.Color = Color.Green; tr.CharacterFormat.FontSize = 24; tr.CharacterFormat.TextColor = Color.DarkKhaki; para.AppendBreak(BreakType.LineBreak); //Wave Border para = section.AddParagraph(); para.Format.HorizontalAlignment = HorizontalAlignment.Left; tr = para.AppendText("Spire.PDF for .Net"); tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.Wave; tr.CharacterFormat.Border.Color = Color.Aqua; tr.CharacterFormat.FontSize = 24; tr.CharacterFormat.TextColor = Color.BurlyWood; para.AppendBreak(BreakType.LineBreak); //Emboss3D Border para = section.AddParagraph(); para.Format.HorizontalAlignment = HorizontalAlignment.Left; tr = para.AppendText("Spire.XLS for .Net"); tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.Emboss3D; tr.CharacterFormat.FontSize = 24; para.AppendBreak(BreakType.LineBreak); //DashDotStroker Border para = section.AddParagraph(); para.Format.HorizontalAlignment = HorizontalAlignment.Left; tr = para.AppendText("Spire.Office for .Net"); tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.DashDotStroker; tr.CharacterFormat.Border.Color = Color.Olive; tr.CharacterFormat.FontSize = 24; tr.CharacterFormat.TextColor = Color.Olive; para.AppendBreak(BreakType.LineBreak); //DoubleWave Border para = section.AddParagraph(); para.Format.HorizontalAlignment = HorizontalAlignment.Left; tr = para.AppendText("Spire.Presentation for .Net"); tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.DoubleWave; tr.CharacterFormat.Border.Color = Color.Blue; tr.CharacterFormat.FontSize = 24; tr.CharacterFormat.TextColor = Color.Blue; para.AppendBreak(BreakType.LineBreak);
Step 3: Save and launch word document
doc.SaveToFile("S1.docx", FileFormat.Docx); System.Diagnostics.Process.Start("S1.docx");
Effect of screenshot:
Full Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Document doc = new Document(); Section section = doc.AddSection(); //DashSmallGap Border Paragraph para = section.AddParagraph(); para.Format.HorizontalAlignment = HorizontalAlignment.Left; TextRange tr = para.AppendText("Spire.Doc for .Net"); tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.DashSmallGap; tr.CharacterFormat.Border.Color = Color.Green; tr.CharacterFormat.FontSize = 24; tr.CharacterFormat.TextColor = Color.DarkKhaki; para.AppendBreak(BreakType.LineBreak); //Wave Border para = section.AddParagraph(); para.Format.HorizontalAlignment = HorizontalAlignment.Left; tr = para.AppendText("Spire.PDF for .Net"); tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.Wave; tr.CharacterFormat.Border.Color = Color.Aqua; tr.CharacterFormat.FontSize = 24; tr.CharacterFormat.TextColor = Color.BurlyWood; para.AppendBreak(BreakType.LineBreak); //Emboss3D Border para = section.AddParagraph(); para.Format.HorizontalAlignment = HorizontalAlignment.Left; tr = para.AppendText("Spire.XLS for .Net"); tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.Emboss3D; tr.CharacterFormat.FontSize = 24; para.AppendBreak(BreakType.LineBreak); //DashDotStroker Border para = section.AddParagraph(); para.Format.HorizontalAlignment = HorizontalAlignment.Left; tr = para.AppendText("Spire.Office for .Net"); tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.DashDotStroker; tr.CharacterFormat.Border.Color = Color.Olive; tr.CharacterFormat.FontSize = 24; tr.CharacterFormat.TextColor = Color.Olive; para.AppendBreak(BreakType.LineBreak); //DoubleWave Border para = section.AddParagraph(); para.Format.HorizontalAlignment = HorizontalAlignment.Left; tr = para.AppendText("Spire.Presentation for .Net"); tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.DoubleWave; tr.CharacterFormat.Border.Color = Color.Blue; tr.CharacterFormat.FontSize = 24; tr.CharacterFormat.TextColor = Color.Blue; para.AppendBreak(BreakType.LineBreak); doc.SaveToFile("S1.docx", FileFormat.Docx); System.Diagnostics.Process.Start("S1.docx"); } } }