With Spire.Doc for .NET, we can easily insert new text to word document at exact position, it also supports to insert new text after the certain text strings at many places. This article will show you how to insert new text strings after the searched text string in word document.

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace Word
{
    class Program
    {
        static void Main(string[] args)
        {
            //load the sample document
            Document doc = new Document();
            doc.LoadFromFile("Sample.docx", FileFormat.Docx2010);

            //find all the text string “New Zealand” from the sample document
            TextSelection[] selections = doc.FindAllString("New Zealand", true, true);
            int index = 0;

            //defines text range
            TextRange range = new TextRange(doc);


            //insert new text string (NY) after the searched text string
            foreach (TextSelection selection in selections)
            {
                range = selection.GetAsOneRange();
                TextRange newrange = new TextRange(doc);
                newrange.Text = ("(NY)");
                index = range.OwnerParagraph.ChildObjects.IndexOf(range);
                range.OwnerParagraph.ChildObjects.Insert(index + 1, newrange);

            }

            //find and highlight the newly added text string NY
            TextSelection[] text2 = doc.FindAllString("NY", false, true);
            foreach (TextSelection seletion in text2)
            {
                seletion.GetAsOneRange().CharacterFormat.HighlightColor = Color.Yellow;
            }

            //save the document 
            doc.SaveToFile("Result.docx", FileFormat.Docx2010);
        }
    }
}

Effective screenshot after adding the text strings to the searched text:

C# add new text strings after the searched text string in word document

A combination chart is a chart that combines at least two chart types in a single chart. This article introduces how to combine clustered column and line chart in PowerPoint using Spire.Presentation for Java.

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
import com.spire.presentation.charts.ChartType;
import com.spire.presentation.charts.IChart;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

public class CombinationChart {

    public static void main(String[] args) throws Exception {

        //create a PowerPoint document
        Presentation presentation = new Presentation();

        //insert a column clustered chart
        Rectangle2D.Double rect = new   Rectangle2D.Double(50, 100, 550, 300);
        IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.COLUMN_CLUSTERED, rect);

        //set chart title
        chart.getChartTitle().getTextProperties().setText("Sales vs. Unit Price");
        chart.getChartTitle().getTextProperties().isCentered(true);
        chart.getChartTitle().setHeight(30);
        chart.hasTitle(true);

        //write data to chart as chart data
        chart.getChartData().get(0,0).setText("Month");
        chart.getChartData().get(0,1).setText("Unit Price");
        chart.getChartData().get(0,2).setText("Sales");
        chart.getChartData().get(1,0).setText("January");
        chart.getChartData().get(1,1).setNumberValue(120);
        chart.getChartData().get(1,2).setNumberValue(5600);
        chart.getChartData().get(2,0).setText("February");
        chart.getChartData().get(2,1).setNumberValue(100);
        chart.getChartData().get(2,2).setNumberValue(7300);
        chart.getChartData().get(3,0).setText("March");
        chart.getChartData().get(3,1).setNumberValue(80);
        chart.getChartData().get(3,2).setNumberValue(10200);
        chart.getChartData().get(4,0).setText("April");
        chart.getChartData().get(4,1).setNumberValue(120);
        chart.getChartData().get(4,2).setNumberValue(5900);
        chart.getChartData().get(5,0).setText("May");
        chart.getChartData().get(5,1).setNumberValue(90);
        chart.getChartData().get(5,2).setNumberValue(9500);
        chart.getChartData().get(6,0).setText("June");
        chart.getChartData().get(6,1).setNumberValue(110);
        chart.getChartData().get(6,2).setNumberValue(7200);

        //set series labels
        chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "C1"));

        //set categories labels
        chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A7"));

        //assign data to series values
        chart.getSeries().get(0).setValues(chart.getChartData().get("B2", "B7"));
        chart.getSeries().get(1).setValues(chart.getChartData().get("C2", "C7"));

        //change the chart type of series 2 to line with markers
        chart.getSeries().get(1).setType(ChartType.LINE_MARKERS);

        //plot data of series 2 on the secondary axis
        chart.getSeries().get(1).setUseSecondAxis(true);

        //hide grid links of secondary axis
        chart.getSecondaryValueAxis().getMajorGridTextLines().setFillType(FillFormatType.NONE);

        //set overlap
        chart.setOverLap(-50);

        //set gap width
        chart.setGapDepth(200);

        //save the document
        presentation.saveToFile("CombinationChart.pptx", FileFormat.PPTX_2010);
    }
}

Create a Combination Chart in PowerPoint in Java

This article demonstrates how to replace an existing image with a new image in a PDF file using Spire.PDF for Java.

