,false,false]–> Not working Use code with caution. Applying the License
By default, the library runs in evaluation mode, which adds watermarks to generated files and restricts collection sizes. Apply a valid or temporary license file early in your application’s lifecycle:
import com.aspose.pdf.License; public class AsposeInitializer { public static void initLicense() { try { License license = new License(); license.setLicense(new java.io.FileInputStream(“Aspose.PDF.lic”)); } catch (Exception e) { System.err.println(“License error: ” + e.getMessage()); } } } Use code with caution. 2. Document Generation and Structure Basics
The library maps the structure of a PDF directly to an accessible object tree via its DOM API. The root element is the Document class, which aggregates a collection of Page objects. Each page contains elements such as paragraphs, tables, images, and text fragments. Creating a PDF from Scratch
The following code instantiates a blank file, appends a new page, injects a standard text fragment, and commits the data to disk:
import com.aspose.pdf.Document; import com.aspose.pdf.Page; import com.aspose.pdf.TextFragment; public class CreatePDF { public static void main(String[] args) { // Instantiate the core document container Document document = new Document(); // Append an empty page to the collection Page page = document.getPages().add(); // Create a structured block of text TextFragment text = new TextFragment(“Hello, PDF World via Aspose.PDF for Java!”); text.getTextState().setFontSize(14); // Add the fragment to the paragraphs array of the target page page.getParagraphs().add(text); // Save the resulting document document.save(“HelloWorld.pdf”); document.close(); } } Use code with caution. 3. Core PDF Manipulation Workflows
Enterprise applications frequently require programmatic modification of pre-existing documents. Below are the most common transactional operations. Modifying and Replacing Text
The library provides the TextFragmentAbsorber utility to sweep through a document, match specific text strings or regex patterns, and update them dynamically:
import com.aspose.pdf.Document; import com.aspose.pdf.TextFragment; import com.aspose.pdf.TextFragmentAbsorber; public class ModifyText { public static void replaceText(String src, String target, String find, String replace) { Document pdfDoc = new Document(src); // Search the document for instances of the desired phrase TextFragmentAbsorber absorber = new TextFragmentAbsorber(find); pdfDoc.getPages().accept(absorber); // Loop through all discovered matching text fragments for (TextFragment fragment : absorber.getTextFragments()) { fragment.setText(replace); } pdfDoc.save(target); pdfDoc.close(); } } Use code with caution. Page Tree Reordering, Extraction, and Removal
Manipulate page assets by interacting directly with the PageCollection container:
import com.aspose.pdf.Document; public class PageOperations { public static void manipulatePages(String sourcePath, String outputPath) { Document document = new Document(sourcePath); // Delete a specific page (e.g., page 3) document.getPages().delete(3); // Extract a page by inserting it into a brand-new document container Document extractedDoc = new Document(); extractedDoc.getPages().add(document.getPages().get_Item(1)); extractedDoc.save(“ExtractedPage.pdf”); extractedDoc.close(); // Save the modified original file document.save(outputPath); document.close(); } } Use code with caution. Document Merging and Splitting
Combine multiple individual source streams into a single composite document using the PdfFileEditor helper utility:
import com.aspose.pdf.facades.PdfFileEditor; public class DocumentAssembler { public static void mergeFiles(String[] inputPaths, String outputPath) { PdfFileEditor fileEditor = new PdfFileEditor(); // Concatenate an array of absolute file paths together into one target location boolean success = fileEditor.concatenate(inputPaths, outputPath); if (success) { System.out.println(“Documents successfully merged.”); } } } Use code with caution. Formatting PDF Document|Aspose.PDF for Java