|
| 1 | +# PDF Editor and Signer |
| 2 | + |
| 3 | +A sophisticated PDF editor with intelligent element detection, click-to-place text insertion, and digital signature capabilities built with PySide6 and PyMuPDF. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +### Intelligent Element Detection |
| 8 | +- **Automatic Detection**: The application automatically detects interactive elements in your PDF: |
| 9 | + - **Horizontal Lines**: Text fields and underlines (green highlight) |
| 10 | + - **Signature Fields**: Areas with "signature" keywords (blue highlight) |
| 11 | + - **Date Fields**: Areas with "date" keywords (red highlight) |
| 12 | +- **Visual Feedback**: Detected elements are highlighted when you hover nearby |
| 13 | +- **No Manual Controls**: Detection is always active - no buttons needed |
| 14 | + |
| 15 | +### Click-to-Place Text Insertion |
| 16 | +- **Smart Placement**: Click near any detected element (within 50 pixels) to add text |
| 17 | +- **Type-Aware Formatting**: |
| 18 | + - Signature fields: Italic script font style |
| 19 | + - Date fields: Auto-filled with current date (MM/DD/YYYY) |
| 20 | + - Text lines: Standard font |
| 21 | + - Freeform: Standard font anywhere on the page |
| 22 | +- **Auto-Focus**: Text boxes automatically receive focus when created |
| 23 | +- **Auto-Remove**: Empty text boxes disappear when you click away |
| 24 | +- **Intuitive UX**: Just click where you want to type - the app handles the rest |
| 25 | + |
| 26 | +### Selectable Text Overlay |
| 27 | +- **Native Selection**: Select and copy text directly from the PDF view |
| 28 | +- **Transparent Layer**: Text overlay doesn't interfere with visual appearance |
| 29 | +- **Preserved Layout**: Text positioned exactly as it appears in the PDF |
| 30 | + |
| 31 | +### Zoom Controls |
| 32 | +- **Zoom Slider**: 50% to 400% zoom range |
| 33 | +- **Zoom Buttons**: +/- buttons for incremental zoom (10% steps) |
| 34 | +- **Fit Width**: Automatically fit PDF to window width |
| 35 | +- **Fit Page**: Automatically fit entire page in view |
| 36 | +- **Zoom-Aware Coordinates**: Text placement accounts for current zoom level |
| 37 | + |
| 38 | +### Digital Signatures |
| 39 | +- **Certificate Management**: |
| 40 | + - Create new certificates with RSA 2048-bit keys |
| 41 | + - Load existing certificate and key files |
| 42 | + - X.509 format with SHA-256 hashing |
| 43 | +- **PDF Signing**: Apply digital signatures to edited PDFs |
| 44 | +- **Signature Validation**: Digitally signed documents maintain integrity |
| 45 | + |
| 46 | +## Installation |
| 47 | + |
| 48 | +### Requirements |
| 49 | +- Python 3.7+ |
| 50 | +- PySide6 (Qt6 GUI framework) |
| 51 | +- PyMuPDF (PDF rendering and manipulation) |
| 52 | +- pyHanko (digital signatures) |
| 53 | +- cryptography (certificate generation) |
| 54 | + |
| 55 | +### Install Dependencies |
| 56 | + |
| 57 | +```bash |
| 58 | +pip install -r requirements.txt |
| 59 | +``` |
| 60 | + |
| 61 | +Or install individually: |
| 62 | + |
| 63 | +```bash |
| 64 | +pip install PySide6>=6.0.0 PyMuPDF>=1.23.0 pyHanko>=0.20.0 cryptography>=41.0.0 |
| 65 | +``` |
| 66 | + |
| 67 | +## Usage |
| 68 | + |
| 69 | +### Run the Application |
| 70 | + |
| 71 | +```bash |
| 72 | +python pdf_editor_signer.py |
| 73 | +``` |
| 74 | + |
| 75 | +### Workflow |
| 76 | + |
| 77 | +1. **Open PDF**: Click "Open PDF" to load a document |
| 78 | +2. **Navigate**: Use page navigation controls or enter page number directly |
| 79 | +3. **Add Text**: |
| 80 | + - Click near a detected line, signature field, or date field |
| 81 | + - The text box appears automatically at the correct location |
| 82 | + - Type your text (dates auto-fill) |
| 83 | + - Click elsewhere when done (empty boxes disappear) |
| 84 | +4. **Zoom**: Use zoom controls to adjust view as needed |
| 85 | +5. **Select Text**: Select and copy text directly from the PDF |
| 86 | +6. **Save**: Click "Save PDF" to save your edits |
| 87 | +7. **Sign** (optional): |
| 88 | + - Create or load a certificate |
| 89 | + - Click "Sign PDF" to apply digital signature |
| 90 | + |
| 91 | +### Text Box Types |
| 92 | + |
| 93 | +The application automatically determines the text box type based on what you click: |
| 94 | + |
| 95 | +- **Text Line**: Click near a horizontal line (green highlight) |
| 96 | +- **Signature**: Click near a signature keyword (blue highlight) |
| 97 | +- **Date**: Click near a date keyword (red highlight) |
| 98 | +- **Freeform**: Click anywhere else on the page |
| 99 | + |
| 100 | +### Certificate Management |
| 101 | + |
| 102 | +**Create Certificate**: |
| 103 | +1. Click "Create Certificate" |
| 104 | +2. Enter details (Country, Organization, Common Name, etc.) |
| 105 | +3. Certificate and key files saved in current directory |
| 106 | + |
| 107 | +**Load Certificate**: |
| 108 | +1. Click "Load Certificate" |
| 109 | +2. Select certificate file (.pem) |
| 110 | +3. Select key file (.pem) |
| 111 | +4. Certificate loaded for signing |
| 112 | + |
| 113 | +## Architecture |
| 114 | + |
| 115 | +### Graphics System |
| 116 | +- **Z-Layering**: |
| 117 | + - Z-Level -2: Selectable text overlay (transparent) |
| 118 | + - Z-Level -1: Element detection highlights |
| 119 | + - Z-Level 0: User-created text boxes |
| 120 | +- **Coordinate Conversion**: Automatic conversion between Qt scene coordinates and PDF coordinates |
| 121 | + |
| 122 | +### Detection Algorithm |
| 123 | +- **Line Detection**: Analyzes PDF drawings for horizontal rectangles (<5px height, >50px width) |
| 124 | +- **Text Pattern Matching**: Searches text blocks for "signature" and "date" keywords |
| 125 | +- **Distance Calculation**: Finds nearest detected element within 50-pixel threshold |
| 126 | + |
| 127 | +### Type Safety |
| 128 | +- Full type annotations throughout codebase |
| 129 | +- Strict mypy compliance |
| 130 | +- Qt type hints for all widgets and events |
| 131 | + |
| 132 | +## File Structure |
| 133 | + |
| 134 | +``` |
| 135 | +PDF Editor and Signer/ |
| 136 | +├── pdf_editor_signer.py # Main application (~1200 lines) |
| 137 | +├── requirements.txt # Python dependencies |
| 138 | +└── README.md # This file |
| 139 | +``` |
| 140 | + |
| 141 | +## Technical Details |
| 142 | + |
| 143 | +### Text Box Styling |
| 144 | +- **Signature**: `font-style: italic; font-family: 'Brush Script MT', cursive;` |
| 145 | +- **Date**: Auto-filled with current date on creation |
| 146 | +- **Standard**: Default font, adjustable size |
| 147 | +- **Border**: Yellow border for all editable boxes |
| 148 | + |
| 149 | +### PDF Manipulation |
| 150 | +- **Rendering**: PyMuPDF (fitz) converts pages to QPixmap |
| 151 | +- **Text Extraction**: `page.get_text("dict")` provides text with bounding boxes |
| 152 | +- **Drawing Detection**: `page.get_drawings()` provides vector graphics data |
| 153 | +- **Text Insertion**: Applied to PDF with zoom-aware coordinate conversion |
| 154 | + |
| 155 | +### Digital Signatures |
| 156 | +- **Key Algorithm**: RSA 2048-bit |
| 157 | +- **Certificate Format**: X.509 |
| 158 | +- **Hash Algorithm**: SHA-256 |
| 159 | +- **Signing Backend**: pyHanko with cryptography library |
| 160 | + |
| 161 | +## Known Limitations |
| 162 | + |
| 163 | +- Element detection works best with text-based PDFs (may not detect elements in scanned/image PDFs) |
| 164 | +- Signature detection requires text containing "signature" keyword |
| 165 | +- Date detection requires text containing "date" keyword |
| 166 | +- Custom detection patterns not currently configurable |
| 167 | + |
| 168 | +## Contributing |
| 169 | + |
| 170 | +Contributions welcome! Areas for enhancement: |
| 171 | +- OCR integration for scanned PDFs |
| 172 | +- Configurable detection patterns |
| 173 | +- Custom text box styles |
| 174 | +- Annotation tools (highlighting, shapes) |
| 175 | +- Form field recognition |
| 176 | + |
| 177 | +## License |
| 178 | + |
| 179 | +[Add your license here] |
| 180 | + |
| 181 | +## Credits |
| 182 | + |
| 183 | +Built with: |
| 184 | +- [PySide6](https://wiki.qt.io/Qt_for_Python) - Qt6 Python bindings |
| 185 | +- [PyMuPDF](https://pymupdf.readthedocs.io/) - PDF processing |
| 186 | +- [pyHanko](https://github.com/MatthiasValvekens/pyHanko) - PDF signing |
| 187 | +- [cryptography](https://cryptography.io/) - Cryptographic operations |
0 commit comments