Java (480)
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
This article demonstrates how to duplicate a page within a PDF document using Spire.PDF for Java.
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.graphics.PdfTemplate;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;
public class DuplicatePage {
public static void main(String[] args) {
//Load a sample PDF document
PdfDocument pdf = new PdfDocument("C:\\Users\\Administrator\\Desktop\\original.pdf");
//Get the first page
PdfPageBase page = pdf.getPages().get(0);
//Get the page size
Dimension2D size = page.getActualSize();
//Create a template based on the page
PdfTemplate template = page.createTemplate();
for (int i = 0; i < 10; i++) {
//Add a new page to the document
page = pdf.getPages().add(size, new PdfMargins(0));
//Draw template on the new page
page.getCanvas().drawTemplate(template, new Point2D.Float(0, 0));
}
//Save the file
pdf.saveToFile("output/DuplicatePage.pdf");
}
}

Published in
Document Operation
Tagged under