0
# Document Operations
1
2
Core functionality for loading, creating, saving, and manipulating PDF documents. This includes document lifecycle management, page operations, and basic document properties.
3
4
## Document Loading
5
6
Load PDF documents from various sources with optional password support for encrypted documents.
7
8
```java { .api }
9
// Static methods in org.apache.pdfbox.Loader
10
public static PDDocument loadPDF(File file) throws IOException;
11
public static PDDocument loadPDF(InputStream input) throws IOException;
12
public static PDDocument loadPDF(byte[] input) throws IOException;
13
public static PDDocument loadPDF(RandomAccessRead source) throws IOException;
14
public static PDDocument loadPDF(File file, String password) throws IOException;
15
public static PDDocument loadPDF(InputStream input, String password) throws IOException;
16
```
17
18
## Document Creation and Management
19
20
Create new documents and manage document lifecycle.
21
22
```java { .api }
23
// Constructors in org.apache.pdfbox.pdmodel.PDDocument
24
public PDDocument();
25
public PDDocument(COSDocument document);
26
27
// Document lifecycle methods
28
public void close() throws IOException;
29
public boolean isEncrypted();
30
```
31
32
## Page Operations
33
34
Add, remove, and manipulate pages within a PDF document.
35
36
```java { .api }
37
// Page management methods in PDDocument
38
public void addPage(PDPage page);
39
public void removePage(int pageIndex);
40
public PDPageTree getPages();
41
public PDPage getPage(int pageIndex);
42
public int getNumberOfPages();
43
public PDPage importPage(PDPage page);
44
```
45
46
## Document Saving
47
48
Save documents to various output destinations.
49
50
```java { .api }
51
// Save methods in PDDocument
52
public void save(File file) throws IOException;
53
public void save(OutputStream stream) throws IOException;
54
public void save(String fileName) throws IOException;
55
```
56
57
## Page Structure
58
59
Individual page operations and properties.
60
61
```java { .api }
62
// Constructors in org.apache.pdfbox.pdmodel.PDPage
63
public PDPage();
64
public PDPage(PDRectangle mediaBox);
65
66
// Page properties
67
public PDRectangle getMediaBox();
68
public void setMediaBox(PDRectangle mediaBox);
69
public PDRectangle getCropBox();
70
public void setCropBox(PDRectangle cropBox);
71
public int getRotation();
72
public void setRotation(int rotation);
73
74
// Page content and resources
75
public PDStream getContents() throws IOException;
76
public void setContents(PDStream contents) throws IOException;
77
public PDResources getResources();
78
public void setResources(PDResources resources);
79
public List<PDAnnotation> getAnnotations() throws IOException;
80
```
81
82
## Page Tree Operations
83
84
Manage document page collections with tree-based operations.
85
86
```java { .api }
87
// Methods in org.apache.pdfbox.pdmodel.PDPageTree
88
public void add(PDPage page);
89
public void insertAfter(PDPage newPage, PDPage afterPage);
90
public void insertBefore(PDPage newPage, PDPage beforePage);
91
public void remove(PDPage page);
92
public PDPage get(int index);
93
public int indexOf(PDPage page);
94
public int getCount();
95
public Iterator<PDPage> iterator();
96
```
97
98
## Document Information
99
100
Access and modify document metadata and properties.
101
102
```java { .api }
103
// Document metadata methods in PDDocument
104
public PDDocumentInformation getDocumentInformation();
105
public void setDocumentInformation(PDDocumentInformation info);
106
public PDDocumentCatalog getDocumentCatalog();
107
108
// Methods in org.apache.pdfbox.pdmodel.PDDocumentInformation
109
public String getTitle();
110
public void setTitle(String title);
111
public String getAuthor();
112
public void setAuthor(String author);
113
public String getSubject();
114
public void setSubject(String subject);
115
public String getKeywords();
116
public void setKeywords(String keywords);
117
public String getCreator();
118
public void setCreator(String creator);
119
public String getProducer();
120
public void setProducer(String producer);
121
public Calendar getCreationDate();
122
public void setCreationDate(Calendar date);
123
public Calendar getModificationDate();
124
public void setModificationDate(Calendar date);
125
```
126
127
## Common Rectangles
128
129
Standard page sizes and rectangle definitions.
130
131
```java { .api }
132
// Constants in org.apache.pdfbox.pdmodel.common.PDRectangle
133
public static final PDRectangle A0;
134
public static final PDRectangle A1;
135
public static final PDRectangle A2;
136
public static final PDRectangle A3;
137
public static final PDRectangle A4;
138
public static final PDRectangle A5;
139
public static final PDRectangle A6;
140
public static final PDRectangle LETTER;
141
public static final PDRectangle LEGAL;
142
143
// Rectangle methods
144
public float getWidth();
145
public float getHeight();
146
public float getLowerLeftX();
147
public float getLowerLeftY();
148
public float getUpperRightX();
149
public float getUpperRightY();
150
```
151
152
## Usage Examples
153
154
### Creating a Simple Document
155
156
```java
157
PDDocument document = new PDDocument();
158
PDPage page = new PDPage(PDRectangle.A4);
159
document.addPage(page);
160
161
// Set document information
162
PDDocumentInformation info = new PDDocumentInformation();
163
info.setTitle("My Document");
164
info.setAuthor("John Doe");
165
info.setCreationDate(Calendar.getInstance());
166
document.setDocumentInformation(info);
167
168
document.save("simple-document.pdf");
169
document.close();
170
```
171
172
### Loading and Inspecting a Document
173
174
```java
175
PDDocument document = Loader.loadPDF(new File("example.pdf"));
176
177
System.out.println("Number of pages: " + document.getNumberOfPages());
178
System.out.println("Is encrypted: " + document.isEncrypted());
179
180
PDDocumentInformation info = document.getDocumentInformation();
181
System.out.println("Title: " + info.getTitle());
182
System.out.println("Author: " + info.getAuthor());
183
184
// Inspect first page
185
PDPage firstPage = document.getPage(0);
186
PDRectangle mediaBox = firstPage.getMediaBox();
187
System.out.println("Page size: " + mediaBox.getWidth() + " x " + mediaBox.getHeight());
188
189
document.close();
190
```