page 161

Add Shapes to PowerPoint in Java

2019-01-03 06:34:39 Written by Koohji

This article demonstrates how to add a variety of shapes to a PowerPoint slide and how to fill the shapes with a solid color, a picture, a pattern or a gradient.

Entire Code

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

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;

public class AddShapes {

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

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

        //append a Triangle and fill the shape with a solid color
        IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.TRIANGLE, new Rectangle2D.Double(115, 130, 100, 100));
        shape.getFill().setFillType(FillFormatType.SOLID);
        shape.getFill().getSolidColor().setColor(Color.orange);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //append an Ellipse and fill the shape with a picture
        shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.ELLIPSE, new Rectangle2D.Double(290, 130, 150, 100));
        shape.getFill().setFillType(FillFormatType.PICTURE);
        shape.getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
        BufferedImage image = ImageIO.read(new File("C:\\Users\\Administrator\\Desktop\\logo.png"));
        shape.getFill().getPictureFill().getPicture().setEmbedImage(presentation.getImages().append(image));
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //append a Heart and fill the shape with a pattern
        shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.HEART, new Rectangle2D.Double(515, 130, 130, 100));
        shape.getFill().setFillType(FillFormatType.PATTERN);
        shape.getFill().getPattern().setPatternType(PatternFillType.CROSS);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //append a Five-Pointed Star and fill the shape with a gradient color
        shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.FIVE_POINTED_STAR, new Rectangle2D.Double(115, 300, 100, 100));
        shape.getFill().setFillType(FillFormatType.GRADIENT);
        shape.getFill().getGradient().getGradientStops().append(0, KnownColors.BLACK);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //append a Rectangle and fill the shape with gradient colors
        shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Double(290, 300, 150, 100));
        shape.getFill().setFillType(FillFormatType.GRADIENT);
        shape.getFill().getGradient().getGradientStops().append(0, KnownColors.LIGHT_SKY_BLUE);
        shape.getFill().getGradient().getGradientStops().append(1, KnownColors.ROYAL_BLUE);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //append a Bent Up Arrow and fill the shape with gradient colors
        shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.BENT_UP_ARROW, new Rectangle2D.Double(515, 300, 130, 100));
        shape.getFill().setFillType(FillFormatType.GRADIENT);
        shape.getFill().getGradient().getGradientStops().append(1f, KnownColors.OLIVE);
        shape.getFill().getGradient().getGradientStops().append(0, KnownColors.POWDER_BLUE);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

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

Output

Add Shapes to PowerPoint in Java

Making your slides easy to read is vital to creating an effective PowerPoint presentation. One of the most common ways of doing this is to format the text as a bulleted or numbered list. A list can help organize information in a neat manner as well as attract the reader’s attention. In this article, you will learn how to create numbered lists and bulleted lists in a PowerPoint slide in Java 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>11.1.1</version>
    </dependency>
</dependencies>

Create a Numbered List in PowerPoint in Java

Spire.Presentation supports adding numerals or bullet points in front of paragraphs to create a numbered or bulleted list. To specify the bullet type, use ParagraphEx.setBulletType() method. The following are the steps to create a numbered list in a PowerPoint slide using Spire.Presentation for Java.

  • Create a Presentation object.
  • Get the first slide using Presentation.getSlides().get() method.
  • Append a shape to the slide using ISlide.getShapes().appendShape() method.
  • Specify list content inside a String list.
  • Create paragraphs based on the list content, and set the bullet type of these paragraphs to NUMBERED using ParagraphEx.setBulletType() method.
  • Set the numbered bullet style using ParagraphEx.setBulletStyle() method.
  • Save the document to a PowerPoint file using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

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

public class CreateNumberedList {

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

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

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

