편집 개요
PDF Oxide는 기존 PDF 편집을 위한 두 가지 수준의 API를 제공합니다: 고수준 Pdf 클래스(권장) 및 저수준 DocumentEditor. 둘 다 PDF를 열어 콘텐츠와 메타데이터를 수정할 수 있습니다. 변경 추적과 저장도 지원합니다.
편집용으로 PDF 열기
Python
from pdf_oxide import PdfDocument
doc = PdfDocument("input.pdf")
편집기는 처음 수정할 때 초기화됩니다. 즉시 읽기를 시작할 수 있으며, set_title() 또는 page()와 같은 변경 메서드를 호출할 때 편집기가 활성화됩니다.
WASM
import { WasmPdfDocument } from "pdf-oxide-wasm";
const bytes = new Uint8Array(/* file bytes */);
const doc = new WasmPdfDocument(bytes);
Rust
통합된 Pdf API를 사용합니다:
use pdf_oxide::api::Pdf;
let mut doc = Pdf::open("input.pdf")?;
또는 저수준 제어를 위해 DocumentEditor를 직접 사용합니다:
use pdf_oxide::editor::DocumentEditor;
let mut editor = DocumentEditor::open("input.pdf")?;
수정 사항 확인
저장하기 전에 변경 사항이 있는지 확인할 수 있습니다:
Python
doc = PdfDocument("input.pdf")
print(doc.is_modified) # False -- no changes yet
doc.set_title("Updated Title")
print(doc.is_modified) # True
Rust
let mut doc = Pdf::open("input.pdf")?;
assert!(!doc.is_modified());
doc.editor().unwrap().set_title("Updated Title");
assert!(doc.is_modified());
저장
Python
doc = PdfDocument("input.pdf")
doc.set_title("New Title")
doc.save("output.pdf")
WASM
import { WasmPdfDocument } from "pdf-oxide-wasm";
const bytes = new Uint8Array(/* file bytes */);
const doc = new WasmPdfDocument(bytes);
doc.setTitle("New Title");
const output = doc.save();
doc.free();
Rust
let mut doc = Pdf::open("input.pdf")?;
doc.editor().unwrap().set_title("New Title");
doc.save("output.pdf")?;
// Or save to a new path
doc.save_as("copy.pdf")?;
save() 메서드는 기본적으로 PDF 전체 재작성을 수행합니다. 고급 저장 옵션(증분 업데이트, 암호화)은 암호화와 보안을 참고하세요.
문서 메타데이터
표준 PDF 메타데이터 필드인 제목, 저자, 주제, 키워드를 읽고 씁니다.
Python
from pdf_oxide import PdfDocument
doc = PdfDocument("input.pdf")
# Set metadata
doc.set_title("Quarterly Report")
doc.set_author("Jane Smith")
doc.set_subject("Q4 2025 Financial Results")
doc.set_keywords("finance, quarterly, 2025")
doc.save("output.pdf")
WASM
import { WasmPdfDocument } from "pdf-oxide-wasm";
const bytes = new Uint8Array(/* file bytes */);
const doc = new WasmPdfDocument(bytes);
// Set metadata
doc.setTitle("Quarterly Report");
doc.setAuthor("Jane Smith");
doc.setSubject("Q4 2025 Financial Results");
doc.setKeywords("finance, quarterly, 2025");
const output = doc.save();
doc.free();
Rust
use pdf_oxide::editor::DocumentEditor;
let mut editor = DocumentEditor::open("input.pdf")?;
// Read metadata
if let Some(title) = editor.title()? {
println!("Current title: {}", title);
}
if let Some(author) = editor.author()? {
println!("Current author: {}", author);
}
if let Some(subject) = editor.subject()? {
println!("Current subject: {}", subject);
}
if let Some(keywords) = editor.keywords()? {
println!("Current keywords: {}", keywords);
}
// Set metadata
editor.set_title("Quarterly Report");
editor.set_author("Jane Smith");
editor.set_subject("Q4 2025 Financial Results");
editor.set_keywords("finance, quarterly, 2025");
editor.save("output.pdf")?;
문서 정보
소스 경로 및 버전
use pdf_oxide::editor::DocumentEditor;
let editor = DocumentEditor::open("input.pdf")?;
// Path to the original file
println!("Source: {}", editor.source_path());
// PDF version as (major, minor)
let (major, minor) = editor.version();
println!("PDF version: {}.{}", major, minor);
// Number of pages
println!("Pages: {}", editor.current_page_count());
전체 API 레퍼런스
DocumentEditor
| 메서드 | 반환값 | 설명 |
|---|---|---|
open(path) |
Result<DocumentEditor> |
편집용으로 PDF 열기 |
is_modified() |
bool |
변경 사항이 있는지 확인 |
source_path() |
&str |
원본 PDF 경로 |
source() |
&PdfDocument |
원본 문서에 읽기 전용으로 접근 |
version() |
(u8, u8) |
PDF 버전(major, minor) |
current_page_count() |
usize |
문서의 페이지 수 |
title() |
Result<Option<String>> |
문서 제목 가져오기 |
set_title(title) |
() |
문서 제목 설정 |
author() |
Result<Option<String>> |
문서 저자 가져오기 |
set_author(author) |
() |
문서 저자 설정 |
subject() |
Result<Option<String>> |
문서 주제 가져오기 |
set_subject(subject) |
() |
문서 주제 설정 |
keywords() |
Result<Option<String>> |
문서 키워드 가져오기 |
set_keywords(keywords) |
() |
문서 키워드 설정 |
save(path) |
Result<()> |
전체 재작성으로 저장 |
save_with_options(path, options) |
Result<()> |
사용자 지정 옵션으로 저장 |
Pdf (통합 API)
| 메서드 | 반환값 | 설명 |
|---|---|---|
Pdf::open(path) |
Result<Pdf> |
편집용으로 PDF 열기 |
Pdf::open_editor(path) |
Result<DocumentEditor> |
DocumentEditor로 바로 열기 |
is_modified() |
bool |
변경 사항이 있는지 확인 |
save(path) |
Result<()> |
문서 저장 |
save_as(path) |
Result<()> |
새 경로로 저장 |
page(index) |
Result<PdfPage> |
DOM 편집용 페이지 가져오기 |
save_page(page) |
Result<()> |
수정된 페이지 되돌려 저장 |
editor() |
Option<&mut DocumentEditor> |
내부 편집기 접근 |
EditableDocument 트레이트
EditableDocument 트레이트는 핵심 편집 계약을 정의합니다:
pub trait EditableDocument {
fn get_info(&mut self) -> Result<DocumentInfo>;
fn set_info(&mut self, info: DocumentInfo) -> Result<()>;
fn page_count(&mut self) -> Result<usize>;
fn get_page_info(&mut self, index: usize) -> Result<PageInfo>;
fn remove_page(&mut self, index: usize) -> Result<()>;
fn move_page(&mut self, from: usize, to: usize) -> Result<()>;
fn duplicate_page(&mut self, index: usize) -> Result<usize>;
fn save(&mut self, path: impl AsRef<Path>) -> Result<()>;
fn save_with_options(&mut self, path: impl AsRef<Path>, options: SaveOptions) -> Result<()>;
}
전체 편집 워크플로
이 예제는 열기, 검사, 메타데이터 수정, 콘텐츠 편집, 저장 등 전체 편집 세션을 보여줍니다.
Python
from pdf_oxide import PdfDocument
# Open the document
doc = PdfDocument("report.pdf")
print(f"Pages: {doc.page_count()}")
# Update metadata
doc.set_title("Annual Report 2025")
doc.set_author("Finance Team")
# Edit text on page 0
page = doc.page(0)
for text in page.find_text_containing("DRAFT"):
page.set_text(text.id, "FINAL")
doc.save_page(page)
# Save
doc.save("report-final.pdf")
Rust
use pdf_oxide::api::Pdf;
let mut doc = Pdf::open("report.pdf")?;
println!("Pages: {}", doc.page_count()?);
// Update metadata
{
let editor = doc.editor().unwrap();
editor.set_title("Annual Report 2025");
editor.set_author("Finance Team");
}
// Edit text on page 0
let mut page = doc.page(0)?;
let drafts = page.find_text_containing("DRAFT");
for t in &drafts {
page.set_text(t.id(), "FINAL")?;
}
doc.save_page(page)?;
// Save
doc.save("report-final.pdf")?;