Create a Multi-Level List in PDF in Java

2021-05-17 02:35:28 Written by Koohji

This article shows you how to create a multi-level list in a PDF document using Spire.PDF for Java.

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfNumberStyle;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.PdfPageSize;
import com.spire.pdf.graphics.*;
import com.spire.pdf.lists.PdfListItem;
import com.spire.pdf.lists.PdfOrderedMarker;
import com.spire.pdf.lists.PdfSortedList;

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

public class CreateMultiLevelList {

    public static void main(String[] args) {

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

        //Set the margin
        PdfMargins margin = new PdfMargins(60, 60, 40, 40);

        //Create one page
        PdfPageBase page = doc.getPages().add(PdfPageSize.A4, margin);

        //Specify the initial coordinate
        float x = 0;
        float y = 15;

        //Create two brushed
        PdfBrush blackBrush = PdfBrushes.getBlack();
        PdfBrush purpleBrush = PdfBrushes.getPurple();

        //Create two fonts
        PdfTrueTypeFont titleFont = new PdfTrueTypeFont(new java.awt.Font("Times New Roman", Font.BOLD, 12));
        PdfTrueTypeFont listFont = new PdfTrueTypeFont(new java.awt.Font("Calibri Light", Font.PLAIN, 12));

        //Draw title
        String title = "XHTML Tutorials/FAQs:";
        page.getCanvas().drawString(title, titleFont, blackBrush, x, y);
        y = y + (float) titleFont.measureString(title).getHeight();
        y = y + 5;

        //Create two ordered makers, which are used to define the number style of sorted list
        PdfOrderedMarker marker1 = new PdfOrderedMarker(PdfNumberStyle.Upper_Roman, listFont);
        PdfOrderedMarker marker2 = new PdfOrderedMarker(PdfNumberStyle.Numeric, listFont);

        //Create a parent list
        String parentListContent = "Introduction To XHTML 1.0\n"
                + "Introduction To Tag and Attribute Syntax";
        PdfSortedList parentList = new PdfSortedList(parentListContent);
        parentList.setFont(listFont);
        parentList.setIndent(8);
        parentList.setBrush(purpleBrush);
        parentList.setMarker(marker1);

        //Create a sub list - "subList_1"
        String subListContent_1 = "What Is XHTML?\n"
                + "What Does an XHMTL Document Look Like?\n"
                + "What Is the Relation between XHTML and HTML?\n"
                + "What Is the Relation between XHTML and XML?";
        PdfSortedList subList_1 = new PdfSortedList(subListContent_1);
        subList_1.setIndent(16);
        subList_1.setFont(listFont);
        subList_1.setBrush(purpleBrush);
        subList_1.setMarker(marker2);

        //Create another sub list -"subList_2"
        String subListContent_2 = "What Is an XHTML Element?\n"
                + "How To Enter Comments into XHTML Documents?\n"
                + "How To Write the Opening Tag of an XHTML Element?";
        PdfSortedList subList_2 = new PdfSortedList(subListContent_2);
        subList_2.setIndent(16);
        subList_2.setFont(listFont);
        subList_2.setBrush(purpleBrush);
        subList_2.setMarker(marker2);

        //Set subList_1 as sub list of the first item of parent list
        PdfListItem item_1 = parentList.getItems().get(0);
        item_1.setSubList(subList_1);

        //Set subList_2 as sub list of the second item of parent list
        PdfListItem item_2 = parentList.getItems().get(1);
        item_2.setSubList(subList_2);

        //Draw parent list
        PdfTextLayout textLayout = new PdfTextLayout();
        textLayout.setBreak(PdfLayoutBreakType.Fit_Page);
        textLayout.setLayout(PdfLayoutType.Paginate);
        parentList.draw(page,new Point2D.Float(x,y),textLayout);

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

Create a Multi-Level List in PDF in Java

This article will show you how to use Spire.Doc for Java to set the character spacing and paragraph spacing on Word.

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

import java.awt.*;
import java.io.*;

public class setSpacing {
    public static void main(String[] args)throws IOException {
        //Load the sample document
        Document document= new Document("Sample1.docx");

        //Add a new paragraph and append the text
        Paragraph para = new Paragraph(document);
        TextRange textRange1 = para.appendText("Newly added paragraph and set the paragraph spacing and character spacing");
        textRange1.getCharacterFormat().setTextColor(Color.blue);
        textRange1.getCharacterFormat().setFontSize(14);

        //Set the spacing before and after paragraph
        para.getFormat().setBeforeAutoSpacing(false);
        para.getFormat().setBeforeSpacing(10);
        para.getFormat().setAfterAutoSpacing(false);
        para.getFormat().setAfterSpacing(10);

        //Set the character spacing
        for (DocumentObject object :(Iterable<DocumentObject>)para.getChildObjects())
        {
            TextRange textRange= (TextRange) object;
            textRange.getCharacterFormat().setCharacterSpacing(3f);
        }

        //Insert the paragraph
        document.getSections().get(0).getParagraphs().insert(2, para);

        //Save the document to file
        document.saveToFile("Result.docx", FileFormat.Docx);

    }
}

Output:

Java set the character spacing and paragraph spacing on Word document

Add image comment to Excel in Java

2021-05-08 03:26:04 Written by Koohji

We have demonstrated how to add and read text comments in Excel in Java applications. This article will show you how to insert image comment to Excel with Spire.XLS for Java.

import com.spire.xls.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

public class Test {
    public static void main(String[] args)throws IOException {
        //Load the sample Excel file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample.xlsx");
        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //set the font
        ExcelFont font = workbook.createFont();
        font.setFontName("Arial");
        font.setSize(11);
        font.setKnownColor(ExcelColors.Orange);

        CellRange range = sheet.getCellRange("D1");
        //Add the commet
        ExcelComment comment = range.addComment();
        //Load the image
        BufferedImage bufferedImage = ImageIO.read(new File("Logo.jpg"));
        //Use the image to fill the comment
        comment.getFill().customPicture(bufferedImage, "Logo.jpg");

        //Set the height and width for the comment
        comment.setHeight(bufferedImage.getHeight());
        comment.setWidth(bufferedImage.getWidth());
        //Show the comment
        comment.setVisible(true);

        //Save the document to file
        workbook.saveToFile("output/setimageComment.xlsx", ExcelVersion.Version2013);

    }
}

Output:

Add image comment to Excel in Java

page 32