Below is the screenshot of the input PDF file we used for demonstration:

Replace image with new image in PDF in Java

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfImage;

import java.io.IOException;

public class ReplaceImage {
    public static void main(String[] args) throws IOException {
        //Load the PDF file
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Input.pdf");

        //Get the first page
        PdfPageBase page = pdf.getPages().get(0);

        //Load an image
        PdfImage image = PdfImage.fromFile("Image.png");

        //Replace the first image in the first page with the loaded image
        page.replaceImage(0, image);

        //Save the file
        pdf.saveToFile("ReplaceImage.pdf");
    }
}

Output:

Replace image with new image in PDF in Java

Wednesday, 27 March 2019 07:03

Convert PowerPoint to XPS in Java

The article will guide you how to convert PowerPoint (.pptx and .ppt) documents to XPS by using Spire.Presentation for Java.

Here is the screenshot of the sample PowerPoint file.

Convert PowerPoint to XPS in Java

import com.spire.presentation.*;

public class PPTtoXPS {
    public static void main(String[] args) throws Exception{

    //create a presentation instance 
    Presentation ppt = new Presentation();

    //load the sample PowerPoint file
    ppt.loadFromFile("Sample.pptx");

    //save to XPS file
    ppt.saveToFile("toXPS.xps", FileFormat.XPS);
ppt.dispose();

    }
}

Effective screenshot of the XPS file:

Convert PowerPoint to XPS in Java

Tuesday, 26 March 2019 07:12

Get PDF Page Labels in C#

Page labels are used to identify each page visually on the screen or in print. This article demonstrates how to get the PDF page labels using Spire.PDF.

Below is the screenshot of the sample PDF document:

Get PDF Page Labels in C#

Detail steps:

Step 1: Create a PdfDocument instance and load the sample.pdf file.

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("sample.pdf");

Step 2: Get the labels of the pages in the PDF file.

StringBuilder sb = new StringBuilder();
for (int i = 0; i < pdf.Pages.Count; i++)
{
    sb.AppendLine(pdf.Pages[i].PageLabel);
}

Step 3: Save to a .txt file.

File.WriteAllText("PageLabels.txt", sb.ToString());

Output:

Get PDF Page Labels in C#

Full code:

using System.IO;
using System.Text;
using Spire.Pdf;

namespace Get_PDF_Page_Labels
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();
            //Load the PDF file
            pdf.LoadFromFile("sample.pdf");

            //Create a StringBuilder instance
            StringBuilder sb = new StringBuilder();
            //Get the lables of the pages in the PDF file
            for (int i = 0; i < pdf.Pages.Count; i++)
            {
                sb.AppendLine(pdf.Pages[i].PageLabel);
            }

            //Save to a .txt file
            File.WriteAllText("PageLabels.txt", sb.ToString());
        }
    }
}
Tuesday, 26 March 2019 04:04

Align Text in PowerPoint Table in Java

This article demonstrates how to align text within a table cell in PowerPoint using Spire.Presenatation for Java.

Code Snippets

import com.spire.presentation.*;

public class AlignTextInTableCell {

    public static void main(String[] args) throws Exception {

        //create a PowerPoint file
        Presentation presentation = new Presentation();

        //add a table
        Double[] widths = new Double[]{100d, 200d, 100d, 100d};
        Double[] heights = new Double[]{30d, 70d, 70d};
        ITable table = presentation.getSlides().get(0).getShapes().appendTable(30,80, widths, heights);
        table.setStylePreset(TableStylePreset.NONE);

        //set the horizontal alignment for the cells in the first row
        table.get(0, 0).getTextFrame().setText("Left");
        table.get(0, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.LEFT);
        table.get(1, 0).getTextFrame().setText("Horizontal Center");
        table.get(1, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);
        table.get(2, 0).getTextFrame().setText("Right");
        table.get(2, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.RIGHT);
        table.get(3, 0).getTextFrame().setText("Justify");
        table.get(3, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.JUSTIFY);

        //Set the vertical alignment for the cells in the second row
        table.get(0, 1).getTextFrame().setText("Top");
        table.get(0, 1).setTextAnchorType(TextAnchorType.TOP);
        table.get(1, 1).getTextFrame().setText("Vertical Center");
        table.get(1, 1).setTextAnchorType(TextAnchorType.CENTER);
        table.get(2, 1).getTextFrame().setText("Bottom");
        table.get(2, 1).setTextAnchorType(TextAnchorType.BOTTOM);

        //set the both horizontal and vertical alignment for the cells in the third row
        table.get(0, 2).getTextFrame().setText("Top Left");
        table.get(0, 2).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.LEFT);
        table.get(0, 2).setTextAnchorType(TextAnchorType.TOP);
        table.get(1, 2).getTextFrame().setText("Center");
        table.get(1, 2).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);
        table.get(1, 2).setTextAnchorType(TextAnchorType.CENTER);
        table.get(2, 2).getTextFrame().setText("Bottom Right");
        table.get(2, 2).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.RIGHT);
        table.get(2, 2).setTextAnchorType(TextAnchorType.BOTTOM);

        //save to file
        presentation.saveToFile("output/Alignment.pptx", FileFormat.PPTX_2013);
        presentation.dispose();
    }
}

