Custom keys and comments
custompagedictkeycreate
JAVA
JAVA
/**
* This example is written by Bruno Lowagie in answer to the following question:
* http://stackoverflow.com/questions/32527211/itext-how-to-check-if-giant-string-is-present-on-the-pdf-page
*/
package sandbox.logging;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfNumber;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfString;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
/**
*
* @author iText
*/
@WrapToTest
public class CustomPageDictKeyCreate {
public static final String DEST = "results/logging/custom_page_dict_key.pdf";
public static void main(String[] args) throws IOException,
DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
CustomPageDictKeyCreate app = new CustomPageDictKeyCreate();
app.createPdf(DEST);
app.check(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
document.add(new Paragraph("Page 1"));
document.newPage();
document.add(new Paragraph("Page 2"));
document.newPage();
document.add(new Paragraph("Page 3"));
document.newPage();
document.add(new Paragraph("Page 4"));
writer.addPageDictEntry(new PdfName("ITXT_PageMarker"), new PdfString("Marker for page 4"));
document.newPage();
document.add(new Paragraph("Page 5"));
document.newPage();
document.add(new Paragraph("Page 6"));
writer.addPageDictEntry(new PdfName("ITXT_PageMarker"), new PdfName("PageMarker"));
document.newPage();
document.add(new Paragraph("Page 7"));
writer.addPageDictEntry(new PdfName("ITXT_PageMarker"), new PdfNumber(7));
document.newPage();
document.add(new Paragraph("Page 8"));
document.close();
}
public void check(String filename) throws IOException {
PdfReader reader = new PdfReader(filename);
PdfDictionary pagedict;
for (int i = 1; i < reader.getNumberOfPages(); i++) {
pagedict = reader.getPageN(i);
System.out.println(pagedict.get(new PdfName("ITXT_PageMarker")));
}
reader.close();
}
}
custompagedictkeymerge
JAVA
JAVA
/*
* This example is written by Bruno Lowagie in answer to the following question:
* http://stackoverflow.com/questions/34617914
*/
package sandbox.logging;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfString;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author Bruno Lowagie (iText Software)
*/
public class CustomPageDictKeyMerge {
public static final String SRC1 = "resources/pdfs/hello.pdf";
public static final String SRC2 = "resources/pdfs/united_states.pdf";
public static final String SRC3 = "resources/pdfs/hero.pdf";
public static final String DEST = "results/logging/custom_page_dict_key_merge.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new CustomPageDictKeyMerge().createPdf(DEST);
}
public void createPdf(String filename) throws IOException, DocumentException {
PdfName marker = new PdfName("ITXT_PageMarker");
List<PdfReader> readers = new ArrayList<PdfReader>();
readers.add(new PdfReader(SRC1));
readers.add(new PdfReader(SRC2));
readers.add(new PdfReader(SRC3));
Document document = new Document();
PdfCopy copy = new PdfCopy(document, new FileOutputStream(filename));
document.open();
int counter = 0;
int n;
PdfImportedPage importedPage;
PdfDictionary pageDict;
for (PdfReader reader : readers) {
counter++;
n = reader.getNumberOfPages();
for (int p = 1; p <= n; p++) {
pageDict = reader.getPageN(p);
pageDict.put(marker, new PdfString(String.format("Page %s of document %s", p, counter)));
importedPage = copy.getImportedPage(reader, p);
copy.addPage(importedPage);
}
}
// close the document
document.close();
for (PdfReader reader : readers) {
reader.close();
}
}
}
pdfwithcomments
JAVA
JAVA
package sandbox.logging;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.OutputStreamCounter;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class PdfWithComments {
public static final String DEST = "results/logging/pdf_comments.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new PdfWithComments().createPdf(DEST);
}
public void createPdf(String filename) throws DocumentException, IOException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
OutputStreamCounter os = writer.getOs();
os.write("% After open\n".getBytes());
// step 4
document.add(new Paragraph("Hello World!"));
document.newPage();
// step 5
os.write("% Before close\n".getBytes());
document.close();
}
}