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

Monday, 26 September 2022 07:03

Java: Convert PowerPoint to XPS

XPS (XML Paper Specification) is a fixed-format document described by an XML-based language. It maintains a consistent appearance for documents, which is an ideal file format for publishing, archiving and transmitting. This article will demonstrate how to programmatically convert PowerPoint to XPS using Spire.Presentation for Java.

Install Spire.Presentation for Java

First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation</artifactId>
        <version>9.3.1</version>
    </dependency>
</dependencies>
    

Convert PowerPoint to XPS

The detailed steps are as follows:

  • Create a Presentation instance.
  • Load a sample PowerPoint document using Presentation.loadFromFile() method.
  • Save the PowerPoint document to XPS using Presentation.saveToFile(java.lang.String file, FileFormat fileFormat) method.
  • Java
import com.spire.presentation.*;

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

        //Create a Presentation instance
        Presentation ppt = new Presentation();

        //Load a sample PowerPoint file
        ppt.loadFromFile("E:\\Files\\test.pptx");

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

Java: Convert PowerPoint to XPS

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.

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#.

using Spire.Xls;
using Spire.Xls.Core.Spreadsheet.Shapes;
namespace SetInternalMargin
{
    class Program
    {
        static void Main(string[] args)
        {
            {

                //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#]
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Security;
using System.Drawing;


namespace SignPDFwithTimestamp
{
    class Program
    {
        static void Main(string[] args)
        {
            //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]
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Security
Imports System.Drawing


Namespace SignPDFwithTimestamp
	Class Program
		Private Shared Sub Main(args As String())
			'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")
		End Sub
	End Class
End Namespace

Output

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

PowerPoint print settings allow users to control how presentation slides are printed, such as print all slides, print some selected slides, print slides with frames or not, print many slides into one page, print order, print color and so on. This article will show you how to set print options when print PowerPoint documents in C#.

Firstly, view Microsoft PowerPoint's page print settings:

Set the print settings of PowerPoint document in C#

Code Snippets of set print settings of PowerPoint document by using PrinterSettings object to print the presentation slides:

static void Main(string[] args)
{
    //load the sample document from file
    Presentation ppt = new Presentation();
    ppt.LoadFromFile("Sample.pptx");


    //use PrinterSettings object to print presentation slides
    PrinterSettings ps = new PrinterSettings();
    ps.PrintRange = PrintRange.AllPages;
    ps.PrintToFile = true;
    ps.PrintFileName = ("Print.xps");

    //print the slide with frame
    ppt.SlideFrameForPrint = true;

    //print the slide with Grayscale
    ppt.GrayLevelForPrint = true;

    //Print 4 slides horizontal
    ppt.SlideCountPerPageForPrint = PageSlideCount.Four;
    ppt.OrderForPrint = Order.Horizontal;


    ////only select some slides to print
    //ppt.SelectSlidesForPrint("1", "3");


    //print the document
    ppt.Print(ps);

}

Code Snippets of set print document name by using PrintDocument object to print presentation slides:

static void Main(string[] args)
{
    //load the sample document from file
    Presentation ppt = new Presentation();
    ppt.LoadFromFile("Sample.pptx");

    //use PrintDocument object to print presentation slides
    PresentationPrintDocument document = new PresentationPrintDocument(ppt);

    //print document to virtual printer 
    document.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";

    //print the slide with frame
    ppt.SlideFrameForPrint = true;

    //print 4 slides horizontal
    ppt.SlideCountPerPageForPrint = PageSlideCount.Four;
    ppt.OrderForPrint = Order.Horizontal;

    //print the slide with Grayscale
    ppt.GrayLevelForPrint = true;

    //set the print document name
    document.DocumentName = "Print Task"; 

    document.PrinterSettings.PrintToFile = true;
    document.PrinterSettings.PrintFileName = ("Print.xps");

    ppt.Print(document);                   
  
}