        //Append a shape to the slide and set shape style
        Rectangle2D rect = new Rectangle2D.Double(50, 50, 300, 200);
        IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect);
        shape.getLine().setFillType(FillFormatType.NONE);
        shape.getFill().setFillType(FillFormatType.NONE);

        //Add text to the default paragraph
        ParagraphEx titleParagraph = shape.getTextFrame().getParagraphs().get(0);
        titleParagraph.setText("Required Web Development Skills:");
        titleParagraph.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
        titleParagraph.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.black);
        titleParagraph.setAlignment(TextAlignmentType.LEFT);

        //Specify list content
        String[] listContent = new String[] {
                " Command-line Unix",
                " Vim",
                " HTML",
                " CSS",
                " Python",
                " JavaScript",
                " SQL"
        };

        //Create a numbered list
        for(int i = 0; i < listContent.length; i ++)
        {
            ParagraphEx paragraph = new ParagraphEx();
            shape.getTextFrame().getParagraphs().append(paragraph);
            paragraph.setText(listContent[i]);
            paragraph.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
            paragraph.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.black);
            paragraph.setBulletType(TextBulletType.NUMBERED);
            paragraph.setBulletStyle(NumberedBulletStyle.BULLET_ARABIC_PERIOD);
        }

        //Save the document
        presentation.saveToFile("NumberedList.pptx", FileFormat.PPTX_2013);
    }
}

Java: Create Numbered or Bulleted Lists in PowerPoint

Create a Bulleted List with Symbol Bullets in PowerPoint in Java

The process of creating a bulleted list using symbol bullets is very similar to that of creating a numbered list. The only difference is that you need to set the bullet type to SYMBOL. The following are the steps.

  • Create a Presentation object.
  • Get the first slide using Presentation.getSlides().get() method.
  • Append a shape to the slide using ISlide.getShapes().appendShape() method.
  • Specify list content inside a String list.
  • Create paragraphs based on the list content, and set the bullet type of these paragraphs to SYMBOL using ParagraphEx.setBulletType() method.
  • Save the document to a PowerPoint file using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

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

public class CreateBulletedListWithSymbol {

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

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

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

        //Append a shape to the slide and set shape style
        Rectangle2D rect = new Rectangle2D.Double(50, 50, 350, 200);
        IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect);
        shape.getLine().setFillType(FillFormatType.NONE);
        shape.getFill().setFillType(FillFormatType.NONE);

        //Add text to the default paragraph
        ParagraphEx titleParagraph = shape.getTextFrame().getParagraphs().get(0);
        titleParagraph.setText("Computer Science Subjects:");
        titleParagraph.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
        titleParagraph.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.black);
        titleParagraph.setAlignment(TextAlignmentType.LEFT);

        //Specify list content
        String[] listContent = new String[] {
                " Data Structure",
                " Algorithm",
                " Computer Networks",
                " Operating System",
                " Theory of Computations",
                " C Programming",
                " Computer Organization and Architecture"
        };

        //Create a bulleted list with symbol bullets
        for(int i = 0; i < listContent.length; i ++)
        {
            ParagraphEx paragraph = new ParagraphEx();
            shape.getTextFrame().getParagraphs().append(paragraph);
            paragraph.setText(listContent[i]);
            paragraph.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
            paragraph.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.black);
            paragraph.setBulletType(TextBulletType.SYMBOL);
        }

        //Save the document
        presentation.saveToFile("SymbolBullets.pptx", FileFormat.PPTX_2013);
    }
}

Java: Create Numbered or Bulleted Lists in PowerPoint

Create a Bulleted List with Image Bullets in PowerPoint in Java

To use an image as bullet points, you need to set the bullet type to PICTURE and specify an image for the BulletPicture object. The following are the detailed steps.

  • Create a Presentation object.
  • Get the first slide using Presentation.getSlides().get() method.
  • Append a shape to the slide using ISlide.getShapes().appendShape() method.
  • Specify list content inside a String list.
  • Create paragraphs based on the list content, and set the bullet type of these paragraphs to PICTURE using ParagraphEx.setBulletType() method.
  • Set the image for the bullets using Paragraph.getBulletPicture().setEmbedImage() method.
  • Save the document to a PowerPoint file using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;

public class CreateBulletedListWithImage {

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

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

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

        //Append a shape to the slide and set shape style
        Rectangle2D rect = new Rectangle2D.Double(50, 50, 400, 180);
        IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect);
        shape.getLine().setFillType(FillFormatType.NONE);
        shape.getFill().setFillType(FillFormatType.NONE);

        //Add text to the default paragraph
        ParagraphEx titleParagraph = shape.getTextFrame().getParagraphs().get(0);
        titleParagraph.setText("Project Task To-Do List:");
        titleParagraph.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
        titleParagraph.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.black);
        titleParagraph.setAlignment(TextAlignmentType.LEFT);

        //Specify list content
        String[] listContent = new String[] {
                " Define projects and tasks you're working on",
                " Assign people to tasks",
                " Define the priority levels of your tasks",
                " Keep track of the progress status of your tasks",
                " Mark tasks as done when completed"
        };

        //Create a bulleted list with image bullets
        BufferedImage image = ImageIO.read(new File("C:\\Users\\Administrator\\Desktop\\R-C25.png"));
        for(int i = 0; i < listContent.length; i ++)
        {
            ParagraphEx paragraph = new ParagraphEx();
            shape.getTextFrame().getParagraphs().append(paragraph);
            paragraph.setText(listContent[i]);
            paragraph.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
            paragraph.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.black);
            paragraph.setBulletType(TextBulletType.PICTURE);
            paragraph.getBulletPicture().setEmbedImage(presentation.getImages().append(image));
        }

        //Save the document
        presentation.saveToFile("ImageBullets.pptx", FileFormat.PPTX_2013);
    }
}

Java: Create Numbered or Bulleted Lists in PowerPoint

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.

PDF is a versatile file format that can render text and graphics on its pages as well as serve as a storage container. People can attach files to PDFs and extract them later. Attaching related documents to a PDF can facilitate centralized management and transmission of documents.

Spire.PDF for Java allows you to attach files in two ways:

  • Document Level Attachment: A file attached to a PDF at the document level won't appear on a page, but can only be viewed in the "Attachments" panel of a PDF reader.
  • Annotation Attachment: A file will be added to a specific position of a page. Annotation attachments are shown as a paper clip icon on the page; reviewers can double-click the icon to open the file.

This article demonstrates how to add or remove these two types of attachments in a PDF document in Java using Spire.PDF for Java.

Install Spire.PDF for Java

First, you're required to add the Spire.Pdf.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.pdf</artifactId>
        <version>11.12.16</version>
    </dependency>
</dependencies>

Add an Attachment to PDF in Java

Adding an attachment to the "Attachments" panel can be easily done by using PdfDocument.getAttachments().add() method. The following are the detailed steps.

  • Create a PdfDocument object.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Create a PdfAttachment object based on an external file.
  • Add the attachment to PDF using PdfDocument.getAttachments().add() method.
  • Save the document to another PDF file using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.attachments.PdfAttachment;

public class AttachFilesToPdf {

    public static void main(String[] args) {

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

        //Load a sample PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pdf");

        //Create a PdfAttachment object based on an external file
        PdfAttachment attachment = new PdfAttachment("C:\\Users\\Administrator\\Desktop\\Data.xlsx");

        //Add the attachment to PDF
        doc.getAttachments().add(attachment);

        //Save to file
        doc.saveToFile("Attachment.pdf");
    }
}

Java: Add or Remove Attachments in PDF

Add an Annotation Attachment to PDF in Java

An annotation attachment can be found in the "Attachments" panel as well as on a specific page. Below are the steps to add an annotation attachment to PDF using Spire.PDF for Java.

  • Create a PdfDocument object.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Get a specific page to add annotation using PdfDocument.getPages().get() method.
  • Create a PdfAttachmentAnnotation object based on an external file.
  • Add the annotation attachment to the page using PdfPageBase.getAnnotationsWidget().add() method.
  • Save the document to another PDF file using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.annotations.*;
import com.spire.pdf.graphics.*;
import com.spire.pdf.PdfDocument;

import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class AnnotationAttachment {

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

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

        //Load a sample PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pdf");

        //Get a specific page
        PdfPageBase page = doc.getPages().get(0);

        //Draw a label on PDF
        String label = "Here is the report:";
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN, 13));
        double x = 35;
        double y = doc.getPages().get(0).getActualSize().getHeight() - 220;
        page.getCanvas().drawString(label, font, PdfBrushes.getRed(), x, y);

        //Attach a file as an annotation
        String filePath = "C:\\Users\\Administrator\\Desktop\\Report.pptx";
        byte[] data = toByteArray(filePath);
        Dimension2D size = font.measureString(label);
        Rectangle2D bound = new Rectangle2D.Float((float) (x + size.getWidth() + 5), (float) y, 10, 15);
        PdfAttachmentAnnotation annotation = new PdfAttachmentAnnotation(bound, filePath, data);
        annotation.setColor(new PdfRGBColor(new Color(0, 128, 128)));
        annotation.setFlags(EnumSet.of(PdfAnnotationFlags.Default));
        annotation.setIcon(PdfAttachmentIcon.Graph);
        annotation.setText("Click here to open the file");
        page.getAnnotationsWidget().add(annotation);

        //Save to file
        doc.saveToFile("Attachments.pdf");
    }
    //Convert file to byte array
    public static byte[] toByteArray(String filePath) throws IOException {

        File file = new File(filePath);
        long fileSize = file.length();
        if (fileSize > Integer.MAX_VALUE) {
            System.out.println("file too big...");
            return null;
        }
        FileInputStream fi = new FileInputStream(file);
        byte[] buffer = new byte[(int) fileSize];
        int offset = 0;
        int numRead = 0;
        while (offset < buffer.length
                && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
            offset += numRead;
        }

        if (offset != buffer.length) {
            throw new IOException("Could not completely read file "
                    + file.getName());
        }
        fi.close();
        return buffer;
    }
}

Java: Add or Remove Attachments in PDF

Remove Attachments from PDF in Java

The attachments of a PDF document can be accessed using PdfDocument.getAttachments() method, and can be removed by using removeAt() method or clear() method of the PdfAttachmentCollection object. The detailed steps are as follows.

  • Create a PdfDocument object.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Get the attachment collection from the document using PdfDocument.getAttachments() method.
  • Remove a specific attachment using PdfAttachmentCollection.removeAt() method. To remove all attachments at once, use PdfAttachmentCollection.clear() method.
  • Save the document to another PDF file using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.attachments.PdfAttachmentCollection;

public class RemoveAttachments {

    public static void main(String[] args) {

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

        //Load a PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Attachments.pdf");

        //Get attachment collection, not containing annotation attachments
        PdfAttachmentCollection attachments = doc.getAttachments();

        //Remove all attachments
        attachments.clear();

        //Remove a specific attachment
        //attachments.removeAt(0);

        //save to file
        doc.saveToFile("output/DeleteAttachments.pdf");
        doc.close();
    }
}

Remove Annotation Attachments from PDF in Java

Annotation is a page-based element. To get all annotations from a document, we must traverse through the pages and get the annotations from each page. Then determine if a certain annotation is an annotation attachment. Lastly, remove the annotation attachment from the annotation collection using remove() method.  The following are the detailed steps.

  • Create a PdfDocument object.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Loop through the pages in the document, and get the annotation collection from a specific page using PdfPageBase.getAnnotationsWidget() method.
  • Determine if an annotation is an instance of PdfAttachmentAnnotationWidget. If yes, remove the annotation attachment using PdfAnnotationCollection.remove() method.
  • Save the document to another PDF file using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.annotations.PdfAnnotation;
import com.spire.pdf.annotations.PdfAnnotationCollection;
import com.spire.pdf.annotations.PdfAttachmentAnnotationWidget;

public class RemoveAnnotationAttachments {

    public static void main(String[] args) {

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

        //Load a PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Attachments.pdf");

        //Loop through the pages
        for (int i = 0; i < doc.getPages().getCount(); i++) {

            //Get the annotation collection
            PdfAnnotationCollection annotationCollection = doc.getPages().get(i).getAnnotationsWidget();

            //Loop through the annotations
            for (Object annotation: annotationCollection) {

                //Determine if an annotation is an instance of PdfAttachmentAnnotationWidget
                if (annotation instanceof PdfAttachmentAnnotationWidget){

                    //Remove the attachment annotation
                    annotationCollection.remove((PdfAnnotation) annotation);
                }
            }
        }

        //save to file
        doc.saveToFile("output/DeleteAnnotationAttachments.pdf");
        doc.close();
    }
}

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.

page 161