Java (481)
This article demonstrates how to split a PowerPoint document into multiple individual slides using Spire.Presentation for Java.
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
public class SplitPowerPoint {
public static void main(String[] args) throws Exception {
//Load the sample PowerPoint file
Presentation ppt = new Presentation();
ppt.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pptx");
//Loop through the slides
for (int i = 0; i < ppt.getSlides().getCount(); i++)
{
//Create an instance of Presentation class
Presentation newppt = new Presentation();
//Remove the default slide
newppt.getSlides().removeAt(0);
//Select a slide from the source file and append it to the new document
newppt.getSlides().append(ppt.getSlides().get(i));
//Save to file
newppt.saveToFile(String.format("output/result-%d.pptx",i), FileFormat.PPTX_2013);
}
}
}
Output:

Published in
Document Operation
Tagged under
Table of contents (TOC) makes the PDF documents more accessible and easier to navigate, especially for large files. This article demonstrates how to create table of contents (TOC) for a PDF document using Spire.PDF for Java.
import com.spire.pdf.*;
import com.spire.pdf.actions.PdfGoToAction;
import com.spire.pdf.annotations.*;
import com.spire.pdf.general.PdfDestination;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;
public class TableOfContent {
public static void main(String[] args) throws Exception
{
//load PDF file
PdfDocument doc = new PdfDocument("sample.pdf");
int pageCount = doc.getPages().getCount();
//Insert a new page as the first page to draw table of content
PdfPageBase tocPage = doc.getPages().insert(0);
//set title
String title = "Table Of Contents";
PdfTrueTypeFont titleFont = new PdfTrueTypeFont(new Font("Arial", Font.BOLD,20));
PdfStringFormat centerAlignment = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
Point2D location = new Point2D.Float((float) tocPage.getCanvas().getClientSize().getWidth() / 2, (float) titleFont.measureString(title).getHeight());
tocPage.getCanvas().drawString(title, titleFont, PdfBrushes.getCornflowerBlue(), location, centerAlignment);
//draw TOC text
PdfTrueTypeFont titlesFont = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN,14));
String[] titles = new String[pageCount];
for (int i = 0; i < titles.length; i++) {
titles[i] = String.format("page%1$s", i + 1);
}
float y = (float)titleFont.measureString(title).getHeight() + 10;
float x = 0;
for (int i = 1; i <= pageCount; i++) {
String text = titles[i - 1];
Dimension2D titleSize = titlesFont.measureString(text);
PdfPageBase navigatedPage = doc.getPages().get(i);
String pageNumText = (String.valueOf(i+1));
Dimension2D pageNumTextSize = titlesFont.measureString(pageNumText);
tocPage.getCanvas().drawString(text, titlesFont, PdfBrushes.getCadetBlue(), 0, y);
float dotLocation = (float)titleSize.getWidth() + 2 + x;
float pageNumlocation = (float)(tocPage.getCanvas().getClientSize().getWidth() - pageNumTextSize.getWidth());
for (float j = dotLocation; j < pageNumlocation; j++) {
if (dotLocation >= pageNumlocation) {
break;
}
tocPage.getCanvas().drawString(".", titlesFont, PdfBrushes.getGray(), dotLocation, y);
dotLocation += 3;
}
tocPage.getCanvas().drawString(pageNumText, titlesFont, PdfBrushes.getCadetBlue(), pageNumlocation, y);
//add TOC action
Rectangle2D titleBounds = new Rectangle2D.Float(0,y,(float)tocPage.getCanvas().getClientSize().getWidth(),(float)titleSize.getHeight());
PdfDestination Dest = new PdfDestination(navigatedPage, new Point2D.Float(-doc.getPageSettings().getMargins().getTop(), -doc.getPageSettings().getMargins().getLeft()));
PdfActionAnnotation action = new PdfActionAnnotation(titleBounds, new PdfGoToAction(Dest));
action.setBorder(new PdfAnnotationBorder(0));
((PdfNewPage) ((tocPage instanceof PdfNewPage) ? tocPage : null)).getAnnotations().add(action);
y += titleSize.getHeight() + 10;
}
//save the resultant file
doc.saveToFile("addTableOfContent.pdf");
doc.close();
}
}
Output:

Published in
Document Operation
Tagged under
With Spire.Presentation for Java, we can format a table by applying preset table styles. This article will demonstrate how to apply predefined styles to a table on presentation slides.
Firstly, view the table on the PowerPoint document:

import com.spire.presentation.ITable;
import com.spire.presentation.Presentation;
import com.spire.presentation.FileFormat;
import com.spire.presentation.TableStylePreset;
public class SetTableStyle {
public static void main(String[] args) throws Exception {
//Create a PPT document and load file
Presentation presentation = new Presentation();
presentation.loadFromFile("Sample.pptx");
ITable table = null;
for (Object shape : presentation.getSlides().get(0).getShapes()) {
if (shape instanceof ITable) {
table = (ITable) shape;
//Set the table style from TableStylePreset and apply it to selected table
table.setStylePreset(TableStylePreset.MEDIUM_STYLE_1_ACCENT_2);
}
}
//Save the file
presentation.saveToFile("SetTableStyle.pptx", FileFormat.PPTX_2010);
}
}
Effective screenshot after updating the table style:

Published in
Table
Tagged under