We have already demonstrated how to set PDF Document Properties in Java. This article we will show you how to set custom properties for PDF files in Java.

import com.spire.pdf.*;

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

        String inputPath = "Sample.pdf";
        PdfDocument doc = new PdfDocument(inputPath);
        doc.loadFromFile(inputPath);

        //Set the custom properties
        doc.getDocumentInformation().setCustomerDefined("Number", "123");
        doc.getDocumentInformation().setCustomerDefined("Name", "Daisy");
        doc.getDocumentInformation().setCustomerDefined("Company", "e-iceblue");
       
        //Save the document to file
        doc.saveToFile("Output/result.pdf");
        doc.close();

    }
}

Effective screenshot after adding custom properties to PDF document:

Set custom properties for PDF files in Java

This article demonstrates how to create simple bulleted and numbered lists in a Word document using Spire.Doc for Java.

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

public class Bullets {
    public static void main(String[] args){
        //load the Word document
        Document document = new Document();
        //add a section
        Section section = document.addSection();

        //add 8 paragraphs
        Paragraph paragraph1 = section.addParagraph();
        paragraph1.appendText("Bulleted List");
        paragraph1.applyStyle(BuiltinStyle.Heading_1);
        Paragraph paragraph2 = section.addParagraph();
        paragraph2.appendText("Chapter 1");
        Paragraph paragraph3 = section.addParagraph();
        paragraph3.appendText("Chapter 2");
        Paragraph paragraph4 = section.addParagraph();
        paragraph4.appendText("Chapter 3");
        Paragraph paragraph5 = section.addParagraph();
        paragraph5.appendText("Numbered List");
        paragraph5.applyStyle(BuiltinStyle.Heading_1);
        Paragraph paragraph6 = section.addParagraph();
        paragraph6.appendText("Chapter 1");
        Paragraph paragraph7 = section.addParagraph();
        paragraph7.appendText("Chapter 2");
        Paragraph paragraph8 = section.addParagraph();
        paragraph8.appendText("Chapter 3");

        //create bulleted list for the 2-4 paragraphs
        for(int i = 1; i < 4; i++){
            Paragraph para = section.getParagraphs().get(i);
            para.getListFormat().applyBulletStyle();
            para.getListFormat().getCurrentListLevel().setNumberPosition(-10);
        }

        //create numbered list for the 6-8 paragraphs
        for(int i = 5; i < 8; i++){
            Paragraph para = section.getParagraphs().get(i);
            para.getListFormat().applyNumberedStyle();
            para.getListFormat().getCurrentListLevel().setNumberPosition(-10);
        }

        //save the document
        document.saveToFile("CreateLists.docx", FileFormat.Docx_2013);
    }
}

Output:

Create Bulleted and Numbered Lists in Word in Java

Thursday, 18 July 2019 09:05

Add Digital Signature to PDF in Java

This article demonstrates how to add a digital signature to a PDF document using Spire.PDF for Java.

import com.spire.pdf.graphics.PdfFont;
import com.spire.pdf.graphics.PdfFontFamily;
import com.spire.pdf.graphics.PdfFontStyle;
import com.spire.pdf.graphics.PdfImage;
import com.spire.pdf.security.GraphicMode;
import com.spire.pdf.security.PdfCertificate;
import com.spire.pdf.security.PdfCertificationFlags;
import com.spire.pdf.security.PdfSignature;

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

public class AddDigitalSignature {

    public static void main(String[] args) {

        //Load a pdf document
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Introduction.pdf");

        //Load the certificate
        PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\gary.pfx", "e-iceblue");

        //Create a PdfSignature object and specify its position and size
        PdfSignature signature = new PdfSignature(doc, doc.getPages().get(0), cert, "MySignature");
        Rectangle2D rect = new Rectangle2D.Float();
        rect.setFrame(new Point2D.Float((float) doc.getPages().get(0).getActualSize().getWidth() - 320, (float) doc.getPages().get(0).getActualSize().getHeight() - 140), new Dimension(270, 100));
        signature.setBounds(rect);

        //Set the graphics mode
        signature.setGraphicMode(GraphicMode.Sign_Image_And_Sign_Detail);

        //Set the signature content
        signature.setNameLabel("Signer:");
        signature.setName("Gary");
        signature.setContactInfoLabel("ContactInfo:");
        signature.setContactInfo("02881705109");
        signature.setDateLabel("Date:");
        signature.setDate(new java.util.Date());
        signature.setLocationInfoLabel("Location:");
        signature.setLocationInfo("Chengdu");
        signature.setReasonLabel("Reason: ");
        signature.setReason("The certificate of this document");
        signature.setDistinguishedNameLabel("DN: ");
        signature.setDistinguishedName(signature.getCertificate().get_IssuerName().getName());
        signature.setSignImageSource(PdfImage.fromFile("C:\\Users\\Administrator\\Desktop\\logo-2.png"));

        //Set the signature font
        signature.setSignDetailsFont(new PdfFont(PdfFontFamily.Helvetica, 10f, PdfFontStyle.Regular));

        //Set the document permission
        signature.setDocumentPermissions(PdfCertificationFlags.Forbid_Changes);
        signature.setCertificated(true);

        //Save to file
        doc.saveToFile("output/AddSignature.pdf");
        doc.close();
    }
}

