pdfa/transparent_stamp_for_pdfa1
Apply a transparent stamp to an existing PDF/A-1 document while maintaining PDF/A-1 conformance.
Download Java Code Switch to PHP Code Show Output
/*
* Transparent stamp for PDF/A-1:
* Apply a transparent stamp to an existing PDF/A-1 document while maintaining
* PDF/A-1 conformance.
*
* Import all pages from an existing PDF/A-1 document and place a stamp on the
* page. The stamp is filled with a pattern color, where the pattern consists of
* a bitmap. The bitmap is used as a mask to create a certain percentage of
* transparency. This is required since real transparency is not allowed in
* PDF/A-1. Transparency by pattern color is PDF/A-1 compatible, so we use it to
* apply a transparent stamp on a PDF/A-1 document while maintaining PDF/A-1
* conformance.
*
* Note that this trick is only necessary for PDF/A-1, as true PDF
* transparency is allowed beginning with PDF/A-2.
*
* If the transparent stamp is not visible in Acrobat (or is visible only at
* certain zoom levels) you can set the following in Acrobat:
* Edit, Preferences, Page Display, enable "Smooth images".
*
* Required software: PDFlib+PDI/PPS 10
* Required data: PDF document
*/
package com.pdflib.cookbook.pdflib.pdfa;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class transparent_stamp_for_pdfa1
{
public static void main (String argv[])
{
/* This is where the data files are. Adjust as necessary. */
String searchpath = "../input";
/* By default annotations are also imported. In some cases this
* requires the Noto fonts for creating annotation appearance streams.
* We therefore set the searchpath to also point to the font directory.
*/
String fontpath = "../resource/font";
String outfile = "transparent_stamp_for_pdfa1.pdf";
String title = "Transparent Stamp for PDF/A-1";
pdflib p = null;
String pdffile = "PLOP-datasheet-PDFA-1b.pdf";
int indoc, pageno, endpage, page, font, pattern;
double w;
double ret;
String res;
int image;
int exitcode = 0;
/* Data set for our halftoning bitmap: the bits describe small
* circles which cover a certain percentage of the total pattern area.
*/
int data[][] = {
{ 0x00, 0x00, /* 30% */
0x00, 0x00,
0x00, 0x00,
0x03, 0xC0,
0x07, 0xE0,
0x0F, 0xF0,
0x1F, 0xF8,
0x1F, 0xF8,
0x1F, 0xF8,
0x1F, 0xF8,
0x0F, 0xF0,
0x07, 0xE0,
0x03, 0xC0,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
},
{ 0x00, 0x00, /* 20% */
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x03, 0xC0,
0x07, 0xE0,
0x0F, 0xF0,
0x0F, 0xF0,
0x0F, 0xF0,
0x0F, 0xF0,
0x07, 0xE0,
0x03, 0xC0,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
}};
int ht = 1; /* index in halftone array */
try {
final int SCALING_FACTOR = 16;
p = new pdflib();
p.set_option("searchpath={" + searchpath + "}");
p.set_option("searchpath={" + fontpath + "}");
/* This means we must check return values of load_font() etc. */
p.set_option("errorpolicy=return");
if (p.begin_document(outfile, "pdfa=PDF/A-1b:2005") == -1)
throw new Exception("Error: " + p.get_errmsg());
p.set_info("Creator", "PDFlib Cookbook");
p.set_info("Title", title);
/*
* Open the input PDF. This must be done before creating the pattern
* because the output intent must be set before defining the
* pattern.
*/
indoc = p.open_pdi_document(pdffile, "");
if (indoc == -1)
throw new Exception("Error: " + p.get_errmsg());
endpage = (int) p.pcos_get_number(indoc, "length:pages");
/*
* Since the input document contains its own output intent retrieve
* the output intent from the input document and copy it to the
* output document.
*/
res = p.pcos_get_string(indoc, "type:/Root/OutputIntents");
if (res.equals("array")) {
ret = p.process_pdi(indoc, -1, "action=copyoutputintent");
if (ret == -1)
throw new Exception("Error: " + p.get_errmsg());
}
else
{
/* If the input document doesn't have any output intent we
* explicitly set sRGB so that we can use grayscale bitmaps.
*/
p.load_iccprofile("sRGB", "usage=outputintent");
}
/*
* Define a bitmap pattern based on an image mask. We scale down
* the image to provide a smoother appearance on screen.
*/
byte[] bitmap = new byte[data[ht].length];
for (int j = 0; j < data[ht].length; j++)
bitmap[j] = (byte) data[ht][j];
p.create_pvf("/pvf/image/bitmap", bitmap, "");
image = p.load_image("raw", "/pvf/image/bitmap",
"bpc=1 components=1 height=16 width=16 invert mask");
if (image == -1)
throw new Exception("Error: " + p.get_errmsg());
w = (double) 16 / SCALING_FACTOR;
pattern = p.begin_pattern_ext(w, w, "");
p.fit_image(image, 0, 0, "scale=" + (double) 1 / SCALING_FACTOR);
p.end_pattern();
p.close_image(image);
/* Loop over all pages of the input document */
for (pageno = 1; pageno <= endpage; pageno++) {
page = p.open_pdi_page(indoc, pageno, "");
if (page == -1)
throw new Exception("Error: " + p.get_errmsg());
/* Page size may be adjusted by fit_pdi_page() */
p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
/*
* Place the imported page on the output page, and adjust the
* page size.
*/
p.fit_pdi_page(page, 0, 0, "adjustpage");
/*
* Load the font for the stamp.
*/
font = p.load_font("NotoSerif-Regular", "unicode", "");
if (font == -1)
throw new Exception("Error: " + p.get_errmsg());
/* Place the stamp, filled with the pattern color */
p.setcolor("fill", "pattern", pattern, 0, 0, 0);
p.fit_textline("PUBLISHED", 20, 20, "font=" + font
+ " fontsize=1 boxsize={550 800} stamp=ll2ur");
p.close_pdi_page(page);
p.end_page_ext("");
}
p.end_document("");
}
catch (PDFlibException e) {
System.err.println("PDFlib exception occurred:");
System.err.println("[" + e.get_errnum() + "] " + e.get_apiname() +
": " + e.get_errmsg());
exitcode = 1;
}
catch (Exception e) {
System.err.println(e);
exitcode = 1;
}
finally {
if (p != null) {
p.delete();
}
System.exit(exitcode);
}
}
}