Tuesday, 29 October 2019 08:33

Rotate shapes on Word document in Java

This article demonstrates how to rotate shapes on a Word document using Spire.Doc for Java.

import com.spire.doc.Document;
import com.spire.doc.DocumentObject;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.*;
import com.spire.doc.fields.ShapeObject;

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

               //Load the Sample Word document.
                Document doc = new Document();
                doc.loadFromFile("InsertShapes.docx");

               //Get the first section
                Section sec = doc.getSections().get(0);

                //Traverse every paragraphs to get the shapes and rotate them
                 for ( Paragraph para: (Iterable) sec.getParagraphs()) {
                    for (DocumentObject obj : (Iterable) para.getChildObjects()) {

                      if (obj instanceof ShapeObject) {
                       ((ShapeObject) obj).setRotation(20);

                  }
             }
        }
                //Save to file
                doc.saveToFile("output/RotateShape.docx", FileFormat.Docx);

    }
}

Effective screenshot after rotating the shapes on word:

Rotate shapes on Word document in Java

Friday, 25 October 2019 09:26

Align Table in PowerPoint in C#

Spire.Presentation supports setting alignment for table in a PowerPoint document. This article demonstrates how to align a table to the bottom of a PowerPoint slide using Spire.Presentation.

Below screenshot shows the original table before setting alignment:

Align Table in PowerPoint in C#

using Spire.Presentation;

namespace AlignTable
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load PowerPoint document
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("Table.pptx");
            ITable table = null;
            //Loop through the shapes in the first slide
            foreach (IShape shape in ppt.Slides[0].Shapes)
            {
                //Find the table and align it to the bottom of the slide
                if (shape is ITable)
                {
                    table = (ITable)shape;
                    table.SetShapeAlignment(Spire.Presentation.ShapeAlignment.AlignBottom);
                }
            }

            //Save the resultant document
            ppt.SaveToFile("Result.pptx", FileFormat.Pptx2013);
        }
    }
}

Output:

Align Table in PowerPoint in C#

Thursday, 24 October 2019 07:46

Get PDF page size in C#

With Spire.PDF for .NET, developers can set page size for PDF in C#. This article will demonstrates how to get the PDF page size using Spire.PDF.

Detail steps:

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

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Sample.pdf");

Step 2: Get the width and height of the first page in the PDF file.

PdfPageBase page = doc.Pages[0];
float pointWidth = page.Size.Width;
float pointHeight = page.Size.Height;

Step 3: Convert the size with other measurement unit, such as in Inch, Centimeter, Unit or Pixel.

//Create PdfUnitConvertor to convert the unit
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();

//Convert the size with "pixel"
float pixelWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel);
float pixelHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel);

//Convert the size with "inch"
float inchWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Inch);
float inchHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Inch);

//Convert the size with "centimeter"
float centimeterWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Centimeter);
float centimeterHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Centimeter);

Step 4: Save to a .txt file.

//Create StringBuilder to save 
StringBuilder content = new StringBuilder();
//Add pointSize string to StringBuilder
content.AppendLine("The page size of the file is (width: " + pointWidth + "pt, height: " + pointHeight + "pt).");
content.AppendLine("The page size of the file is (width: " + pixelWidth + "pixel, height: " + pixelHeight + "pixel).");
content.AppendLine("The page size of the file is (width: " + inchWidth + "inch, height: " + inchHeight + "inch).");
content.AppendLine("The page size of the file is (width: " + centimeterWidth + "cm, height: " + centimeterHeight + "cm.)");

String output = "GetPageSize_out.txt";
//Save them to a txt file
File.WriteAllText(output, content.ToString());

Output:

Get PDF page size in C#

Full code:

using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.General;
using Spire.Pdf.Graphics;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GetPDFPageSize
{
    class Program
    {
        static void Main(string[] args)
        {

            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("Sample.pdf");

            //Get the first page of the loaded PDF file
            PdfPageBase page = doc.Pages[0];
            //Get the width of page based on "point"
            float pointWidth = page.Size.Width;
            //Get the height of page
            float pointHeight = page.Size.Height;

            //Create PdfUnitConvertor to convert the unit
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();

            //Convert the size with "pixel"
            float pixelWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel);
            float pixelHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel);

            //Convert the size with "inch"
            float inchWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Inch);
            float inchHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Inch);

            //Convert the size with "centimeter"
            float centimeterWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Centimeter);
            float centimeterHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Centimeter);

            //Create StringBuilder to save 
            StringBuilder content = new StringBuilder();
            
            //Add pointSize string to StringBuilder
            content.AppendLine("The page size of the file is (width: " + pointWidth + "pt, height: " + pointHeight + "pt).");
            content.AppendLine("The page size of the file is (width: " + pixelWidth + "pixel, height: " + pixelHeight + "pixel).");
            content.AppendLine("The page size of the file is (width: " + inchWidth + "inch, height: " + inchHeight + "inch).");
            content.AppendLine("The page size of the file is (width: " + centimeterWidth + "cm, height: " + centimeterHeight + "cm.)");

            String output = "GetPageSize_out.txt";

            //Save them to a txt file
            File.WriteAllText(output, content.ToString());        

        }
    }
}

Step 1: Download the latest version of Spire.Doc Pack from the link below, unzip it, and you'll get the DLL files for .NET Standarad from the "netstandard2.0" folder. If you already have this folder in your disk, go straight to step two.

How to Mannually Add Spire.Doc as Dependency in a .NET Standard Library Project

Step 2: Create a .NET Standard library project in your Visual Studio.

How to Mannually Add Spire.Doc as Dependency in a .NET Standard Library Project

Step 3: Add all DLL files under the "netstandard2.0" folder as dependencies in your project.

Right-click "Dependencies" – select "Add Reference" – click "Browse" – selcet all DLLs under "netstandard2.0" folder – click "Add".

How to Mannually Add Spire.Doc as Dependency in a .NET Standard Library Project

Step 4: Install the other three packages in your project via the NuGet Package Manager. They are SkiaSharp, System.Text.Encoding.CodePages and System.Security.Cryptography.Xml.

Right-click "Dependencies" – select "Manage NuGet Packages" – click "Browse" – type the package name – select the package from the search results – click "Install".

How to Mannually Add Spire.Doc as Dependency in a .NET Standard Library Project

Step 5: Now that you've added all the dependences successfully, you can start to write your own .NET Standard library that is capable of creating and processing Word documents.

using Spire.Doc;
using Spire.Doc.Documents;

namespace SpireDocStandard
{
    public class Class1
    {
        public void CreateWord()
        {
            //Create a document object
            Document doc = new Document();

            //Add a section
            Section section = doc.AddSection();

            //Add a paragrah
            Paragraph paragraph = section.AddParagraph();

            //Append text to the paragraph
            paragraph.AppendText("Hello World");

            //Save to file
            doc.SaveToFile("Output.docx", FileFormat.Docx2013);
        }  
    }
}
Thursday, 17 October 2019 09:30

Add Data Labels to Chart in PowerPoint in Java

This article demonstrates how to add data labels to a chart and set the appearance (border style and fill style) for the data labels in PowerPoint using Spire.Presentation for Java. Note some chart types like Surface3D, Surface3DNoColor, Contour and ContourNoColor do not support data labels.

Below screenshot shows the original chart before adding data labels:

Add Data Labels to Chart in PowerPoint in Java

import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import com.spire.presentation.charts.IChart;
import com.spire.presentation.charts.entity.ChartDataLabel;
import com.spire.presentation.charts.entity.ChartSeriesDataFormat;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.*;

public class AddDataLabelsToChart {
    public static void main(String[] args) throws Exception {
        //Load the PowerPoint document
        Presentation ppt = new Presentation();
        ppt.loadFromFile("Chart.pptx");

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);
        //Get the chart in the slide
        IChart chart = (IChart)slide.getShapes().get(0);

        //Loop through the series in the chart
        for (ChartSeriesDataFormat series:(Iterable)chart.getSeries()
             ) {
            //Add data labels for the data points in each series
            for(int i = 0; i < 4; i++){
                ChartDataLabel dataLabel = series.getDataLabels().add();
                //Show label value
                dataLabel.setLabelValueVisible(true);
                //Show series name
                dataLabel.setSeriesNameVisible(true);
                //Set border line style
                dataLabel.getLine().setFillType(FillFormatType.SOLID);
                dataLabel.getLine().getSolidFillColor().setColor(Color.RED);
                //Set fill style
                dataLabel.getFill().setFillType(FillFormatType.SOLID);
                dataLabel.getFill().getSolidColor().setColor(Color.YELLOW);
            }
        }

        //Save the resultant document
        ppt.saveToFile("DataLabels.pptx", FileFormat.PPTX_2013);
    }
}

Output:

Add Data Labels to Chart in PowerPoint in Java

Step 1: Download the latest version of Spire.PDF Pack from the link below, unzip it, and you'll get the DLL files for .NET Core in the "netcoreapp2.0" folder. If you already have this folder in your disk, go straight to step two.

How to Mannually Add Spire.PDF as Dependency in a .NET Core Application

Step 2: Create a .NET Core application in your Visual Studio.

How to Mannually Add Spire.PDF as Dependency in a .NET Core Application

Step 3: Add all DLL files under the "netcoreapp2.0" folder as dependencies in your project.

Right-click "Dependencies" – select "Add Reference" – click "Browse" – selcet all DLLs under "netcoreapp2.0" folder – click "Add".

How to Mannually Add Spire.PDF as Dependency in a .NET Core Application

Step 4: Install the other two packages in your project via the NuGet Package Manager. They are System.Drawing.Common and System.Text.Encoding.CodePages.

Right-click "Dependencies" – select "Manage NuGet Packages" – click "Browse" –type the package name – select the package from the search results – click "Install".

Note: If you fail to find these packages in the NuGet Package Manager, check if you have set the "nuget.org" as the "Package source".

How to Mannually Add Spire.PDF as Dependency in a .NET Core Application

Step 5: Now that you’ve added all the dependences successfully, you can start to code. The following code snippet gives you an exmaple of how to create a simple PDF document using Spire.PDF.

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace SpirePdfCore
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Add a page
            PdfPageBase page = doc.Pages.Add();

            //Draw text on the page at the specified position
            page.Canvas.DrawString("Hello World",
                                    new PdfFont(PdfFontFamily.Helvetica, 13f),
                                    new PdfSolidBrush(Color.Black),
                                    new PointF(50, 50));

            //Save the document
            doc.SaveToFile("Output.pdf");
        }
    }
}

This article will demonstrate how to merge and split table cells on word document in Java applications.

Merge Table cells:

import com.spire.doc.*;

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

        String output = "output/MergeTableCells.docx";

        //Create a Document instance
        Document document = new Document();

        Section section = document.addSection();
        Table table = section.addTable(true);
        table.resetCells(4, 4);
        
        //how to merge cells horizontally
        table.applyHorizontalMerge(0, 0, 3);

        //how to merge cells vertically
        table.applyVerticalMerge(0, 2, 3);

        //save the document to file
        document.saveToFile(output, FileFormat.Docx);
    }
}

Java merge and split table cells on Word document

Split table cells:

import com.spire.doc.*;

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

        String output = "output/SplitTableCells.docx";

        //Create a Document instance
        Document document = new Document();

        Section section = document.addSection();
        Table table = section.addTable(true);
        table.resetCells(4, 4);

        //split the cell
        table.getRows().get(3).getCells().get(3).splitCell(2, 2);

        //save the document to file
        document.saveToFile(output, FileFormat.Docx);
    }
}

Java merge and split table cells on Word document

Step 1: Download the latest version of Spire.Doc Pack from the link below, unzip it, and you'll get the DLL files for .NET Core in the “netcoreapp2.0” folder. If you already have this folder in your disk, go straight to step two.

How to Mannually Add Spire.Doc as Dependency in a .NET Core Application

Step 2: Create a .Net Core application in your Visual Studio.

How to Mannually Add Spire.Doc as Dependency in a .NET Core Application

Step 3: Add all DLL files under the "netcoreapp2.0" folder as dependencies in your project.

Right-click "Dependencies" – select "Add Reference" – click "Browse" – selcet all DLLs under "netcoreapp2.0" folder – click "Add".

How to Mannually Add Spire.Doc as Dependency in a .NET Core Application

Step 4: Install the other three packages in your project via the NuGet Package Manager. They are System.Drawing.Common, System.Text.Encoding.CodePages and System.Security.Cryptography.Xml.

Right-click "Dependencies" – select "Manage NuGet Packages" – click "Browse" –type the package name – select the package from the search results – click "Install".

Note: If you fail to find these packages in the NuGet Package Manager, check if you have set the "nuget.org" as the "Package source".

How to Mannually Add Spire.Doc as Dependency in a .NET Core Application

Step 5: Now that you've added all the dependences successfully, you can start to code. The following code snippet gives you an exmaple of how to create a simple Word document using Spire.Doc.

using Spire.Doc;
using Spire.Doc.Documents;

namespace SpireDocNetCore
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a document object
            Document doc = new Document();

            //Add a section
            Section section = doc.AddSection();

            //Add a paragrah
            Paragraph paragraph = section.AddParagraph();

            //Append text to the paragraph
            paragraph.AppendText("This article shows you how to mannually add Spire.Doc as dependency in a .NET Core application.");

            //Save to file
            doc.SaveToFile("Output.docx", FileFormat.Docx2013);
        }
    }
}
Saturday, 12 October 2019 09:41

Detect PDF Page Orientation in Java

Spire.PDF for Java supports detecting the orientation of a PDF page by comparing the value of page width and page height. This article shows how to use Spire.PDF for Java to accomplish this function.

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

public class DetectPageOrientation {
    public static void main(String[] args){
        //Load PDF file
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Fields.pdf");

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

        //Compare the value of page width and height
        if (page.getSize().getWidth()> page.getSize().getHeight()){
            System.out.println("The page orientation is Landscape");
        }
        else{
            System.out.println("The page orientation is Portrait");
        }
    }
}

Output

Detect PDF Page Orientation in Java

Saturday, 12 October 2019 03:45

Auto Fit Text or Shape in PowerPoint in Java

This article demonstrates how to automatically shrink text to fit a shape or how to automatically resize a shape to fit text by using Spire.Presentation for Java.

import com.spire.presentation.*;

import java.awt.geom.Rectangle2D;

public class AutoFitTextOrShape {

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

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

        //get the first slide 
        ISlide slide = presentation.getSlides().get(0);

        //add a shape to slide 
        IAutoShape textShape1 = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float(50,50,200,80));

        //add text to shape
        textShape1.getTextFrame().setText("Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape.");

        //set the auto-fit type to normal, which means the text automatically shrinks to fit the shape when text overflows the shape
        textShape1.getTextFrame().setAutofitType(TextAutofitType.NORMAL);

        //add another shape to slide 
        IAutoShape textShape2 = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float(350, 50, 200, 80));
        textShape2.getTextFrame().setText("Resize shape to fit text.");

        //set the auto-fit type to shape, which means the shape size automatically decreases or increases to fit text
        textShape2.getTextFrame().setAutofitType(TextAutofitType.SHAPE);

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

Auto Fit Text or Shape in PowerPoint in Java

Page 10 of 121