0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JAVA PDFブックマークの追加、修正、削除

Last updated at Posted at 2019-06-06

より長いPDFファイルを読む場合は、前回の場合に迅速に前回の位置に達することができるように、ブックマークを挿入します。また、既存のしおりについても必要に応じて変更や削除などの操作が可能である。本文章はJavaプログラミングを通してPDFブックマークの添付、修正及び削除方法を展示します。

ツール使用:

  • Free Spire.Pdf for Java 2.4.4 (無費版)
  • IntelliJ IDEA

Jarパッケージ導入:
:clap: 方式1: まず、公式サイトからFree Spire.PDF for Javaを取得した後に圧縮を開放する。以下は、IDEAにおいてProject Structureインタフェースを簡単に開く方式。以下の図のように:
image.png
その後、以下の手順で操作を行います。①「Modules」-「Dependencies」を選択し、外付けjarパッケージを追加します。②「Attach File or Directores」画面に入ってjarファイルのパスを選択し、「OK」をクリックします。次の図のように:
image.png
image.png
:clap: 方式二: Mavenを使ってjarパッケージを配置する。導入方法を参考にすることができます。
テストドキュメント:
image.png

JAVAコード参照

1.ブックマークをつける追加

import com.spire.pdf.*;
import com.spire.pdf.actions.PdfGoToAction;
import com.spire.pdf.bookmarks.PdfBookmark;
import com.spire.pdf.bookmarks.PdfTextStyle;
import com.spire.pdf.general.PdfDestination;
import com.spire.pdf.graphics.PdfRGBColor;
import java.awt.*;
import java.awt.geom.Point2D;

public class bookmark {
    public static void main(String[] args) throws Exception{
             //Create a pdf document. 
            PdfDocument doc = new PdfDocument();
            //Load PDF file from the disk
            doc.loadFromFile("data/SampleEn.pdf");
            //Get the third page 
            PdfPageBase page = doc.getPages().get(2);
            //Add the bookmark 
            PdfBookmark bookmark = doc.getBookmarks().add("3ページ目");
            //Set the position-At the start of the page 
            PdfDestination bookmarkLocation = new PdfDestination(page, new Point2D.Float(0 , 0));
            bookmark.setAction(new PdfGoToAction(bookmarkLocation)); 
            //Set the style of bookmark
            bookmark.setColor(new PdfRGBColor(Color.BLUE));
            bookmark.setDisplayStyle(PdfTextStyle.Bold);
            PdfPageBase page1 = doc.getPages().get(3);
            PdfBookmark bookmark1 = doc.getBookmarks().add("4ページ目");
            PdfDestination bookmarkLocation1 = new PdfDestination(page1, new Point2D.Float(0 , 0));
            bookmark1.setAction(new PdfGoToAction(bookmarkLocation1));
            bookmark1.setColor(new PdfRGBColor(Color.RED));
            bookmark1.setDisplayStyle(PdfTextStyle.Bold);
            //Save pdf file. 
        doc.saveToFile("output/bookmarks.pdf");
        doc.close();
        }
    }

ブックマーク追加効果:
image.png

2.栞を书き直す

import com.spire.pdf.PdfDocument;
import com.spire.pdf.bookmarks.*;
import com.spire.pdf.graphics.PdfRGBColor;
import java.awt.*;

public class updateBookmark {
    public static void main(String[] args) {
        //Create the PDF
        PdfDocument doc = new PdfDocument();
        //Load PDF file from the disk
        doc.loadFromFile("output/bookmarks.pdf");
        //Get the first bookmark 
        PdfBookmark bookmark = doc.getBookmarks().get(0);
        //Change the title of the bookmark 
        bookmark.setTitle("変更されたブックマーク");
        //Set the color of the bookmark 
        bookmark.setColor(new PdfRGBColor(Color.black));
        //Set the outline text style of the bookmark 
        bookmark.setDisplayStyle(PdfTextStyle.Bold);
        //Save the file 
        doc.saveToFile("output/updateBookmark.pdf");
        doc.close();
    }
}

ブックマークの効果を変更する:
image.png

3.しおりを削除する

import com.spire.pdf.PdfDocument;

public class deleteBookmark {
    public static void main(String[] args) {
        //Create the PDF
        PdfDocument doc = new PdfDocument();
        //Load PDF file from the disk
        doc.loadFromFile("output/bookmarks.pdf");
        //removeAt() to delete the bookmark
        doc.getBookmarks().removeAt(0);
        //Save the file
        doc.saveToFile("output/deleteBookmark.pdf");
        doc.close();
    }
}

ブックマークの削除効果:
image.png
(全文が終わる)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?