Output

Align Text in PowerPoint Table in Java

With Spire.XLS, developers can add text or image to the textbox to Excel worksheet easily. From version 9.3.10, Spire.XLS supports to set the inner margin of contents on Excel text box. With this feature, we can adjust the position of the text contents on the textbox to make it beautiful. This article is going to introduce how to set the inner margins of the textbox in Excel worksheet in C#.

{
     
     //load the sample document
     Workbook workbook = new Workbook();
     workbook.LoadFromFile("Sample.xlsx", ExcelVersion.Version2010);

     //get the first worksheet
     Worksheet sheet = workbook.Worksheets[0];

     //add a textbox to the sheet and set its position and size
     XlsTextBoxShape textbox = sheet.TextBoxes.AddTextBox(4, 2, 100, 300) as XlsTextBoxShape;
     
     //set the text on the textbox
     textbox.Text = "Insert TextBox in Excel and set the margin for the text";
     textbox.HAlignment = CommentHAlignType.Center;
     textbox.VAlignment = CommentVAlignType.Center;

     //set the inner margins of the contents 
     textbox.InnerLeftMargin = 1;
     textbox.InnerRightMargin = 3;
     textbox.InnerTopMargin = 1;
     textbox.InnerBottomMargin = 1;
     
     //save the document to file
     workbook.SaveToFile("Result.xlsx", ExcelVersion.Version2010);        
     
 }

Effective screenshot after setting the margins of the contents:

Set the internal margin of excel textbox in C#

Thursday, 21 March 2019 06:35

Get Cell Type in Excel in C#

Cell type refers to the data type in a cell. There are six cell types in Spire.XLS, i.e. String, Number, Formula, Boolean, Error and Blank. This article is going to show you how to get the cell types of specified cells in an Excel file using Spire.XLS.

Detail steps

Step 1: Create a Workbook instance and load the Excel file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Input.xlsx");

Step 2: Get the second worksheet.

Worksheet sheet = workbook.Worksheets[1];

Step 3: Get the cell types of the cells in range "A2:A7".

foreach (CellRange range in sheet.Range["A2:A7"])
{
    XlsWorksheet.TRangeValueType cellType = sheet.GetCellType(range.Row, range.Column, false);
    sheet[range.Row, range.Column+1].Text = cellType.ToString();
    sheet[range.Row, range.Column + 1].Style.Font.Color = Color.Red;
    sheet[range.Row, range.Column+1].Style.Font.IsBold = true;
}

Step 4: Save the file.

workbook.SaveToFile("GetCellType.xlsx", ExcelVersion.Version2013);

Output:

Get Cell Type in Excel in C#

Full code:

using System.Drawing;
using Spire.Xls;
using Spire.Xls.Core.Spreadsheet;

namespace GetCellType
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load the Excel file
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Input.xlsx");

            //Get the second worksheet
            Worksheet sheet = workbook.Worksheets[1];

            //Get the cell types of the cells in range “A2:A7”
            foreach (CellRange range in sheet.Range["A2:A7"])
            {
                XlsWorksheet.TRangeValueType cellType = sheet.GetCellType(range.Row, range.Column, false);
                sheet[range.Row, range.Column+1].Text = cellType.ToString();
                sheet[range.Row, range.Column + 1].Style.Font.Color = Color.Red;
                sheet[range.Row, range.Column+1].Style.Font.IsBold = true;
            }            

            //Save the file
            workbook.SaveToFile("GetCellType.xlsx", ExcelVersion.Version2013);            
        }
    }
}

To emphasize the text on a shape, we can only animate text instead of the whole shape. This article demonstrates how to apply animations to text in PowerPoint using Spire.Presenstation with C# and VB.NET.

Code Snippets

[C#]
//create a PowerPoint document
Presentation ppt = new Presentation();

//get the first slide
ISlide slide = ppt.Slides[0];

//add a shape to the slide
IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 50, 200, 80));
shape.Fill.FillType = FillFormatType.Solid;
shape.Fill.SolidColor.Color = Color.Purple;
shape.ShapeStyle.LineColor.Color = Color.White;
shape.AppendTextFrame("Welcome to download Spire.Presentation");

