hwplib

repository·main·Indexed 20 days ago

https://github.com/neolord0/hwplib

A Java library for parsing, reading, and writing HWP (Hangul Word Processor) files. It provides functionality to extract text via TextExtractor, manage form fields with FieldFinder, and manipulate document structures, tables, and content. It supports reading from files or URLs and writing to files or streams, though it does not support encrypted files or conversion to PDF, Image, or HTML.

Tokens
1.3K
Snippets
6
Records
8
Agent score
20%

What's inside hwplib

  1. Supported HWP manipulation tasks

    main

    The library supports a wide range of HWP document manipulations, including:

    • Document Structure: Adding paragraphs from other files (Adding_Paragraph_Between_HWPFile), changing paper size (Changing_PaperSize).
    • Tables: Inserting tables (Inserting_Table), merging cells (Merging_Cell), and removing rows (Removing_Table_Row).
    • Content: Inserting images (Inserting_Image), inserting hyperlinks (Inserting_HyperLink), and setting character shapes like font size or underlines (Inserting_CharShape).
    • Fields: Finding and setting field text and finding all fields.
  2. Limitations of hwplib

    main

    The following features are not currently supported by hwplib:

    • Reading or writing encrypted HWP files.
    • Converting HWP files to Image, PDF, or HTML formats.
    • Retrieving specific page content or calculating the total number of pages.
  3. Install hwplib via Maven

    main

    Add the following dependency to your Maven pom.xml to use the hwplib library for handling HWP (Hangul Word Processor) files.

    <dependency>
        <groupId>kr.dogfoot</groupId>
        <artifactId>hwplib</artifactId>
    </dependency>
  4. Read HWP files from a file or URL

    main

    Use the HWPReader class to load HWP files into memory as HWPFile objects. You can read from a local file path or a remote URL.

    // Reading from a local file
    String filename = "sample.hwp"; 
    HWPFile hwpFile = HWPReader.fromFile(filename); 
    
    // Reading from a URL
    String url = "http://example.com/sample.hwp";
    HWPFile hwpFile = HWPReader.fromURL(url); 
  5. Extract text from HWP files

    main

    The TextExtractor class allows you to extract text from an HWPFile using different strategies defined by TextExtractMethod:

    • OnlyMainParagraph: Extracts text only from the main paragraphs.
    • InsertControlTextBetweenParagraphText: Inserts the text of controls (like images or tables) between paragraph texts during extraction.
    • AppendControlTextAfterParagraphText: Appends the text of controls after the paragraph text.

    For very large files, use the specialized extraction methods designed to prevent memory issues.

    HWPFile hwpFile = HWPReader.fromFile(filename); 
    // Extract text with control text inserted between paragraphs
    String hwpText = TextExtractor.extract(hwpFile, TextExtractMethod.InsertControlTextBetweenParagraphText);
  6. Find and manipulate field text (Click-Here fields)

    main

    You can find and manage field text (often used for forms/templates) using the FieldFinder class.

    • FieldFinder.getClickHereText(hwpFile, "fieldName"): Retrieves the text associated with a specific field name.
    • FieldFinder.getClickHereText(hwpFile, "fieldName", TextExtractMethod.OnlyMainParagraph): Retrieves field text with specific extraction constraints.
    • FieldFinder.findAll(hwpFile): Finds all fields in the document.
    • Setting_FieldText: Methods are available to set text for specific fields.
    // Get text from a specific field
    String text1 = FieldFinder.getClickHereText(hwpFile, "필드이름");
    
    // Get text with specific extraction method
    String text2 = FieldFinder.getClickHereText(hwpFile, "필드1", TextExtractMethod.OnlyMainParagraph);
  7. Find controls using a custom filter

    main

    Use ControlFinder.find() to search for specific controls within an HWPFile by implementing the ControlFilter interface.

    public static class MyControlFilter implements ControlFilter {
        @Override
        public boolean isMatched(Control control, Paragraph paragraph, Section section) {
            // Define your filtering logic here
            return true; 
        }
    }
    
    // Usage
    ArrayList<Control> result = ControlFinder.find(hwpFile, new MyControlFilter());
  8. Save HWP files to a file or stream

    main

    After modifying a HWPFile object, you can save it back to a file using HWPWriter.toFile() or output it to a stream using HWPWriter.toStream().

    // Open a file
    String filename = "test.hwp"; 
    HWPFile hwpFile = HWPReader.fromFile(filename); 
    
    if (hwpFile != null) {
        // ... perform modifications ...
    
        // Save to a new file
        String writePath = "test-edited.hwp";
        HWPWriter.toFile(hwpFile, writePath);
    }