Untitled

Class 1
mail@pastecode.io avatar
unknown
java
a month ago
6.3 kB
0
Indexable
Never
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class PDFHandlerApp {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(PDFHandlerApp::createAndShowGUI);
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("PDF Handler");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        // Control Panel
        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new GridLayout(3, 2));

        // File Chooser Button
        JButton createButton = new JButton("Create PDF");
        JButton readButton = new JButton("Read PDF");
        JButton mergeButton = new JButton("Merge PDFs");
        JButton splitButton = new JButton("Split PDF");
        JButton annotateButton = new JButton("Annotate PDF");

        controlPanel.add(createButton);
        controlPanel.add(readButton);
        controlPanel.add(mergeButton);
        controlPanel.add(splitButton);
        controlPanel.add(annotateButton);

        frame.add(controlPanel, BorderLayout.NORTH);

        createButton.addActionListener(e -> createPDF());
        readButton.addActionListener(e -> readPDF());
        mergeButton.addActionListener(e -> mergePDFs());
        splitButton.addActionListener(e -> splitPDF());
        annotateButton.addActionListener(e -> annotatePDF());

        frame.pack();
        frame.setVisible(true);
    }

    private static void createPDF() {
        PDDocument document = new PDDocument();
        PDPage page = new PDPage(PDRectangle.A4);
        document.addPage(page);

        try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
            contentStream.beginText();
            contentStream.setFont(PDType1Font.HELVETICA, 12);
            contentStream.newLineAtOffset(100, 700);
            contentStream.showText("Hello, PDFBox!");
            contentStream.endText();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            document.save("CreatedPDF.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                document.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void readPDF() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileFilter(new FileNameExtensionFilter("PDF Files", "pdf"));

        int returnValue = fileChooser.showOpenDialog(null);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            try (PDDocument document = PDDocument.load(file)) {
                document.getPages().forEach(page -> System.out.println("Page: " + page));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void mergePDFs() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setMultiSelectionEnabled(true);
        fileChooser.setFileFilter(new FileNameExtensionFilter("PDF Files", "pdf"));

        int returnValue = fileChooser.showOpenDialog(null);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File[] files = fileChooser.getSelectedFiles();
            try (PDDocument mergedDocument = new PDDocument()) {
                for (File file : files) {
                    PDDocument document = PDDocument.load(file);
                    List<PDPage> pages = document.getPages().toList();
                    for (PDPage page : pages) {
                        mergedDocument.addPage(page);
                    }
                    document.close();
                }
                mergedDocument.save("MergedPDF.pdf");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void splitPDF() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileFilter(new FileNameExtensionFilter("PDF Files", "pdf"));

        int returnValue = fileChooser.showOpenDialog(null);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            try (PDDocument document = PDDocument.load(file)) {
                int totalPages = document.getNumberOfPages();
                for (int i = 0; i < totalPages; i++) {
                    PDDocument splitDocument = new PDDocument();
                    splitDocument.addPage(document.getPage(i));
                    splitDocument.save("SplitPDF_Page_" + (i + 1) + ".pdf");
                    splitDocument.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void annotatePDF() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileFilter(new FileNameExtensionFilter("PDF Files", "pdf"));

        int returnValue = fileChooser.showOpenDialog(null);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            try (PDDocument document = PDDocument.load(file)) {
                PDPage page = document.getPage(0);
                PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true);
                contentStream.beginText();
                contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
                contentStream.newLineAtOffset(100, 600);
                contentStream.showText("Annotation Text");
                contentStream.endText();
                contentStream.close();
                document.save("AnnotatedPDF.pdf");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Leave a Comment