PDF Post Processing Plugin Destination - Concatenating PDF File and Applying Watermark to PDF Output
PDF Post Processing Plugin Destination - Concatenating PDF File and Applying Watermark to PDF Output
Titleimage
Posted by Patrick Hamou on 2017:09:19 22:36:14
APPLIES TO: Oracle Reports Developer,
Oracle Reports Developer - Version 9.0.4.0 to 10.1.2.0.0 [Release Oracle9i to 10gr2]
Information in this document applies to any platform.
Checked for relevance on 10-Dec-2012
Oracle Reports PDF output.
PURPOSE: PDF file to Reports output
This pluggable destination provides the functionality to append a PDF file to Reports output.
It also applies a watermark to the resulting file and assigns page numbers to the final file.
REQUIREMENTS: PDF library, iText
This utility uses third party PDF library from iText.
CONFIGURING
1)Download itext-1.3.jar from
http://prdownloads.sourceforge.net/itext/itext-1.3.jar
Include it in REPORTS_CLASSPATH.
2)Include pdfutils.zip in REPORTS_CLASSPATH. This zip file can be extracted from the attached file pdfcontact.zipp (first the extension of the file should be changed from "zipp" to "zip") or can be built from the java sources provided below.
3)Include a new destination in the Reports Server configuration file.
<destination destype="pdfconcat" class="pdfutils.PdfConcat">
</destination>
INSTRUCTIONS
Run the report using this syntax
rwclient server=pdfconcat report=test.rdf destype=pdfconcat desname=;;; desformat=pdf
Example
rwclient server=pdfconcat report=test.rdf destype=pdfconcat desname=D:\ids10g\bin\hello.pdf;D:\ids10g\bin\tt.pdf;d:\ids10g\bin\watermark.jpg;D:\ids10g\bin\final.pdf desformat=pdf
CAUTION: Oracle Support
This sample code is provided for educational purposes only, and is not supported by Oracle Support. It has been tested internally, however, we do not guarantee that it will work for you. Ensure that you run it in your test environment before using.
SAMPLE CODE
Concatenate.java
******************************************
/*iText Library - Copyright (C) 1999-2005 by Bruno Lowagie and Paulo Soares. All Rights Reserved.
*http://www.lowagie.com/iText */
package pdfutils;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PRAcroForm;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.SimpleBookmark;
/**
* Tool that can be used to concatenate existing PDF files.
*/
public class Concatenate {
/**
* This class can be used to concatenate existing PDF files.
* (This was an example known as PdfCopy.java)
* @param args the command line arguments
*/
public static void concatenate(String args[]) {
if (args.length < 2) {
System.err.println("arguments: file1 [file2 ...] destfile");
}
else {
System.out.println("PdfCopy example");
try {
int pageOffset = 0;
ArrayList master = new ArrayList();
int f = 0;
String outFile = args[args.length-1];
Document document = null;
PdfCopy writer = null;
while (f < args.length-1) {
// we create a reader for a certain document
PdfReader reader = new PdfReader(args[f]);
reader.consolidateNamedDestinations();
// we retrieve the total number of pages
int n = reader.getNumberOfPages();
List bookmarks = SimpleBookmark.getBookmark(reader);
if (bookmarks != null) {
if (pageOffset != 0)
SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null);
master.addAll(bookmarks);
}
pageOffset += n;
if (f == 0) {
// step 1: creation of a document-object
document = new Document(reader.getPageSizeWithRotation(1));
// step 2: we create a writer that listens to the document
writer = new PdfCopy(document, new FileOutputStream(outFile));
// step 3: we open the document
document.open();
}
// step 4: we add content
PdfImportedPage page;
for (int i = 0; i < n; ) {
++i;
page = writer.getImportedPage(reader, i);
writer.addPage(page);
}
PRAcroForm form = reader.getAcroForm();
if (form != null)
writer.copyAcroForm(reader);
f++;
}
if (master.size() > 0)
writer.setOutlines(master);
// step 5: we close the document
document.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
}
******************************************
AddWatermarkPageNumbers.java
*******************************************
/*iText Library - Copyright (C) 1999-2005 by Bruno Lowagie and Paulo Soares. All Rights Reserved.
*http://www.lowagie.com/iText */
package pdfutils;
import java.io.FileOutputStream;
import java.util.HashMap;
import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
/**
* Reads the pages of an existing PDF file, adds pagenumbers and a watermark.
*/
public class AddWatermarkPageNumbers {
/**
* Reads the pages of an existing PDF file, adds pagenumbers and a watermark.
* @param args no arguments needed
*/
public static void pagenum(String infile, String outfile, String watermark) {
System.out.println("Add watermarks and pagenumbers");
try {
// we create a reader for a certain document
PdfReader reader = new PdfReader(infile);
int n = reader.getNumberOfPages();
// we create a stamper that will copy the document to a new file
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outfile));
// adding some metadata
HashMap moreInfo = new HashMap();
stamp.setMoreInfo(moreInfo);
// adding content to each page
int i = 0;
PdfContentByte under;
PdfContentByte over;
Image img = Image.getInstance(watermark);
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
img.setAbsolutePosition(200, 400);
while (i < n) {
i++;
// watermark under the existing page
under = stamp.getUnderContent(i);
under.addImage(img);
}
stamp.close();
}
catch (Exception de) {
de.printStackTrace();
}
}
}
*******************************************
PdfConcat.java
**********************************************
/*iText Library - Copyright (C) 1999-2005 by Bruno Lowagie and Paulo Soares. All Rights Reserved.
*http://www.lowagie.com/iText */
/*
* Oracle Reports System Development Kit samples.
* Copyright Oracle Corporation.
* 500 Oracle Parkway, Redwood Shores, California, 94065, U.S.A.
* All rights reserved.
* Note: The API's used here are subject to change in future releases.
*
* These samples include useful utilities that you can use as-is or
* use as a basis for your own extension.
* These addins are sample code that is not part of the core product
* and as such are provided.
*
* Please post questions on Oracle Technology Network (OTN) at
* http://otn.oracle.com on the Reports Discussion Forum.
*
*/
package pdfutils;
import java.io.File;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.Properties;
import oracle.reports.JobConstants;
import oracle.reports.RWException;
import oracle.reports.server.Destination;
import oracle.reports.utility.Utility;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PRAcroForm;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.SimpleBookmark;
import java.io.*;
public class PdfConcat extends Destination
{
/**
* Implementation for init of the destination class.
* Do anything in here that needs to occur when
* OracleAS Reports Services starts up.
*/
/** reports parameters **/
public String concatfile = null;
public String finalfile = null;
public String appendfile = null;
public String finalfile1 = null;
public String watermarkfile = null;
public static void init(Properties destProps) throws RWException
{
//
}
public static void concat(String concatfile,String cachefilename,String appendfile,String finalfile)
{
String[] ar = new String[4];
ar[0]=concatfile;
ar[1]=cachefilename;
ar[2]=appendfile;
ar[3]=finalfile;
Concatenate.concatenate(ar);
}
/**
* Implementation for shutdown of this destination class.
* Do anything in here that needs to occur when
* OracleAS Reports Services shuts down.
*/
public static void shutdown()
{
//
}
/**
* Start method is invoked before sending first file to the
* destination for the current job. Can be overrided by subclass if necessary
* @param props properties for sending output to the destination
* @param desname destination name
* @param totalFile total number of files that need distributing
* @param totalSize total size of all the files to distributes
* @param mainFormat format for main output file (HTML, PDF, etc.)
* @return true - send each file
* false - don't send
*/
public boolean start(
Properties props,
String desname,
int totalFile,
long totalSize,
short mainFormat
) throws RWException
{
/*
String s1= desname;
String s2=s1.substring(0,s1.indexOf(';'));
concatfile = s2;
s1=s1.substring(s2.length() +1);
finalfile = s1;
try {FileWriter writer=new FileWriter("d:\\ids10g\\bin\\cat.txt");
writer.write(appendfile);
writer.write(concatfile);
writer.write(finalfile);
writer.close();
}
catch(Exception e){} */
/*String s1= desname;
String s2=s1.substring(0,s1.indexOf(';'));
concatfile = s2;
s1=s1.substring(s2.length() +1);
s2=s1.substring(0,s1.indexOf(';'));
appendfile = s2;
s1=s1.substring(s2.length() +1);
finalfile1 = s1;
finalfile = "temp" + finalfile;*/
String s1= desname;
String s2=s1.substring(0,s1.indexOf(';'));
concatfile = s2;
s1=s1.substring(s2.length() +1);
s2=s1.substring(0,s1.indexOf(';'));
appendfile = s2;
s1=s1.substring(s2.length() +1);
s2=s1.substring(0,s1.indexOf(';'));
watermarkfile = s2;
s1=s1.substring(s2.length() +1);
finalfile1 = s1;
finalfile = "temp" + finalfile;
return true;
}
/**
* Stop method is invoked after sending the last file to the
* destination for the current job. Can be overrided by subclass if necessary.
*/
public void stop() throws RWException
{
//
}
/**
* Send a file to the destination
* @param main true if file is the main file of the job (eg with html, there may be images which are considered subfiles)
* @param fileName file name with path
* @param fileFormat file format
* @param fileSize file size
*/
public void sendFile(
boolean main,
String fileName,
short fileFormat,
long fileSize
) throws RWException
{
String cachefilename= fileName;
concat(concatfile,cachefilename,appendfile,finalfile);
AddWatermarkPageNumbers.pagenum(finalfile,finalfile1,watermarkfile);
/* try {FileWriter writer=new FileWriter("d:\\ids10g\\bin\\cat.txt");
writer.write(cachefilename);
writer.write(concatfile);
writer.write(finalfile);
writer.close();
}
catch(Exception e){}*/
}
}
*****************************************
Posted by Patrick Hamou on 2017:09:19 22:36:14