Add arrow line to Excel worksheet in C#
With Spire.XLS for .NET, developers can easily use C# to add shapes to Excel worksheet. From version 9.8.11, Spire.XLS supports to add arrow lines to Excel worksheet. The following sample will show you how to insert arrow line, double Arrow, Elbow Arrow, Elbow Double-Arrow, Curved Arrow and Curved Double-Arrow to Excel worksheet in C#.
using Spire.Xls; using System.Drawing; namespace Add_Lines_to_Excel { class Program { static void Main(string[] args) { //Initiate a Workbook object and get the first worksheet Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; //Add a Double Arrow and fill the line with solid color var line = sheet.TypedLines.AddLine(); line.Top = 10; line.Left = 20; line.Width = 100; line.Height = 0; line.Color = Color.Blue; line.BeginArrowHeadStyle = ShapeArrowStyleType.LineArrow; line.EndArrowHeadStyle = ShapeArrowStyleType.LineArrow; //Add an Arrow and fill the line with solid color var line_1 = sheet.TypedLines.AddLine(); line_1.Top = 50; line_1.Left = 30; line_1.Width = 100; line_1.Height = 100; line_1.Color = Color.Red; line_1.BeginArrowHeadStyle = ShapeArrowStyleType.LineNoArrow; line_1.EndArrowHeadStyle = ShapeArrowStyleType.LineArrow; //Add an Elbow Arrow Connector Spire.Xls.Core.Spreadsheet.Shapes.XlsLineShape line3 = sheet.TypedLines.AddLine() as Spire.Xls.Core.Spreadsheet.Shapes.XlsLineShape; line3.LineShapeType = LineShapeType.ElbowLine; line3.Width = 30; line3.Height = 50; line3.EndArrowHeadStyle = ShapeArrowStyleType.LineArrow; line3.Top = 100; line3.Left = 50; //Add an Elbow Double-Arrow Connector Spire.Xls.Core.Spreadsheet.Shapes.XlsLineShape line2 = sheet.TypedLines.AddLine() as Spire.Xls.Core.Spreadsheet.Shapes.XlsLineShape; line2.LineShapeType = LineShapeType.ElbowLine; line2.Width = 50; line2.Height = 50; line2.EndArrowHeadStyle = ShapeArrowStyleType.LineArrow; line2.BeginArrowHeadStyle = ShapeArrowStyleType.LineArrow; line2.Left = 120; line2.Top = 100; //Add a Curved Arrow Connector line3 = sheet.TypedLines.AddLine() as Spire.Xls.Core.Spreadsheet.Shapes.XlsLineShape; line3.LineShapeType = LineShapeType.CurveLine; line3.Width = 30; line3.Height = 50; line3.EndArrowHeadStyle = ShapeArrowStyleType.LineArrowOpen; line3.Top = 100; line3.Left = 200; //Add a Curved Double-Arrow Connector line2 = sheet.TypedLines.AddLine() as Spire.Xls.Core.Spreadsheet.Shapes.XlsLineShape; line2.LineShapeType = LineShapeType.CurveLine; line2.Width = 30; line2.Height = 50; line2.EndArrowHeadStyle = ShapeArrowStyleType.LineArrowOpen; line2.BeginArrowHeadStyle = ShapeArrowStyleType.LineArrowOpen; line2.Left = 250; line2.Top = 100; //Save the file workbook.SaveToFile("AddLines.xlsx", ExcelVersion.Version2013); } } }
Insert HTML String in PowerPoint in Java
This article demonstrates how to render text with simple HTML tags to formatted text in a presentation slide by using Spire.Presentation for Java.
import com.spire.presentation.FileFormat; import com.spire.presentation.IAutoShape; import com.spire.presentation.Presentation; import com.spire.presentation.ShapeType; import com.spire.presentation.drawing.FillFormatType; import java.awt.geom.Rectangle2D; public class InsertHTML { public static void main(String[] args) throws Exception { //create a Presentation object Presentation ppt = new Presentation(); //add a shape to the first slide IAutoShape shape = ppt.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float(50, 50, 400, 100)); shape.getFill().setFillType(FillFormatType.NONE); //clear the default paragraph shape.getTextFrame().getParagraphs().clear(); //define html string String htmlString = "<ul>" + "<li style=\"color:blue\">Spire.Presentation for Java</li>" + "<li style=\"color:green\">Spire.PDF for Java</li>" + "<li style=\"color:gray\">Spire.Doc for Java</li>" + "<li style=\"color:red\">Spire.Barcode for Java</li>" + "</ul>"; //insert html string in the shape shape.getTextFrame().getParagraphs().addFromHtml(htmlString); //save to file ppt.saveToFile("output/InsertHtml.pptx", FileFormat.PPTX_2013); } }
Create Table of Contents in Word in Java
This article demonstrates how to create table of contents in a Word document using Spire.Doc for Java.
The following example shows how to create a table of contents with default appearance that includes all text formatted with built-in styles Heading 1, Heading 2, Heading 3, and page numbers right-aligned with tab leaders.
import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.*; import java.awt.*; public class TableofContents { public static void main(String[] args){ //instantiate a Document object Document doc = new Document(); //add a section Section section = doc.addSection(); //add a paragraph Paragraph para = section.addParagraph(); TextRange tr = para.appendText("Table of Contents"); //set font size and text color tr.getCharacterFormat().setFontSize(11); tr.getCharacterFormat().setTextColor(Color.blue); //set the space after the paragraph para.getFormat().setAfterSpacing(10); //add a paragraph para = section.addParagraph(); //add a table of contents with default appearance by specifying lower heading level and upper heading level. The heading level range must be from 1 to 9. para.appendTOC(1, 3); //add a new section section = doc.addSection(); //add a paragraph para = section.addParagraph(); para.appendText("Heading 1"); //apply Heading 1 style to the paragraph para.applyStyle(BuiltinStyle.Heading_1); section.addParagraph(); //add a paragraph para = section.addParagraph(); para.appendText("Heading 2"); //apply Heading 2 style to the paragraph para.applyStyle(BuiltinStyle.Heading_2); section.addParagraph(); //add a paragraph para = section.addParagraph(); para.appendText("Heading 3"); //apply Heading 3 style to the paragraph para.applyStyle(BuiltinStyle.Heading_3); section.addParagraph(); //update Table of Contents doc.updateTableOfContents(); //save the resultant document doc.saveToFile("createTableOfContents.docx", FileFormat.Docx); } }
Output:
We can also create a custom table of contents and determine what entries to appear in the table of contents by using TOC switches. The following example shows how to create a custom table of contents that includes all text formatted with built-in styles Heading 1, Heading 2 and Heading 3 but omits page numbers from heading levels 1-3.
import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.*; import java.awt.*; public class TableofContents { public static void main(String[] args){ //instantiate a Document object Document doc = new Document(); //add a section Section section = doc.addSection(); //add a paragraph Paragraph para = section.addParagraph(); TextRange tr = para.appendText("Table of Contents"); //set font size and text color tr.getCharacterFormat().setFontSize(11); tr.getCharacterFormat().setTextColor(Color.blue); //set the space after the paragraph para.getFormat().setAfterSpacing(10); //create a custom table of contents that omits page numbers from heading levels 1-3. TableOfContent toc = new TableOfContent(doc, "{\\o \"1-3\" \\n 1-3}"); para = section.addParagraph(); para.getItems().add(toc); para.appendFieldMark(FieldMarkType.Field_Separator); para.appendText("TOC"); para.appendFieldMark(FieldMarkType.Field_End); doc.setTOC(toc); //add a new section section = doc.addSection(); //add a paragraph para = section.addParagraph(); para.appendText("Heading 1"); //apply Heading 1 style to the paragraph para.applyStyle(BuiltinStyle.Heading_1); section.addParagraph(); //add a paragraph para = section.addParagraph(); para.appendText("Heading 2"); //apply Heading 2 style to the paragraph para.applyStyle(BuiltinStyle.Heading_2); section.addParagraph(); //add a paragraph para = section.addParagraph(); para.appendText("Heading 3"); //apply Heading 3 style to the paragraph para.applyStyle(BuiltinStyle.Heading_3); section.addParagraph(); //update Table of Contents doc.updateTableOfContents(); //save the resultant document doc.saveToFile("customTableOfContents.docx", FileFormat.Docx); } }
Output:
Java print a PowerPoint document
Spire.Presentation for Java supports to print the presentation slides in Java applications. This article will show you how to print PowerPoint documents from the following aspects:
- Print all presentation slides with default printer
- Select some slides from the PowerPoint document to print
Print PowerPoint document to a default printer and print all the presentation slides.
import com.spire.presentation.Presentation; import com.spire.presentation.PresentationPrintDocument; public class PrintPPT { public static void main(String[] args) throws Exception { String inputFile = "Sample.pptx"; //Create a ppt document and load file Presentation presentation = new Presentation(); presentation.loadFromFile(inputFile); //Print all the presentation slides with default printer PresentationPrintDocument document = new PresentationPrintDocument(presentation); document.print(); presentation.dispose(); } }
Select some discontinuous slides from the PowerPoint document to print
import com.spire.presentation.Presentation; import com.spire.presentation.PresentationPrintDocument; public class PrintPPT { public static void main(String[] args) throws Exception { String inputFile = "Sample.pptx"; //Create a ppt document and load file Presentation presentation = new Presentation(); presentation.loadFromFile(inputFile); PresentationPrintDocument document = new PresentationPrintDocument(presentation); //Select the slides to print document.selectSlidesForPrint("1", "2-6"); document.print(); presentation.dispose(); } }
Create Pivot Chart in Excel in C#
Starting from version 9.8.5, Spire.XLS supports creating pivot chart based on pivot table. This article is going to demonstrate how we can use Spire.XLS to implement this feature.
The input.xlsx Excel file:
Sample Code
using Spire.Xls; using Spire.Xls.Core; namespace CreatePivotChart { class Program { static void Main(string[] args) { //load the Excel file Workbook workbook = new Workbook(); workbook.LoadFromFile("Input.xlsx"); //get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //get the first pivot table in the worksheet IPivotTable pivotTable = sheet.PivotTables[0]; //create a clustered column chart based on the pivot table Chart chart = sheet.Charts.Add(ExcelChartType.ColumnClustered, pivotTable); //set chart position chart.TopRow = 19; chart.BottomRow = 38; //set chart title chart.ChartTitle = "Pivot Chart"; //save the resultant file workbook.SaveToFile("CreatPivotChart.xlsx", ExcelVersion.Version2013); } } }
Screenshot of the created pivot chart:
Add Page Numbers to Word Sections in Java
This article demonstrats how to add continuous or discontinuous page numbers to different sections in a Word document by using Spire.Doc for Java.
Add continuous page numbers to different sections
By default, when we insert page numbers to the header or footer of the first section, the other sections will link to the previous section to continue using the same header or footer. So, we only need to set up page numbering for the first section.
import com.spire.doc.Document; import com.spire.doc.FieldType; import com.spire.doc.FileFormat; import com.spire.doc.HeaderFooter; import com.spire.doc.documents.HorizontalAlignment; import com.spire.doc.documents.Paragraph; public class ContinuousNumbering { public static void main(String[] args) { //load a Word document Document document = new Document("C:\\Users\\Administrator\\Desktop\\test.docx"); //get footer object of the first section HeaderFooter footer = document.getSections().get(0).getHeadersFooters().getFooter(); //add a paragraph to footer Paragraph footerParagraph = footer.addParagraph(); //append text, automatic page field and number field to the paragraph footerParagraph.appendText("Page "); footerParagraph.appendField("page number", FieldType.Field_Page); footerParagraph.appendText(" of "); footerParagraph.appendField("number of pages", FieldType.Field_Num_Pages); footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); //save to file document.saveToFile("output/ContinuousNumbering.docx", FileFormat.Docx_2013); } }
Add discontinuous page numbers to different sections
import com.spire.doc.Document; import com.spire.doc.FieldType; import com.spire.doc.FileFormat; import com.spire.doc.HeaderFooter; import com.spire.doc.documents.HorizontalAlignment; import com.spire.doc.documents.Paragraph; public class DiscontinuousNumbering { public static void main(String[] args) { //load a Word document Document document = new Document("C:\\Users\\Administrator\\Desktop\\test.docx"); //get footer object of the first section HeaderFooter footer = document.getSections().get(0).getHeadersFooters().getFooter(); //add a paragraph to footer Paragraph footerParagraph = footer.addParagraph(); //append text and automatic page field to the paragraph footerParagraph.appendText("Page "); footerParagraph.appendField("page number", FieldType.Field_Page); footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); //determine if the document has more than one section if (document.getSections().getCount()>1) { //loop through the sections except the first one for (int i = 1; i < document.getSections().getCount(); i++) { //restart page numbering of the current section document.getSections().get(i).getPageSetup().setRestartPageNumbering(true); //set the starting number to 1 document.getSections().get(i).getPageSetup().setPageStartingNumber(1); } } //save to file document.saveToFile("output/DiscontinuousNumbering.docx", FileFormat.Docx_2013); } }
Add a round corner rectangle to presentation slide in C#
With the help of Spire.Presentation, we can add shapes to the presentation slides easily. This example shows you how to add a round corner rectangle to presentation slide and set the radius of the round corner rectangle in C#.
using Spire.Presentation; using Spire.Presentation.Drawing; using System.Drawing; namespace RoundRectangle { class Program { static void Main(string[] args) { Presentation presentation = new Presentation(); //Insert a round corner rectangle and set its radious presentation.Slides[0].Shapes.InsertRoundRectangle(0, 60, 90, 100, 200, 36); //Append a round corner rectangle and set its radious IAutoShape shape = presentation.Slides[0].Shapes.AppendRoundRectangle(260, 90, 100, 200, 80); //Set the color and fill style of shape shape.Fill.FillType = FillFormatType.Solid; shape.Fill.SolidColor.Color = Color.SeaGreen; shape.ShapeStyle.LineColor.Color = Color.White; //Rotate the shape to 90 degree shape.Rotation = 90; //Save the document to file presentation.SaveToFile("Result.pptx", FileFormat.Pptx2013); } } }
Effective screenshot of the round corner rectangle on presentation slide:
Get Bookmark Text in Java
This article demonstrates how to get the text inside a bookmark in a Word document using Spire.Doc for Java.
import com.spire.doc.Document; import com.spire.doc.documents.BookmarksNavigator; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.TextBodyPart; import com.spire.doc.fields.TextRange; import java.io.FileNotFoundException; import java.io.PrintWriter; public class GetBookmarkText { public static void main(String[] args) throws FileNotFoundException { //create a Document object Document doc = new Document(); //load a sample Word file doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx"); //get the specific bookmark BookmarksNavigator navigator = new BookmarksNavigator(doc); navigator.moveToBookmark("MyBookmark"); //get the bookmark content TextBodyPart textBodyPart = navigator.getBookmarkContent(); //declare a String variable String text = ""; //loop through body items for (Object item : textBodyPart.getBodyItems()) { //determine if an item is a paragraph if (item instanceof Paragraph) { Paragraph paragraph = (Paragraph) item; //loop through the child objects of the paragraph for (Object childObj : paragraph.getChildObjects()) { //determine if a child object is a text range if (childObj instanceof TextRange) { //get text from the text range TextRange textRange = (TextRange) childObj; text = text + textRange.getText(); } } } } //write the bookmark text to a .txt file PrintWriter printWriter = new PrintWriter("output/BookmarkText.txt"); printWriter.println(text); printWriter.close(); } }
Create a Multi-Column Word Document in Java
We can format Word document in a multi-column newsletter layout by adding columns. This article demonstrates how to add multiple columns to a Word document and specify the column width and the spacing between columns using Spire.Doc for Java.
import com.spire.doc.*; import com.spire.doc.documents.*; public class CreateMutiColumnWordDocument { public static void main(String[] args){ //create a Document object Document document = new Document(); //add a section Section section = document.addSection(); //add 3 columns to the section section.addColumn(100, 20); section.addColumn(100, 20); section.addColumn(100, 20); //add a paragraph to the section Paragraph paragraph = section.addParagraph(); //add a paragraph to the section paragraph = section.addParagraph(); String text = "Spire.Doc for Java is a professional Java Word API that enables Java applications " +"to create, convert, manipulate and print Word documents without using Microsoft Office."; //add text to the paragraph paragraph.appendText(text); //add column break to the paragraph paragraph.appendBreak(BreakType.Column_Break); //add a paragraph to the section paragraph = section.addParagraph(); //add text to the paragraph paragraph.appendText(text); //add column break to the paragraph paragraph.appendBreak(BreakType.Column_Break); //add a paragraph to the section paragraph = section.addParagraph(); //add text to the paragraph paragraph.appendText(text); //add line between columns section.getPageSetup().setColumnsLineBetween(true); //save the resultant document document.saveToFile("Muti-Column Document.docx", FileFormat.Docx_2013); } }
Output:
Java make a booklet from a PDF document
When we print a huge PDF document, print as a booklet is a great way to save the paper and make the pages tidy. This article we will introduce how to create a booklet from a PDF document in Java applications.
import com.spire.pdf.*; public class PDFBooklet{ public static void main(String[] args) throws Exception { String inputPath = "Sample.pdf"; PdfDocument doc = new PdfDocument(); doc.loadFromFile(inputPath); PdfPageBase page = doc.getPages().get(0); float width = (float) page.getSize().getWidth()*2; float height = (float) page.getSize().getHeight(); doc.createBooklet(inputPath, width, height,true); doc.saveToFile("Output/Booklet.pdf"); } }
Effective screenshot after creating PDF booklet: