This article demonstrates how to add or delete rows and columns in a PowerPoint table using Spire.Presentation for Java.

Here is a screenshot of the sample PowerPoint file:

Add or Delete Rows and Columns from PowerPoint Table in Java

Add a row and a column

import com.spire.presentation.*;

public class AddRowAndColumn {

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

        //load the sample PowerPoint file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx");

        //get the table in the document
        ITable table = null;
        for (Object shape : presentation.getSlides().get(0).getShapes()) {
            if (shape instanceof ITable) {
                table = (ITable) shape;

                //add the last row to the end of the table as a new row
                int rowCount = table.getTableRows().getCount();
                TableRow row = table.getTableRows().get(rowCount - 1);
                table.getTableRows().append(row);

                //get the new row and set the text for each cell
                rowCount = table.getTableRows().getCount();
                row = table.getTableRows().get(rowCount - 1);
                row.get(0).getTextFrame().setText("America");
                row.get(1).getTextFrame().setText("Washington");
                row.get(2).getTextFrame().setText("North America");
                row.get(3).getTextFrame().setText("9372610");

                //add the last column to the end of the table as a new column
                int colCount = table.getColumnsList().getCount();
                TableColumn column =table.getColumnsList().get(colCount-1);
                table.getColumnsList().add(column);

                //get the new column and set the text for each cell
                colCount = table.getColumnsList().getCount();
                column = table.getColumnsList().get(colCount-1);
                column.get(0).getTextFrame().setText("Population");
                column.get(1).getTextFrame().setText("32370000");
                column.get(2).getTextFrame().setText("7350000");
                column.get(3).getTextFrame().setText("15040000");
                column.get(4).getTextFrame().setText("26500000");
                column.get(5).getTextFrame().setText("329740000");
            }
        }
        
        //save the document
        presentation.saveToFile("output/AddRowAndColumn.pptx", FileFormat.PPTX_2013);
    }
}

Add or Delete Rows and Columns from PowerPoint Table in Java

Delete a row and a column

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

public class DeleteRowAndColumn {

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

        //load the sample PowerPoint file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx");

        //get the table in the document
        ITable table = null;
        for (Object shape : presentation.getSlides().get(0).getShapes()) {
            if (shape instanceof ITable) {
                table = (ITable) shape;
                
                //delete the second column
                table.getColumnsList().removeAt(1, false);
                
                //delete the second row
                table.getTableRows().removeAt(1, false);
            }
        }
        
        //save the document
        presentation.saveToFile("output/DeleteRowAndColumn.pptx", FileFormat.PPTX_2013);
    }
}

Add or Delete Rows and Columns from PowerPoint Table in Java

Tuesday, 12 November 2019 05:56

Modify Hyperlinks in Word in Java

This article demonstrates how to modify hyperlinks in Word including modifying hyperlink address and display text using Spire.Doc for Java.

Below is the sample Word document we used for demonstration:

Modify Hyperlinks in Word in Java

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.Field;

import java.util.ArrayList;
import java.util.List;

public class ModifyHyperlink {
    public static void main(String[] args) {
        //Load Word document
        Document doc = new Document();
        doc.loadFromFile("Hyperlink.docx");

        List hyperlinks = new ArrayList();

        //Loop through the section in the document
        for (Section section : (Iterable<Section>) doc.getSections()
                ) {
            //Loop through the section in the document
            for (Paragraph para : (Iterable<Paragraph>) section.getParagraphs()
                    ) {
                for (DocumentObject obj:(Iterable<DocumentObject>) para.getChildObjects()
                     ) {
                    if (obj.getDocumentObjectType().equals(DocumentObjectType.Field)) {
                        Field field = (Field) obj;
                        if (field.getType().equals(FieldType.Field_Hyperlink)) {
                            hyperlinks.add(field);
                        }
                    }
                }
            }
        }

        hyperlinks.get(0).setCode("HYPERLINK \"http://www.google.com\"");
        hyperlinks.get(0).setFieldText("www.google.com");

        doc.saveToFile("EditHyperlink.docx", FileFormat.Docx_2013);
    }
}

Output:

Modify Hyperlinks in Word in Java

Monday, 11 November 2019 07:31

Remove footnote from Word document in Java

We have already demonstrated how to use Spire.Doc to insert footnote to word document in Java applications. This article will show how to remove footnote from word document in Java.

Firstly, view the sample document with footnotes:

Remove footnote from Word document in Java

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.*;

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

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

        Section section = document.getSections().get(0);

        //Traverse paragraphs in the section and find the footnote
        for (int j = 0; j < section.getParagraphs().getCount(); j++) {
            Paragraph para = section.getParagraphs().get(j);
            int index = -1;
            for (int i = 0, cnt = para.getChildObjects().getCount(); i < cnt; i++) {
                ParagraphBase pBase = (ParagraphBase) para.getChildObjects().get(i);
                if (pBase instanceof Footnote) {
                    index = i;
                    break;
                }
            }

            if (index > -1)
                //remove the footnote
                para.getChildObjects().removeAt(index);
        }
        document.saveToFile("Removefootnote.docx", FileFormat.Docx);
    }
}

Effective screenshot after remove the footnote from word document:

Remove footnote from Word document in Java

Friday, 08 November 2019 01:57

Apply Background Image to Slides in Java

This article demonstrates how to apply background image to all slides in a PowerPoint presentation using Spire.Presentation for Java.

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideBackground;
import com.spire.presentation.drawing.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;

public class AppplyBgToAllSlides {

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

        //load a PowerPoint file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx");

        //get the image data
        BufferedImage bufferedImage = ImageIO.read(new FileInputStream("C:\\Users\\Administrator\\Desktop\\bg.jpg"));
        IImageData imageData = presentation.getImages().append(bufferedImage);

        //loop through the slides
        for (int i = 0; i < presentation.getSlides().getCount() ; i++) {

            //apply the image to the specific slide as background
            SlideBackground background = presentation.getSlides().get(i).getSlideBackground();
            background.setType(BackgroundType.CUSTOM);
            background.getFill().setFillType(FillFormatType.PICTURE);
            background.getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
            background.getFill().getPictureFill().getPicture().setEmbedImage(imageData);
        }

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

Apply Background Image to Slides in Java

Tuesday, 05 November 2019 09:15

Detect and remove Word Macros in Java

Spire.Doc load the word document with macros, it also supports to detect if a Word document contains VBA macros and remove all the VBA macros from a word document. This article demonstrates how to detect and remove VBA macros from Word document in Java applications.

Firstly, please view the sample document with macros:

Detect and remove Word Macros in Java

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


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

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

        //If the document contains Macros, remove them from the document.
        if (doc.isContainMacro() )
        {
            doc.clearMacros();
        }

        //save to file
        doc.saveToFile("output/RemoveMacro.docm", FileFormat.Docm);

    }
}

Effective screenshot after clear the VBA macros from word document:

Detect and remove Word Macros in Java

Friday, 01 November 2019 09:12

Replace Image with New Image in Word in Java

This article demonstrates how to replace an existing image in a Word document with a new image using Spire.Doc for Java.

Below is the screenshot of the original Word document before replacing image:

Replace Image with New Image in Word in 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.Paragraph;
import com.spire.doc.fields.DocPicture;

public class ReplaceImages {
    public static void main(String[] args){
        //Load the Word document
        Document doc = new Document();
        doc.loadFromFile("Images.docx");

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

        //Loop through the paragraphs in the section
        for (Paragraph para:(Iterable) section.getParagraphs()
             ) {
            //Loop through the child object in the paragraph
            for (DocumentObject obj:(Iterable) para.getChildObjects()
                 ) {
                //Replace image with new image
                if(obj instanceof DocPicture){
                    DocPicture pic = (DocPicture)obj;
                    pic.loadImage("Hydrangeas.jpg");
                }
            }
        }

        //Save the resultant document
        doc.saveToFile("ReplaceWithImage.docx", FileFormat.Docx_2013);
    }
}

Output:

Replace Image with New Image in Word 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 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.PDF 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.PDF 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.PDF as Dependency in a .NET Standard Library Project

Step 4: Install the other five packages in your project via the NuGet Package Manager. They are SkiaSharp, System.Buffers, System.Memory, System.Text.Encoding.CodePages and System.Runtime.CompilerServices.Unsafe.

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.PDF 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 PDF documents.

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

namespace SpirePdfStandard
{
    public class Class1
    {
        public void CreatePdf()
        {
            //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");
        }

    }
}
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());        

        }
    }
}
Page 1 of 113