### Create PDF with Page Labels Example Source: https://github.com/apache/pdfbox/blob/trunk/RELEASE-NOTES.txt This example demonstrates how to create a PDF document with custom page labels. ```java Create example of PDF creation with page labels ``` -------------------------------- ### Add Example to Jump to Local Page in AddAnnotations Source: https://github.com/apache/pdfbox/blob/trunk/RELEASE-NOTES.txt Includes an example demonstrating how to add annotations that jump to a local page within the PDF document. ```java PDDocument document = new PDDocument(); PDPage page1 = new PDPage(); document.addPage(page1); PDPage page2 = new PDPage(); document.addPage(page2); // Create a destination on page2 PDPageXYZDestination dest = new PDPageXYZDestination(page2); dest.setZoom(1.0f); // Create an action to go to the destination PDActionGoTo action = new PDActionGoTo(); action.setDestination(dest); // Create an annotation (e.g., a link) that triggers the action PDAnnotationLink link = new PDAnnotationLink(); link.setRectangle(new PDRectangle(50, 50, 100, 100)); // Annotation position link.setAction(action); page1.addAnnotation(link); document.save("document_with_link.pdf"); document.close(); ``` -------------------------------- ### Get Async Profiler Help Source: https://github.com/apache/pdfbox/blob/trunk/benchmark/README.md Display help information and available options for configuring the async-profiler within JMH. ```bash java -jar target/benchmarks.jar -prof async:help ``` -------------------------------- ### Create Flexible CheckBox and RadioButtons Example Source: https://github.com/apache/pdfbox/blob/trunk/RELEASE-NOTES.txt This example showcases the creation of flexible CheckBox and RadioButtons in PDF documents. ```java Create flexible CheckBox and RadioButtons example ``` -------------------------------- ### CreateVisibleSignature Example with Correct Alias Source: https://github.com/apache/pdfbox/blob/trunk/RELEASE-NOTES.txt Corrects the CreateVisibleSignature example to use the proper alias for keystore access, ensuring signatures are created with the intended credentials. ```java KeyStore ks = KeyStore.getInstance("PKCS12"); ks.load(new FileInputStream(keyStorePath), keyStorePassword.toCharArray()); String alias = "my-alias"; // Correct alias for the certificate // ... rest of the signing process ... ``` -------------------------------- ### Create PDF with Patterns Example Source: https://github.com/apache/pdfbox/blob/trunk/RELEASE-NOTES.txt Sample code for creating a PDF with patterns. This demonstrates how to use patterns for graphical elements within a PDF document. ```java PDPageContentStream contentStream = new PDPageContentStream(document, page); // ... pattern setup code ... contentStream.fillPatten(pattern, rect); contentStream.close(); ``` -------------------------------- ### Create PDFMergerUtility Example with Metadata Handling Source: https://github.com/apache/pdfbox/blob/trunk/RELEASE-NOTES.txt Provides an example of using PDFMergerUtility with improved metadata handling, ensuring metadata is correctly preserved during merging. ```java PDFMergerUtility merger = new PDFMergerUtility(); merger.addSource("source1.pdf"); merger.addSource("source2.pdf"); merger.setDestinationFileName("merged.pdf"); merger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly()); ``` -------------------------------- ### Create AcroForm Example with Two Widgets Source: https://github.com/apache/pdfbox/blob/trunk/RELEASE-NOTES.txt Provides an example of creating an AcroForm field that has two associated widgets, demonstrating complex form field configurations. ```java PDAcroForm acroForm = new PDAcroForm(document); document.getDocumentCatalog().setAcroForm(acroForm); PDTextField textField = new PDTextField(acroForm); textField.setPartialName("MyTextField"); // Create first widget PDRectangle rect1 = new PDRectangle(50, 700, 100, 20); PDAnnotationWidget widget1 = new PDAnnotationWidget(); widget1.setRectangle(rect1); widget1.setFieldType(PDAnnotationWidget.FIELD_TYPE_TEXT); textField.add(widget1); // Create second widget PDRectangle rect2 = new PDRectangle(50, 680, 100, 20); PDAnnotationWidget widget2 = new PDAnnotationWidget(); widget2.setRectangle(rect2); widget2.setFieldType(PDAnnotationWidget.FIELD_TYPE_TEXT); textField.add(widget2); acroForm.addFields(textField); document.save("form_with_two_widgets.pdf"); document.close(); ``` -------------------------------- ### Build Apache PDFBox Source: https://github.com/apache/pdfbox/blob/trunk/AGENTS.md Standard Maven command to clean and install the entire project. Ensure the correct Java version for the target branch is set. ```bash mvn clean install ``` -------------------------------- ### Run Apache PDFBox Tests Source: https://github.com/apache/pdfbox/blob/trunk/AGENTS.md Maven command to execute all tests without performing a full installation. Use this for quick validation of changes. ```bash mvn test ``` -------------------------------- ### Add Watermark Example Source: https://github.com/apache/pdfbox/blob/trunk/RELEASE-NOTES.txt This snippet provides an example of how to add a watermark to a PDF document. ```java Add watermark example ``` -------------------------------- ### Add Text to AcroForm Examples Source: https://github.com/apache/pdfbox/blob/trunk/RELEASE-NOTES.txt This example shows how to add text to AcroForm fields within a PDF document. ```java Add text to AcroForm examples ``` -------------------------------- ### Add Polygon Annotation Example Source: https://github.com/apache/pdfbox/blob/trunk/RELEASE-NOTES.txt This snippet demonstrates how to add a polygon annotation to a PDF document using the AddAnnotations.java example. ```java AddAnnotations.java ``` -------------------------------- ### CWEAVE Parsing Example Source: https://github.com/apache/pdfbox/blob/trunk/pdfbox/src/test/resources/input/cweb.pdf-sorted.txt Demonstrates the output of CWEAVE when run in debugging mode ('@ @c @2') on a simple C function. The output shows grammar rules being applied during parsing. ```text main (argc,argv) char **argv; { for (;argc>0;argc--) printf("%s\n",argv[argc-1]); } ``` ```text 4:*exp ( +exp+ )... 11:*exp +exp+ int... 5:*+exp+ int +unorbinop+... ... 60: +fn_decl+*+{+ -stmt- +}- 55:*+fn_decl+ -stmt- 52:*+function-... ``` -------------------------------- ### CWEAVE Peeping Mode Example Output Source: https://github.com/apache/pdfbox/blob/trunk/pdfbox/src/test/resources/input/cweb.pdf.txt Illustrates the output generated by CWEAVE when its 'peeping' mode is active, showing grammar rule applications and parsing states. This helps in understanding CWEAVE's internal processing. ```text 4:*exp ( +exp+ )... 11:*exp +exp+ int... 5:*+exp+ int +unorbinop+... ... 60: +fn_decl+*+{+ -stmt- +}- 55:*+fn_decl+ -stmt- 52:*+function-... ``` -------------------------------- ### Usage of Separation Color (Spot Color) Example Source: https://github.com/apache/pdfbox/blob/trunk/RELEASE-NOTES.txt This snippet illustrates the usage of separation color, also known as spot color, in PDF documents. ```java Add example of usage of separation color (spot color) ``` -------------------------------- ### CWEAVE Formatting Example with Brackets Source: https://github.com/apache/pdfbox/blob/trunk/pdfbox/src/test/resources/input/cweb.pdf.txt Example of C code formatted by CWEAVE with the '-e' option enabled, which encloses C material in \PB{...} for special hooks. This option has no effect on CTANGLE. ```c \PB{main (argc,argv) char **argv; { for (;argc>0;argc--) printf("%s\n",argv[argc-1]); }} ``` -------------------------------- ### Index Entry with Custom Sort Key Source: https://github.com/apache/pdfbox/blob/trunk/pdfbox/src/test/resources/input/cweb.pdf.txt Example of using the '@:' feature to create an index entry for 'TeX' with a custom sort key. The \9 macro ensures the sort key is not printed. ```tex @:TeX}{\TeX@> ``` -------------------------------- ### Define Custom Sanitization with \def and \ifheader Source: https://github.com/apache/pdfbox/blob/trunk/pdfbox/src/test/resources/input/cweb.pdf.txt This example demonstrates how to define a macro \klujj that conditionally formats text based on whether it's intended for a header. It also shows how to create a custom table of contents entry and bookmark text. ```tex \def\klujj#1\{\ifheader FOO\else foo\fi} @*Chinese \klujj bar\. ``` ```tex \ZZ {Chinese \klujj bar\}{1}{1}{1}{Chinese bar} ``` -------------------------------- ### List All Available Benchmarks Source: https://github.com/apache/pdfbox/blob/trunk/benchmark/README.md Display a list of all benchmarks that can be run. This is useful for identifying specific tests. ```bash java -jar target/benchmarks.jar -l ``` -------------------------------- ### Run Benchmark with GC Profiler Source: https://github.com/apache/pdfbox/blob/trunk/benchmark/README.md Execute benchmarks and measure allocation rates using the GC profiler. Focus on 'norm' allocation rates for per-operation measurements. ```bash java -jar target/benchmarks.jar -prof gc ``` -------------------------------- ### Run Benchmark with Async Profiler Source: https://github.com/apache/pdfbox/blob/trunk/benchmark/README.md Execute benchmarks using the async-profiler. Specify the library path to the async profiler shared object. ```bash java -jar target/benchmarks.jar -prof async:libPath=/path/to/libasyncProfiler.so ``` -------------------------------- ### CWEB Translation of Embedded C Code Source: https://github.com/apache/pdfbox/blob/trunk/pdfbox/src/test/resources/input/cweb.pdf.txt This is how CWEB translates the example of embedded C code into TEX, demonstrating the handling of special characters and formatting. ```tex If \\{pa} is declared as ‘&{int} ${}{*}\\\{pa}$’, the assignment $\\\{pa}\K{\AND}\|a[\T{0}]$ makes \\{pa} point to the zeroth element of \|a. ``` -------------------------------- ### Run Benchmark with Async Profiler and Flame Graph Output Source: https://github.com/apache/pdfbox/blob/trunk/benchmark/README.md Execute benchmarks using the async-profiler and generate a flame graph. The semicolon is escaped to prevent command separation. ```bash java -jar target/benchmarks.jar -prof async:libPath=/path/to/libasyncProfiler.so\;output=flamegraph ``` -------------------------------- ### Run All Benchmarks Source: https://github.com/apache/pdfbox/blob/trunk/benchmark/README.md Execute all available benchmarks in the module. Ensure the JMH JAR is in the current directory or specify its path. ```bash java -jar target/benchmarks.jar ``` -------------------------------- ### Table of Contents Data Entry - TEX Source: https://github.com/apache/pdfbox/blob/trunk/pdfbox/src/test/resources/input/cweb.pdf-sorted.txt Example format for data entries in a table of contents file. Each line represents a section with its title, and numerical identifiers. ```tex \ZZ {Introduction}{0}{1}{28}{} \ZZ {The character set}{2}{5}{29}{} ``` -------------------------------- ### Implement GoToE Action Type Source: https://github.com/apache/pdfbox/blob/trunk/RELEASE-NOTES.txt Implementation of the GoToE action type for PDF navigation. This allows for creating links that navigate to specific destinations within a PDF. ```java PDActionGoTo action = new PDActionGoTo(); action.setDestination(destination); currentPage.addPDAction(action); ``` -------------------------------- ### CWEB Logic for Skipping Comment Lines Source: https://github.com/apache/pdfbox/blob/trunk/pdfbox/src/test/resources/input/cweb.pdf-sorted.txt This CWEB code snippet demonstrates how to skip over comment lines in a change file, allowing lines starting with '@@' but not '@@y', '@@z', or '@@i'. ```c while(1) { change_line++; ``` -------------------------------- ### Skip Comment Lines in Change File (TeX) Source: https://github.com/apache/pdfbox/blob/trunk/pdfbox/src/test/resources/input/cweb.pdf.txt Skips over comment lines in the change file, identified by lines starting with '@'. Returns if the end of the file is reached. ```tex \X13:Skip over comment lines in the change file; \PB{\&{return}} if end of file\X${} \&{while} (\T{1})\5 ${}\{} ${}\{change\_line}\PP;{} \&{if} ${}(\R\{input\_ln}(\{change\_file})){}$\1 \&{return};\2 \&{if} ${}(\{limit}<\{buffer}+\T{2}){}$\1 \&{continue};\2 \&{if} ${}(\{buffer}[\T{0}]\I\.{'@'}){}$\1 \&{continue};\2 \&{if} (\\{xisupper}(\{buffer}[\T{1}]))\1 ${}\{buffer}[\T{1}]\K\\{tolower}(\{buffer}[\T{1}]);{} \&{if} ${}(\{buffer}[\T{1}]\E\.{'x'}){}$\1 \&{break};\2 \&{if} ${}(\{buffer}[\T{1}]\E\.{'y'}\V\{buffer}[\T{1}]\E\.{'z'}\V\{buffer}[% \T{1}]\E\.{'i'}){}$\5 ${}\{} ${}\{loc}\K\{buffer}+\T{2};{} \\{err\_print}(\.{"!\ Missing\ @x\ in\ cha}\)\,.{nge\ file");{} \4${}\{} \4${}\{} \par \U12.\fi ``` -------------------------------- ### List Available Profilers Source: https://github.com/apache/pdfbox/blob/trunk/benchmark/README.md Display a list of all available profilers that can be used with JMH benchmarks. ```bash java -jar target/benchmarks.jar -lprof ``` -------------------------------- ### CWEAVE Command Line Usage Source: https://github.com/apache/pdfbox/blob/trunk/pdfbox/src/test/resources/input/cweb.pdf-sorted.txt Details the command-line syntax for the CWEAVE program, mirroring CTANGLE's conventions for file handling and options. Output extensions include .tex and .idx/.scn for index files. ```bash cweave [options] web_file[.w] [{change_file[.ch]|−} [out_file]] ``` -------------------------------- ### Skip Comment Lines in Change File (C) Source: https://github.com/apache/pdfbox/blob/trunk/pdfbox/src/test/resources/input/cweb.pdf.txt Skips over comment lines in the change file, identified by lines starting with '@'. Returns if the end of the file is reached. ```c while(1){ change_line++; if(!input_ln(change_file))return; if(limit' inserts one inch of space, and '@t\4@>' backspaces by one indentation unit. ```cweb @t\hskip 1in@> ``` ```cweb @t\4@> ``` ```cweb @t}\3{−5@>} ``` -------------------------------- ### CWEB Function to Initialize Change Buffer Source: https://github.com/apache/pdfbox/blob/trunk/pdfbox/src/test/resources/input/cweb.pdf-sorted.txt The `prime_the_change_buffer` function initializes the `change_buffer` for the next matching operation. It is called when `changing` is 1 and handles end-of-file conditions. ```c void prime_the_change_buffer() { change_limit=change_buffer; @; @; @; } ``` -------------------------------- ### Referencing Sections in C and TEX Source: https://github.com/apache/pdfbox/blob/trunk/pdfbox/src/test/resources/input/cweb.pdf-sorted.txt Demonstrates how to reference C code sections within both C text and TEX text using the '@
' construct. Note the use of '|...|' when citing a section in TEX text. ```c if (x==0) @ ... using the algorithm in |@|. ``` -------------------------------- ### Create Clickable URLs with \pdfURL Source: https://github.com/apache/pdfbox/blob/trunk/pdfbox/src/test/resources/input/cweb.pdf-sorted.txt Use the \pdfURL macro to create clickable links in PDF documents. The first argument is the displayed text, and the second is the URL. Special characters in URLs like '@' and '~' should be escaped using '@@' and '\\TILDE/' respectively. ```tex You can send email to \pdfURL{the author}{mailto:andreas.scherer@@pobox.com} or visit \pdfURL{his home page}{http://www.pobox.com/\TILDE/scherer}. ``` -------------------------------- ### Move Buffer and Limit to Change Buffer (C) Source: https://github.com/apache/pdfbox/blob/trunk/pdfbox/src/test/resources/input/cweb.pdf-sorted.txt Copies the content from the current buffer and limit to the change buffer and change limit. This is used to store the current line for later comparison or processing. ```c { change_limit= change_buffer-buffer+limit; strncpy(change_buffer,buffer,limit-buffer+1); } ``` -------------------------------- ### Using RenderingMode with PDPageContentStream Source: https://github.com/apache/pdfbox/blob/trunk/RELEASE-NOTES.txt Demonstrates how to use the RenderingMode with PDPageContentStream for controlling text rendering behavior, such as showing text as filled or stroked. ```java PDPageContentStream contentStream = new PDPageContentStream(document, page); contentStream.setTextRenderingMode(RenderingMode.FILL_STROKE); contentStream.showText("Sample Text"); contentStream.close(); ```