2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Poi

Last updated at Posted at 2023-12-15

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;

public class WordToPdfConverter {

public static void main(String[] args) {
    try {
        // Wordファイル読み込み
        FileInputStream fis = new FileInputStream("input.docx");
        XWPFDocument document = new XWPFDocument(fis);
        
        // PDFファイル作成
        PDDocument pdfDocument = new PDDocument();
        PDPage page = new PDPage();
        pdfDocument.addPage(page);

        // PDFにテキスト書き込み
        PDPageContentStream contentStream = new PDPageContentStream(pdfDocument, page);
        contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
        
        // WordファイルのテキストをPDFに書き込む(ここでは単純にテキストをコピー)
        for (XWPFParagraph paragraph : document.getParagraphs()) {
            contentStream.beginText();
            contentStream.newLineAtOffset(10, 700); // 位置調整
            contentStream.showText(paragraph.getText());
            contentStream.endText();
        }
        
        contentStream.close();

        // PDFファイル保存
        FileOutputStream fos = new FileOutputStream("output.pdf");
        pdfDocument.save(fos);
        pdfDocument.close();
        fis.close();
        fos.close();
        
        System.out.println("変換が完了しました。");

    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

Excel to PDF (using Apache POI):

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelToPdfConverter {

public static void main(String[] args) {
    try {
        // Excelファイル読み込み
        FileInputStream excelFile = new FileInputStream("input.xlsx");
        Workbook workbook = WorkbookFactory.create(excelFile);

        // PDFファイル作成
        FileOutputStream pdfFile = new FileOutputStream("output.pdf");
        workbook.write(pdfFile);

        // ファイルクローズ
        workbook.close();
        excelFile.close();
        pdfFile.close();

        System.out.println("ExcelからPDFへの変換が完了しました。");

    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

PowerPoint to PDF (using Apache POI):

import org.apache.poi.xslf.usermodel.XMLSlideShow;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class PowerPointToPdfConverter {

public static void main(String[] args) {
    try {
        // PowerPointファイル読み込み
        FileInputStream pptFile = new FileInputStream("input.pptx");
        XMLSlideShow ppt = new XMLSlideShow(pptFile);

        // PDFファイル作成
        FileOutputStream pdfFile = new FileOutputStream("output.pdf");
        ppt.write(pdfFile);

        // ファイルクローズ
        ppt.close();
        pptFile.close();
        pdfFile.close();

        System.out.println("PowerPointからPDFへの変換が完了しました。");

    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFFooter;
import org.apache.poi.xwpf.usermodel.XWPFFootnote;
import org.apache.poi.xwpf.usermodel.XWPFHeader;
import org.apache.poi.xwpf.usermodel.XWPFPicture;
import org.apache.poi.xwpf.usermodel.XWPFRelation;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFSmartTag;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

public class WordHeaderExtractor {
public static void main(String[] args) {
String filePath = "your_word_file.docx";
try (FileInputStream fis = new FileInputStream(filePath)) {
XWPFDocument document = new XWPFDocument(fis);

        List<XWPFHeader> headers = document.getHeaderList();

        int pageNum = 1;
        for (XWPFHeader header : headers) {
            System.out.println("Header on Page " + pageNum + ":");
            System.out.println(getTextFromParagraphs(header.getParagraphs()));
            System.out.println();
            pageNum++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static String getTextFromParagraphs(List<XWPFRun> runs) {
    StringBuilder text = new StringBuilder();
    for (XWPFRun run : runs) {
        text.append(run.getText());
    }
    return text.toString();
}

}

import java.io.FileInputStream;
import org.apache.poi.xwpf.usermodel.*;

public class ReadWordHeadersAndFooters {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("YourWordFile.docx");
XWPFDocument document = new XWPFDocument(fis);

        // 各セクションのページ数取得
        int sectionCount = document.getDocument().getSections().size();

        // 各セクションのヘッダーおよびフッターを出力
        for (int sectionIndex = 0; sectionIndex < sectionCount; sectionIndex++) {
            XWPFHeaderFooterPolicy headerFooterPolicy = document.getHeaderFooterPolicy();
            XWPFHeader header = headerFooterPolicy.getHeader(sectionIndex);
            XWPFFooter footer = headerFooterPolicy.getFooter(sectionIndex);

            int pageCountInSection = headerFooterPolicy.getNumOfHeaderFooterInSection(sectionIndex);

            for (int pageIndex = 0; pageIndex < pageCountInSection; pageIndex++) {
                System.out.println("Section " + (sectionIndex + 1) + ", Page " + (pageIndex + 1) + " Header:");
                if (header != null) {
                    System.out.println(header.getText());
                }

                System.out.println("Section " + (sectionIndex + 1) + ", Page " + (pageIndex + 1) + " Footer:");
                if (footer != null) {
                    System.out.println(footer.getText());
                }

                System.out.println("--------");
            }
        }

        fis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDType1Font;

public class TextToPDFConverter {

public static void main(String[] args) {
    try {
        // テキストファイルのパス
        String textFilePath = "input.txt";

        // PDFドキュメントを作成します
        PDDocument document = new PDDocument();
        
        // 新しいページを作成します
        PDPage page = new PDPage();
        document.addPage(page);

        // ページにコンテンツを追加します
        PDPageContentStream contentStream = new PDPageContentStream(document, page);

        // テキストファイルからテキストを読み込んでPDFに書き込みます
        try (BufferedReader reader = new BufferedReader(new FileReader(textFilePath))) {
            String line;
            float yPosition = 700; // ページ上のテキストのY座標
            while ((line = reader.readLine()) != null) {
                contentStream.beginText();
                contentStream.setFont(PDType1Font.HELVETICA, 12);
                contentStream.newLineAtOffset(100, yPosition);
                contentStream.showText(line);
                contentStream.endText();
                yPosition -= 12; // テキストの高さ分を減算
            }
        }

        // ページを閉じます
        contentStream.close();

        // PDFドキュメントを保存します
        document.save(new File("output.pdf"));

        // ドキュメントを閉じます
        document.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?