Instantly share code, notes, and snippets.

Embed
What would you like to do?
This sample will read a PDF file that contains Stamps looking for one with a specific name and then apply it to a PDF file in the same way that Acrobat does.
/*
* Copyright Datalogics, Inc. 2015
*/
package pdfjt.cookbook.stamps;
import com.adobe.internal.io.ByteReader;
import com.adobe.internal.io.ByteWriter;
import com.adobe.internal.io.InputStreamByteReader;
import com.adobe.pdfjt.pdf.document.PDFDocument;
import com.adobe.pdfjt.pdf.document.PDFOpenOptions;
import com.adobe.pdfjt.pdf.document.PDFSaveFullOptions;
import com.adobe.pdfjt.pdf.graphics.xobject.PDFXObjectForm;
import com.adobe.pdfjt.pdf.interactive.annotation.PDFAnnotationStamp;
import com.adobe.pdfjt.pdf.interactive.annotation.PDFAppearance;
import com.adobe.pdfjt.pdf.page.PDFPage;
import com.adobe.pdfjt.services.manipulations.PMMTemplates;
import com.adobe.pdfjt.services.xobjhandler.PageContentXObject;
import com.adobe.pdfjt.services.xobjhandler.XObjectContentType;
import com.adobe.pdfjt.services.xobjhandler.XObjectUseOptions;
import java.io.InputStream;
import java.net.URL;
import pdfjt.util.SampleFileServices;
/**
* This sample will read a PDF file that contains Stamps looking for one with a
* specific name and then apply it to a PDF file in the same way that Acrobat
* does.
*/
public class UsingAcrobatStamps {
private static final String inputPDF = "http://dev.datalogics.com/cookbook/stamps/BlankInput.pdf";
private static final String standardStampsPDF = "http://dev.datalogics.com/cookbook/stamps/StandardBusiness.pdf";
private static final String outputDir = "cookbook/Stamps/output/";
static public void main(String[] args) throws Exception {
try {
/*
* Start by reading in the two input files
*/
InputStream fis = new URL(inputPDF).openStream();
ByteReader byteReader = new InputStreamByteReader(fis);
PDFDocument pdfDocument = PDFDocument.newInstance(byteReader, PDFOpenOptions.newInstance());
fis = new URL(standardStampsPDF).openStream();
byteReader = new InputStreamByteReader(fis);
PDFDocument stampsPDF = PDFDocument.newInstance(byteReader, PDFOpenOptions.newInstance());
// Get the first page of the PDF that we want to add the stamp to.
PDFPage pageOne = pdfDocument.requirePages().getPage(0);
/*
* Get the page with the template name that matches the
* "Confidential" stamp in the Acrobat UI.
*/
PMMTemplates pmmTemplates = new PMMTemplates(stampsPDF);
PDFPage confidentialStampPage = pmmTemplates.templateExistsForName(stampsPDF, "SBConfidential=Confidential");
/*
* We are going to use pages from the "StandardBusiness.pdf" that
* ships with Adobe Acrobat as the Stamp Appearance. We can pull
* content from other PDF files by encapsulating the page content
* stream (with the associated Resources dictionary) in an XObject.
* For Stamps we don't need any special kind of XObject type so we
* just use "General".
*/
XObjectUseOptions useOptions = new XObjectUseOptions();
XObjectContentType xObjectContentType = XObjectContentType.General;
useOptions.setContentType(xObjectContentType);
// Create the Stamp Annotation Object.
PDFAnnotationStamp pdfAnnotationStamp = PDFAnnotationStamp.newInstance(pdfDocument);
PDFAppearance pdfAppearance = PDFAppearance.newInstance(pdfDocument);
/*
* Create an XObject from the page that has the stamp that we want,
* and then set it as the normal appearance for the stamp
* annotation.
*/
PDFXObjectForm approvedStampXObjectForm = PageContentXObject.generateContentXObject(pdfDocument, confidentialStampPage, useOptions);
pdfAppearance.setNormalAppearance(approvedStampXObjectForm);
pdfAnnotationStamp.setAppearance(pdfAppearance);
/*
* Add a Title and Content for the Stamp Pop-Up. These strings will
* appear in the Acrobat Comments panel and when you roll over the
* Stamp.
*/
pdfAnnotationStamp.setTitle("Datalogics");
pdfAnnotationStamp.setContents("My Confidential Stamp");
/*
* Set the location of the Stamp to be one inch from the top, left
* of the page.
*/
double stampWidth = approvedStampXObjectForm.getBBox().width();
double stampHeight = approvedStampXObjectForm.getBBox().height();
double llx = 72;
double lly = (pageOne.getCropBox().height() - 72 - stampHeight);
double urx = 72 + stampWidth;
double ury = pageOne.getCropBox().height() - 72;
pdfAnnotationStamp.setRect(llx, lly, urx, ury);
// Add the Stamp Annotation to the page
pageOne.addAnnotation(pdfAnnotationStamp);
// Save the file.
SampleFileServices.createDir(outputDir);
ByteWriter outputFile = SampleFileServices.getRAFByteWriter(outputDir + "StampedDocument.pdf");
pdfDocument.save(outputFile, PDFSaveFullOptions.newInstance());
} finally {
//
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment