Modernisation Oracle Forms

Destination plugin de post-traitement PDF Concaténer un fichier PDF et appliquer un filigrane à la sortie PDF

S'APPLIQUE À : Oracle Reports Developer,

Oracle Reports Developer - versions 9.0.4.0 à 10.1.2.0.0 [Release Oracle9i à 10gr2]
Les informations de ce document s'appliquent à toutes les plateformes.
Pertinence vérifiée le 10-déc-2012

Sortie PDF d'Oracle Reports.

OBJECTIF : ajouter un fichier PDF à la sortie de Reports

Cette destination enfichable permet d'ajouter un fichier PDF à la sortie de Reports.
Elle applique aussi un filigrane au fichier obtenu et numérote les pages du fichier final.

PRÉREQUIS : bibliothèque PDF iText

Cet utilitaire utilise la bibliothèque PDF tierce iText.

CONFIGURATION

1) Téléchargez itext-1.3.jar à l'adresse

http://prdownloads.sourceforge.net/itext/itext-1.3.jar

Ajoutez-le au REPORTS_CLASSPATH.

2) Ajoutez pdfutils.zip au REPORTS_CLASSPATH. Ce fichier zip peut être extrait du fichier joint pdfcontact.zipp (changez d'abord l'extension du fichier de "zipp" à "zip") ou être construit à partir des sources Java fournies ci-dessous.

3) Ajoutez une nouvelle destination dans le fichier de configuration du Reports Server.

<destination destype="pdfconcat" class="pdfutils.PdfConcat"> 
</destination>

INSTRUCTIONS

Exécutez le rapport avec la syntaxe suivante

rwclient server=pdfconcat report=test.rdf destype=pdfconcat desname=;;; desformat=pdf 


Exemple

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

MISE EN GARDE : soutien Oracle

Cet exemple de code est fourni à des fins éducatives seulement et n'est pas pris en charge par le soutien Oracle. Il a été testé à l'interne, mais nous ne garantissons pas qu'il fonctionnera chez vous. Assurez-vous de l'exécuter dans votre environnement de test avant de l'utiliser.

EXEMPLE DE 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){}*/

}
}

*****************************************