//apply animation to the text in shape
AnimationEffect animation = shape.Slide.Timeline.MainSequence.AddEffect(shape, AnimationEffectType.Float);
animation.SetStartEndParagraphs(0, 0);

//save to file
ppt.SaveToFile("result.pptx", FileFormat.Pptx2013);
ppt.Dispose();
[VB.NET]
'create a PowerPoint document
Dim ppt As Presentation =  New Presentation() 
 
'get the first slide
Dim slide As ISlide =  ppt.Slides(0) 
 
'add a shape to the slide
Dim shape As IAutoShape =  slide.Shapes.AppendShape(ShapeType.Rectangle,New RectangleF(50,50,200,80)) 
shape.Fill.FillType = FillFormatType.Solid
shape.Fill.SolidColor.Color = Color.Purple
shape.ShapeStyle.LineColor.Color = Color.White
shape.AppendTextFrame("Welcome to download Spire.Presentation")
 
'apply animation to the text in shape
Dim animation As AnimationEffect =  shape.Slide.Timeline.MainSequence.AddEffect(shape,AnimationEffectType.Float) 
animation.SetStartEndParagraphs(0, 0)
 
'save to file
ppt.SaveToFile("result.pptx", FileFormat.Pptx2013)
ppt.Dispose()

Output

Apply Animations to Text in PowerPoint in C#, VB.NET

Digital timestamps mark a PDF signature with the time and date as proof of integrity. A timestamp shows that the contents of the document existed at a point in time, and are unchanged. This article is going to introduce how to digitally sign a PDF document with a timestamp server by using Spire.PDF.

Code Snippets

[C#]
//create a PdfDocument object and load a PDF file
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\Example.pdf");

//load the certificate .pfx file
PdfCertificate cert = new PdfCertificate(@"C:\Users\Administrator\Desktop\gary.pfx", "e-iceblue");

//add a signature to the specified position
PdfSignature signature = new PdfSignature(doc, doc.Pages[0], cert, "signature");
signature.Bounds = new RectangleF(new PointF(350, 700), new SizeF(180, 90));

//set the signature content
signature.NameLabel = "Digitally signed by:Gary";
signature.LocationInfoLabel = "Location:";
signature.LocationInfo = "CN";
signature.ReasonLabel = "Reason: ";
signature.Reason = "Ensure authenticity";
signature.ContactInfoLabel = "Contact Number: ";
signature.ContactInfo = "028-81705109";
signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill | PdfCertificationFlags.ForbidChanges;
signature.GraphicsMode = GraphicMode.SignImageAndSignDetail;
signature.SignImageSource = PdfImage.FromFile(@"C:\Users\Administrator\Desktop\company-logo.jpg");

//configure a timestamp server
string url = "http://timestamp.wosign.com/rfc3161";
signature.ConfigureTimestamp(url);

//save to file
doc.SaveToFile("output.pdf");
[VB.NET]
'create a PdfDocument object and load a PDF file
Dim doc As PdfDocument =  New PdfDocument() 
doc.LoadFromFile("C:\Users\Administrator\Desktop\Example.pdf")
 
'load the certificate .pfx file
Dim cert As PdfCertificate =  New PdfCertificate("C:\Users\Administrator\Desktop\gary.pfx","e-iceblue") 
 
'add a signature to the specified position
Dim signature As PdfSignature =  New PdfSignature(doc,doc.Pages(0),cert,"signature") 
signature.Bounds = New RectangleF(New PointF(350, 700), New SizeF(180, 90))
 
'set the signature content
signature.NameLabel = "Digitally signed by:Gary"
signature.LocationInfoLabel = "Location:"
signature.LocationInfo = "CN"
signature.ReasonLabel = "Reason: "
signature.Reason = "Ensure authenticity"
signature.ContactInfoLabel = "Contact Number: "
signature.ContactInfo = "028-81705109"
signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill | PdfCertificationFlags.ForbidChanges
signature.GraphicsMode = GraphicMode.SignImageAndSignDetail
signature.SignImageSource = PdfImage.FromFile("C:\Users\Administrator\Desktop\company-logo.jpg")
 
'configure a timestamp server
Dim url As String =  "http://timestamp.wosign.com/rfc3161" 
signature.ConfigureTimestamp(url)
 
'save to file
doc.SaveToFile("output.pdf")

Output

How to Digitally Sign PDF with Timestamp Server in C#, VB.NET

Page 10 of 113