Add Digital Signature to PDF in Java

In the article of convert PDF to SVG by Spire.PDF, each page on the PDF file has been saved as a single SVG file. For example, if the PDF contains 10 pages, we will get 10 SVG files separately. From version 2.7.6, Spire.PDF for Java supports to convert a multipage PDF to one single SVG file in Java.

import com.spire.pdf.*;


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

        String inputPath = "Sample.pdf";

        PdfDocument document = new PdfDocument();
        document.loadFromFile(inputPath);

        document.getConvertOptions().setOutputToOneSvg(true);

        document.saveToFile("output.svg", FileFormat.SVG);
        document.close();
    }
}

Effective screenshot of the resulted one SVG file:

Java convert a multipage PDF to one SVG

We can set left, centered, right and justified alignment for paragraphs in Word. This article will show you how to set the alignment for paragraphs in a Word document using Spire.Doc for Java.

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

public class ParagraphAlighment {
    public static void main(String[] args){
        //Instantiate a Document object
        Document document = new Document();
        
        //Add a section
        Section section = document.addSection();

        //Add a paragraph and make it left aligned
        Paragraph paragraph = section.addParagraph();
        paragraph.appendText("This paragraph is left-aligned");
        paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);

        //Add a paragraph and make it centered
        paragraph = section.addParagraph();
        paragraph.appendText("This paragraph is centered");
        paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

        //Add a paragraph and make it right alighed
        paragraph = section.addParagraph();
        paragraph.appendText("This paragraph is right-aligned");
        paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);

        //Add a paragraph and make it justified
        paragraph = section.addParagraph();
        paragraph.appendText("This paragraph is justified");
        paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Justify);

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

Output:

Set Alignment for Paragraphs in Word in Java

Wednesday, 10 July 2019 03:44

Convert Word to Images in Java

This following code snippets demonstrate how to convert Word documents to image files using Spire.Doc for Java.

Convert a particular page to an image

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

//load a Word file
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");

//save the first page to a BufferedImage
BufferedImage image= doc.saveToImages(0, ImageType.Bitmap);

//write the image data to a .png file
File file= new File("output/ToPNG.png");
ImageIO.write(image, "PNG", file);

Convert the whole document to multiple images

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

//load a Word file
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");

//loop through the pages
for (int i = 0; i < doc.getPageCount(); i++) {

    //save the specific page to a BufferedImage
    BufferedImage image= doc.saveToImages(i, ImageType.Bitmap);

    //write the image data to a .png file
    File file = new File( "output/" + String.format(("Img-%d.png"), i));
    ImageIO.write(image, "PNG", file);
}

Spire.Presentation for Java supports to save the whole presentation slides with table, chart and shape to image. And it also supports to save the single element in the presentation slides, such as chart, table and shape into image. This article will show you how to save shape and chart on Presentation Slide as image in Java.

import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class ShapeAsImage {
    public static void main(String[] args) throws Exception {
        String inputFile = "Sample.pptx";
        String outputFile = "output/";

        //Create a Presentation instance and load sample file
        Presentation presentation = new Presentation();
        presentation.loadFromFile(inputFile);

        //Traverse every slides and every shapes on the slides
        int k = 0;
        for (int i = 0; i < presentation.getSlides().getCount(); i++) {
            ISlide slide = presentation.getSlides().get(i);
            for (int j = 0; j < slide.getShapes().getCount(); j++) {
                String fileName = outputFile + String.format("shapeToImage-%1$s.png", k);
                //Save every single shape as image
                BufferedImage image = slide.getShapes().saveAsImage(j);
                ImageIO.write(image, "PNG", new File(fileName));
                k++;
            }
        }
    }
}

Save Chart, Table and Shape on Presentation slides as Image in Java

Save Chart, Table and Shape on Presentation slides as Image in Java

Save Chart, Table and Shape on Presentation slides as Image in Java

Animation can make a PowerPoint document more dynamic. In this article, we'll introduce how to add animations to shapes in a PowerPoint document using Spire.Presentation for Java.

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

import java.awt.*;

import java.awt.geom.Rectangle2D;

public class SetAnimation {
    public static void main(String[] args) throws Exception {
        //create a PowrePoint document 
        Presentation ppt = new Presentation();
        //add a slide
        ISlide slide = ppt.getSlides().get(0);

        //Add a shape to slide
        IAutoShape shape = slide.getShapes().appendShape(ShapeType.CUBE, new Rectangle2D.Double(50, 150, 150, 150));
        shape.getFill().setFillType(FillFormatType.SOLID);
        shape.getFill().getSolidColor().setColor(Color.orange);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //Add animation to the shape
        slide.getTimeline().getMainSequence().addEffect(shape, AnimationEffectType.FADED_SWIVEL);

        //save the document
        ppt.saveToFile("AddAnimationToShape.pptx", FileFormat.PPTX_2013);
    }
}

