Delete Bookmarks in PDF in Java

2020-07-27 06:52:14 Written by Koohji

Spire.PDF for Java supports deleting a specific child bookmark, a parent bookmark along with its child bookmark(s) or delete all the bookmarks from a PDF file. In this article, you will learn how to delete PDF bookmarks using Spire.PDF for Java.

The input PDF file:

Delete Bookmarks in PDF in Java

import com.spire.pdf.PdfDocument;

public class DeleteBookmarks {
    public static void main(String[] args) {
        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();
        //Load the PDF file
        pdf.loadFromFile("AddBookmarks.pdf");

        //Delete the first child bookmark
        pdf.getBookmarks().get(0).removeAt(0);

        //Delete the first bookmark along with its child bookmark
        //pdf.getBookmarks().removeAt(0);

        //Delete all the bookmarks
        //pdf.getBookmarks().clear();

        //Save the result file
        pdf.saveToFile("DeleteBookmarks.pdf");
    }
}

The output PDF file after deleting the first child bookmark:

Delete Bookmarks in PDF in Java

How to Autofit a Word Table in Java

2020-07-24 05:43:57 Written by Koohji

This article demonstrates how to auto fit a Word table to content or to window, as well as how to fix the cloumn widths, by using Spire.Doc for Java.

Autofit to content

import com.spire.doc.*;

public class AutofitToContent {
    public static void main(String[] args) {

        //Create a Document object
        Document document = new Document();

        //Add a section
        Section section = document.addSection();

        //Add a table
        Table table = section.addTable();
        table.resetCells(3, 2);

        //Add content to the cells
        table.get(0,0).addParagraph().appendText("Product Code");
        table.get(0,1).addParagraph().appendText("Description");
        table.get(1,0).addParagraph().appendText("T1052");
        table.get(1,1).addParagraph().appendText("AdvoCareSlim Tropical Swirl");
        table.get(2,0).addParagraph().appendText("T1062");
        table.get(2,1).addParagraph().appendText("AdvoCareSlim Caffeine Free Strawberry Kiwi");

        //Autofit column widths to contents
        table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents);

        //Save the document
        document.saveToFile("AutofitToContent.docx", FileFormat.Docx);
    }
}

How to Autofit a Word Table in Java

Autofit to window

import com.spire.doc.*;

public class AutofitToWindow {

    public static void main(String[] args) {

        //Create a Document object
        Document document = new Document();

        //Add a section
        Section section = document.addSection();

        //Add a table
        Table table = section.addTable();
        table.resetCells(3, 2);

        //Add content to the cells
        table.get(0,0).addParagraph().appendText("Product Code");
        table.get(0,1).addParagraph().appendText("Description");
        table.get(1,0).addParagraph().appendText("T1052");
        table.get(1,1).addParagraph().appendText("AdvoCareSlim Tropical Swirl");
        table.get(2,0).addParagraph().appendText("T1062");
        table.get(2,1).addParagraph().appendText("AdvoCareSlim Caffeine Free Strawberry Kiwi");

        //Autofit column widths to window
        table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Window);

        //Save the document
        document.saveToFile("AutofitToWindow.docx", FileFormat.Docx);
    }
}

How to Autofit a Word Table in Java

Fix column width

import com.spire.doc.*;

public class FixColumnWidths {
    public static void main(String[] args) {

        //Create a Document object
        Document document = new Document();

        //Add a section
        Section section = document.addSection();

        //Add a table
        Table table = section.addTable();
        table.resetCells(3, 2);

        //Add content to the cells
        table.get(0, 0).addParagraph().appendText("Product Code");
        table.get(0, 1).addParagraph().appendText("Description");
        table.get(1, 0).addParagraph().appendText("T1052");
        table.get(1, 1).addParagraph().appendText("AdvoCareSlim Tropical Swirl");
        table.get(2, 0).addParagraph().appendText("T1062");
        table.get(2, 1).addParagraph().appendText("AdvoCareSlim Caffeine Free Strawberry Kiwi");

        //Set the column widths
        for (int i = 0; i < table.getRows().getCount(); i++) {

            table.get(i,0).setCellWidth(80f,CellWidthType.Point);
            table.get(i,1).setCellWidth(160f,CellWidthType.Point);
        }

        //Fix the column widths so that the column width does not increases when the content exceeds the width
        table.autoFit(AutoFitBehaviorType.Fixed_Column_Widths);

        //Save the document
        document.saveToFile("FixColumnWidths.docx", FileFormat.Docx);
    }
}

How to Autofit a Word Table in Java

This article demonstrates how to set the transparency of image on a PDF file using Spire.PDF for Java.

import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import java.awt.geom.*;
import java.awt.*;

public class ImageTransparency {
    public static void main(String[] args) {

        //Create a PDFDocument
        PdfDocument doc = new PdfDocument();
        //Add a section
        PdfSection section = doc.getSections().add();

        // Load image and set its width and height
        PdfImage image = PdfImage.fromFile("logo.png");
        double imageWidth = image.getPhysicalDimension().getWidth() / 3;
        double imageHeight = image.getPhysicalDimension().getHeight() / 3;

        //Add a page
        PdfPageBase page = section.getPages().add();
        float pageWidth = (float) page.getCanvas().getClientSize().getWidth();
        float y = 10;

        //Draw Title and set the font and format
        y = y + 5;
        PdfBrush brush = new PdfSolidBrush(new PdfRGBColor(new Color(255,69,0)));
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.BOLD,12));
        PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center);
        String text = String.format(" Set image Transparency ");
        page.getCanvas().drawString(text, font, brush, pageWidth / 2, y, format);
        Dimension2D size = font.measureString(text, format);
        y = y + (float) size.getHeight() + 6;

        //Set image transparency
        page.getCanvas().setTransparency(0.2f, 0.2f, PdfBlendMode.Normal);

        //Add image to the page
        page.getCanvas().drawImage(image, 0, y, imageWidth, imageHeight);
        page.getCanvas().save();

        // Save pdf file.
        doc.saveToFile("output/Transparency.pdf");
        // Close pdf file
        doc.close();
    }
}

Effective screenshot after adding the image to PDF and set the image transparency:

Set image transparency on PDF file in Java

page 42

Coupon Code Copied!

Christmas Sale

Celebrate the season with exclusive savings

Save 10% Sitewide

Use Code:

View Campaign Details