Thursday, 01 July 2021 06:31

Set Text Alignment within a Shape in Java

This article demonstrates how to set text horizontal and vertical alignment within a shape using Spire.Presentation for Java.

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.*;
import java.awt.geom.Rectangle2D;

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

        //Create a Presentation object 
        Presentation presentation = new Presentation();
        presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

        //Add a shape 
        IAutoShape textShape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float(50, 50, 400, 200));
        textShape.getShapeStyle().getLineColor().setColor(Color.DARK_GRAY);
        textShape.getFill().setFillType(FillFormatType.NONE);

        //Remove the default paragraphs 
        textShape.getTextFrame().getParagraphs().clear();

        //Add a paragraph and append some text to it 
        textShape.getTextFrame().getParagraphs().append(new ParagraphEx());
        textShape.getTextFrame().getParagraphs().get(0).getTextRanges().append(new PortionEx("Text Alignment"));
        textShape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setFontHeight(20f);
        textShape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setLatinFont(new TextFont("Arial"));
        textShape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
        textShape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).getFill().getSolidColor().setColor(Color.BLACK);

        //Set text horizontal alignment to right 
        textShape.getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.RIGHT);

        //Set text vertical alignment to bottom 
        textShape.getTextFrame().setAnchoringType(TextAnchorType.BOTTOM);

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

Set Text Alignment within a Shape in Java

This article demonstrates how to get the position of text in PowerPoint in Java using Spire.Presentation for Java.

import com.spire.presentation.IAutoShape;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;

import java.awt.geom.Point2D;

public class GetPositionOfText {
    public static void main(String []args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Load a PowerPoint document
        ppt.loadFromFile("Sample.pptx");

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);
        //Get the first shape
        IAutoShape shape = (IAutoShape)slide.getShapes().get(0);
        //Get location of text in the shape
        Point2D location =shape.getTextFrame().getTextLocation();

        //Print out the x and y coordinates of the location relative to slide
        String  point1="Text's position relative to Slide: x= "+location.getX()+" y = "+location.getY();
        System.out.println(point1);

        //Print out the x and y coordinates of the location relative to shape
        String point2 = "Text's position relative to shape: x= " + (location.getX() - shape.getLeft()) + "  y = " + (location.getY() - shape.getTop());
        System.out.println(point2);
    }
}

Output:

Get Position of Text in PowerPoint in Java

In the earlier tutorial, we have given a brief introduction on how to insert Textbox in Word and this article will demonstrate how to position the text vertically in a text box using Spire.Doc for .NET.

C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;

namespace WordTextbox
{
    class Program
    {
        static void Main(string[] args)
        {
            // Instantiate document object
            Document document = new Document();

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

            //Set the margin
            section.PageSetup.Margins.Left = 90;
            section.PageSetup.Margins.Right = 90;
            Paragraph paragraph = section.AddParagraph();

            //Add texbox 1
            TextBox textBox1 = paragraph.AppendTextBox(section.PageSetup.Margins.Left - 20, section.PageSetup.PageSize.Height + 20);
           
            //Fix the position of textbox 
            textBox1.Format.HorizontalOrigin = HorizontalOrigin.Page;
            textBox1.Format.HorizontalPosition = 0;
            textBox1.Format.VerticalPosition = -10f;
            textBox1.Format.VerticalOrigin = VerticalOrigin.Page;

            //Set the text vertically
            textBox1.Format.TextAnchor = ShapeVerticalAlignment.Center;
            textBox1.Format.LayoutFlowAlt = TextDirection.LeftToRight;

            //Add text and set the font
            Paragraph textboxPara1 = textBox1.Body.AddParagraph();
            TextRange txtrg = textboxPara1.AppendText("Name_______Number_________Class__________");
            txtrg.CharacterFormat.FontName = "Arial";
            txtrg.CharacterFormat.FontSize = 10;
            txtrg.CharacterFormat.TextColor = System.Drawing.Color.Black;
            textboxPara1.Format.HorizontalAlignment = HorizontalAlignment.Center;

            //Save the document
            document.SaveToFile("Result.docx");

        }
    }
}
VB.NET
Namespace WordTextbox
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            Dim document As Document = New Document
            Dim section As Section = document.AddSection
            section.PageSetup.Margins.Left = 90
            section.PageSetup.Margins.Right = 90
            Dim paragraph As Paragraph = section.AddParagraph
            Dim textBox1 As TextBox = paragraph.AppendTextBox((section.PageSetup.Margins.Left - 20), (section.PageSetup.PageSize.Height + 20))
            textBox1.Format.HorizontalOrigin = HorizontalOrigin.Page
            textBox1.Format.HorizontalPosition = 0
            textBox1.Format.VerticalPosition = -10!
            textBox1.Format.VerticalOrigin = VerticalOrigin.Page
            textBox1.Format.TextAnchor = ShapeVerticalAlignment.Center
            textBox1.Format.LayoutFlowAlt = TextDirection.LeftToRight
            Dim textboxPara1 As Paragraph = textBox1.Body.AddParagraph
            Dim txtrg As TextRange = textboxPara1.AppendText("Name_______Number_________Class__________")
            txtrg.CharacterFormat.FontName= "Arial"
            txtrg.CharacterFormat.FontSize = 10
            txtrg.CharacterFormat.TextColor = System.Drawing.Color.Black
            textboxPara1.Format.HorizontalAlignment = HorizontalAlignment.Center
            document.SaveToFile("Result.docx")
        End Sub
    End Class
End Namespace

Output

How to Set Text Direction in Textbox in C#, VB.NET

This article demonstrates how to evenly distribute the rows and columns of a PowerPoint table using Spire.Presentation for Java.

The input document:

Evenly Distribute Rows and Columns in PowerPoint Table in Java

import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.ITable;
import com.spire.presentation.Presentation;

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

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);
        //Get the table in the slide
        ITable table = (ITable) slide.getShapes().get(0);
        //Distribute table rows
        table.distributeRows(0,4);
        //Distribute table columns
        table.distributeColumns(0,4);

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

The output document:

Evenly Distribute Rows and Columns in PowerPoint Table in Java

This article demonstrates how to apply transparency to text in PowerPoint using Spire.Presentation for Java.

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class ApplyTransparency {

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

        //Create a PowerPoint document
        Presentation presentation = new Presentation();
        presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

        //Add a shape
        IAutoShape textbox = presentation .getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE,new Rectangle2D.Float(50, 70, 300, 120));
        textbox.getShapeStyle().getLineColor().setColor(new Color(1,1,1,0));
        textbox.getFill().setFillType(FillFormatType.NONE);

        //Remove default paragraphs
        textbox.getTextFrame().getParagraphs().clear();

        //Add three paragraphs and apply colors with different alpha values to the text
        int alpha = 55;
        for (int i = 0; i < 3; i++)
        {
            textbox.getTextFrame().getParagraphs().append(new ParagraphEx());
            textbox.getTextFrame().getParagraphs().get(i).getTextRanges().append(new PortionEx("Text Transparency"));
            textbox.getTextFrame().getParagraphs().get(i).getTextRanges().get(0).getFill().setFillType(FillFormatType.NONE);
            textbox.getTextFrame().getParagraphs().get(i).getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
            textbox.getTextFrame().getParagraphs().get(i).getTextRanges().get(0).getFill().getSolidColor().setColor(new Color(176, 48, 96, alpha));
            alpha += 100;
        }

        //Save to file
        presentation.saveToFile("TextTransparency.pptx", FileFormat.PPTX_2013);
    }
}

Apply Transparency to Text in PowerPoint in Java

This article demonstrates the steps to use Spire.OCR for .NET in .NET Core applications.

Step 1: Create a .NET Core project in Visual Studio.

How to use Spire.OCR for .NET in .NET Core Applications

Step 2: Add reference to Spire.OCR for .NET DLLs in your project.

You can add reference to Spire.OCR for .NET DLLs through one of the following two ways:

1. Install Spire.OCR for .NET through NuGet using NuGet Package Manager (recommended):

  • In Solution Explorer, right-click the project or "Dependencies" and select "Manage NuGet Packages".
  • Click "Browse" tab and search Spire.OCR.
  • Install Spire.OCR.

How to use Spire.OCR for .NET in .NET Core Applications

2. Manually add reference to Spire.OCR for .NET DLLs.

  • Download Spire.OCR for .NET package from the following link, unzip it, you will get the DLLs from the "netstandard2.0" folder.
  • How to use Spire.OCR for .NET in .NET Core Applications

  • Right-click the project or "Dependencies" – select "Add Reference" – click "Browse" – select all DLLs under "netstandard2.0" folder – click "Add".
  • How to use Spire.OCR for .NET in .NET Core Applications

  • Install the other two packages: SkiaSharp and System.Text.Encoding.CodePages in your project via the NuGet Package Manager.
  • Right-click the project or "Dependencies" – select "Manage NuGet Packages" – click "Browse" – type the package name – select the package from the search results – click "Install".

    How to use Spire.OCR for .NET in .NET Core Applications

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

Step 3: Copy dependency DLLs to running directory of your project.

If you install Spire.OCR from NuGet and your project's target framework is .NET Core 3.0 or above, please build the project, then copy the 6 DLLs from bin\Debug\netcoreapp3.0\runtimes\win-x64\native folder to the running directory such as bin\Debug\netcoreapp3.0 or C:\Windows\System32 .

How to use Spire.OCR for .NET in .NET Core Applications

If your project's target framework is below .NET Core 3.0 or you download Spire.OCR from our website, please copy the 6 DLLs from Spire.OCR\Spire.OCR_Dependency\x64 folder to the running directory such as bin\Debug\netcoreapp2.1 or C:\Windows\System32.

How to use Spire.OCR for .NET in .NET Core Applications

Step 4: Now you have successfully included Spire.OCR in your project. You can refer the following code example to scan images using Spire.OCR.

C#
using Spire.OCR;
using System.IO;

namespace SpireOCR
{
    class Program
    {
        static void Main(string[] args)
        {
            OcrScanner scanner = new OcrScanner();            
            scanner.Scan("image.png");
            File.WriteAllText("output.txt", scanner.Text.ToString());
        }
    }
}
VB.NET
Imports Spire.OCR
Imports System.IO

Namespace SpireOCR
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Dim scanner As OcrScanner = New OcrScanner()
            scanner.Scan("image.png")
            File.WriteAllText("output.txt", scanner.Text.ToString())
        End Sub
    End Class
End Namespace
Tuesday, 15 June 2021 02:53

Convert PowerPoint to HTML in Java

This article shows you how to convert PowerPoint documents to HTML format files using Spire.Presentation for Java.

Here is a screenshot of the sample PowerPoint document.

Convert PowerPoint to HTML in Java

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;

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

        //Create a Presentation object 
        Presentation presentation = new Presentation();

        //Load the sample document 
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pptx");

        //Save the document to HTML format 
        presentation.saveToFile("C:\\Users\\Administrator\\Desktop\\ToHtml.html", FileFormat.HTML);
    }
}

Output

Convert PowerPoint to HTML in Java

Wednesday, 09 June 2021 06:01

Add Digital Signatures to Word in Java

This article shows you how to digitally sign a Word document using Spire.Doc for Java.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class AddDigitalSignature {

    public static void main(String[] args) {


        //Create a Document object
        Document doc = new Document();

        //Load  the sample Word file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Java Doc.docx");

        //Specify certificate path
        String certificatePath = "C:\\Users\\Administrator\\Desktop\\gary.pfx";

        //Specify password of the certificate
        String certificatePassword = "e-iceblue";

        //Sign the document with certificate and save to file
        doc.saveToFile("AddDigitalSignature.docx", FileFormat.Docx_2013, certificatePath, certificatePassword);
    }
}

Add Digital Signatures to Word in Java

This article demonstrates how to create multi-level category chart in Excel using Spire.XLS for Java.

import com.spire.xls.*;
import com.spire.xls.charts.*;

public class CreateMultiLevelChart {
    public static void main(String []args) throws Exception {
        //create a workbook
        Workbook workbook = new Workbook();
        Worksheet sheet = workbook.getWorksheets().get(0);

        //write data to cells
        sheet.getCellRange("A1").setText( "Main Category");
        sheet.getCellRange("A2").setText("Fruit");
        sheet.getCellRange("A6").setText("Vegies");
        sheet.getCellRange("B1").setText("Sub Category");
        sheet.getCellRange("B2").setText( "Bananas");
        sheet.getCellRange("B3").setText( "Oranges");
        sheet.getCellRange("B4").setText( "Pears");
        sheet.getCellRange("B5").setText("Grapes");
        sheet.getCellRange("B6").setText( "Carrots");
        sheet.getCellRange("B7").setText( "Potatoes");
        sheet.getCellRange("B8").setText( "Celery");
        sheet.getCellRange("B9").setText( "Onions");
        sheet.getCellRange("C1").setText("Value");
        sheet.getCellRange("C2").setValue("52");
        sheet.getCellRange("C3").setValue( "65");
        sheet.getCellRange("C4").setValue( "50");
        sheet.getCellRange("C5").setValue( "45");
        sheet.getCellRange("C6").setValue( "64");
        sheet.getCellRange("C7").setValue( "62");
        sheet.getCellRange("C8").setValue( "89");
        sheet.getCellRange("C9").setValue( "57");

        //vertically merge cells from A2 to A5, A6 to A9
        sheet.getCellRange("A2:A5").merge();
        sheet.getCellRange("A6:A9").merge();
        sheet.autoFitColumn(1);
        sheet.autoFitColumn(2);

        //add a clustered bar chart to worksheet
        Chart chart = sheet.getCharts().add(ExcelChartType.BarClustered);
        chart.setChartTitle( "Value");
        chart.getPlotArea().getFill().setFillType( ShapeFillType.NoFill);
        chart.getLegend().delete();
        chart.setLeftColumn(5);
        chart.setTopRow(1);
        chart.setRightColumn(14);

        //set the data source of series data
        chart.setDataRange(sheet.getCellRange("C2:C9"));
        chart.setSeriesDataFromRange(false);

        //set the data source of category labels
        ChartSerie serie = chart.getSeries().get(0);
        serie.setCategoryLabels( sheet.getCellRange("A2:B9"));

        //show multi-level category labels
        chart.getPrimaryCategoryAxis().setMultiLevelLable( true);

        //save the document
        workbook.saveToFile("output/createMultiLevelChart.xlsx", ExcelVersion.Version2013);
    }
}

Output:

Create Multi-Level Category Chart in Excel in Java

It is possible to perform Word to PDF conversion in Azure apps such as Azure Web apps and Azure Functions apps using Spire.Doc for .NET. In this article, you can see the code example to achieve this function with Spire.Doc for .NET.

The input Word document:

C#, VB.NET Convert Word to PDF in Azure Apps

Step 1: Install Spire.Doc NuGet Package as a reference to your project from NuGet.org.

C#, VB.NET Convert Word to PDF in Azure Apps

Step 2: Add the following code to convert Word to PDF.

C#
//Create a Document instance
Document document = new Document();
//Load the Word document
document.LoadFromFile(@"sample.docx");

//Create a ToPdfParameterList instance
ToPdfParameterList ps = new ToPdfParameterList
{
    UsePSCoversion = true
};
//Save Word document to PDF using PS conversion
document.SaveToFile("ToPdf.pdf", ps);
VB.NET
Private Sub SurroundingSub()
    Dim document As Document = New Document()
    document.LoadFromFile("sample.docx")
    Dim ps As ToPdfParameterList = New ToPdfParameterList With {
        .UsePSCoversion = True
    }
    document.SaveToFile("ToPdf.pdf", ps)
End Sub

The Output PDF document:

C#, VB.NET Convert Word to PDF in Azure Apps

Page 10 of 143