Add Animations to Shapes in PowerPoint in Java

Embedding and applying private fonts in Word or PDF documents can make your documents unique. This article demonstrates how to embed fonts in a Word document as well as a converted PDF document, by using Spire.Doc for Java.

Embed Font in Word Document

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.PrivateFontPath;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;

public class EmbedPrivateFontInWord {

    public static void main(String[] args) {

        //create a Word document
        Document document = new Document();
        
        //add a paragraph
        Paragraph paragraph = document.addSection().addParagraph();
        
        //embed a private font in Word document
        document.setEmbedFontsInFile(true);
        PrivateFontPath fontPath = new PrivateFontPath();
        fontPath.setFontName("Otto");
        fontPath.setFontPath("C:\\Users\\Administrator\\Desktop\\Otto.ttf");
        document.getPrivateFontList().add(fontPath);

        //add text to the paragraph and apply the embedded font 
        TextRange tr = paragraph.appendText("Spire.Doc for Java is a professional Java Word API that enables "+
                        "Java applications to create, convert, manipulate and print Word documents without " +
                        "using Microsoft Office.");
        tr.getCharacterFormat().setFontName("Otto");
        tr.getCharacterFormat().setFontSize(26f);

        //save to file
        document.saveToFile("output/EmbedFont.docx", FileFormat.Docx);
    }
}

Embed Private Fonts When Saving Word to DOCX and PDF in Java

Embed Font in Converted PDF Document

import com.spire.doc.Document;
import com.spire.doc.PrivateFontPath;
import com.spire.doc.ToPdfParameterList;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;

import java.util.*;

public class EmbedPrivateFontInConvertedPDF {

    public static void main(String[] args) {

        //create a Word document
        Document document = new Document();

        //add a paragraph
        Paragraph paragraph = document.addSection().addParagraph();

        //initialize PrivateFontPath object
        PrivateFontPath fontPath = new PrivateFontPath("Otto","C:\\Users\\Administrator\\Desktop\\Otto.ttf");

        //add text to paragraph and apply the private font
        TextRange tr = paragraph.appendText("Spire.Doc for Java is a professional Java Word API that enables "+
                "Java applications to create, convert, manipulate and print Word documents without " +
                "using Microsoft Office.");
        tr.getCharacterFormat().setFontName("Otto");
        tr.getCharacterFormat().setFontSize(26f);

        //create a ToPdfParameterList object
        ToPdfParameterList toPdfParameterList = new ToPdfParameterList();

        //set private fonts as a parameter for converting Word to PDF
        List pathList = new LinkedList<>();
        pathList.add(fontPath);
        toPdfParameterList.setPrivateFontPaths(pathList);

        //save to PDF file
        document.saveToFile("output/EmbedFont.pdf",toPdfParameterList);
    }
}

Embed Private Fonts When Saving Word to DOCX and PDF in Java

Wednesday, 03 July 2019 09:23

Compress PDF document in Java

This article will demonstrate how to use Spire.PDF for Java to compress PDF document by compressing the PDF contents and compressing the images in the PDF document.

Compressing content

import com.spire.pdf.*;

public class CompressPDF {

    public static void main(String[] args) {

        String inputFile = "Sample.pdf";
        String outputFile = "output/CompressPDFcontent.pdf";

        PdfDocument document = new PdfDocument();
        document.loadFromFile(inputFile);

        document.getFileInfo().setIncrementalUpdate(false);

        document.setCompressionLevel(PdfCompressionLevel.Best);

        document.saveToFile(outputFile, FileFormat.PDF);
        document.close();
        }
    }

Effective screenshot after compressing the PDF content:

Compress PDF document in Java

Compressing image

import com.spire.pdf.*;
import com.spire.pdf.exporting.PdfImageInfo;
import com.spire.pdf.graphics.PdfBitmap;

public class CompressPDF {

    public static void main(String[] args) {

        String inputFile = "Sample.pdf";
        String outputFile = "output/CompressPDFImage.pdf";

        PdfDocument document = new PdfDocument();
        document.loadFromFile(inputFile);

        document.getFileInfo().setIncrementalUpdate(false);

        for (int i = 0; i < document.getPages().getCount(); i++) {

            PdfPageBase page = document.getPages().get(i);
            PdfImageInfo[] images = page.getImagesInfo();
            if (images != null && images.length > 0)
                for (int j = 0; j < images.length; j++) {
                    PdfImageInfo image = images[j];
                    PdfBitmap bp = new PdfBitmap(image.getImage());
                    bp.setQuality(20);
                    page.replaceImage(j, bp);

                }
        }
        document.saveToFile(outputFile, FileFormat.PDF);
        document.close();
    }
}

Effective screenshot after compressing the PDF image:

Compress PDF document in Java

Page 6 of 113