Wasserzeichen und Kopfzeilen im jadice web toolkit
Dieser Artikel beschreibt, wie im jadice web toolkit vor dem Downlad Seitenmodifikationen durchgeführt werden können.
In diesem Artikel wird gezeigt, wie Dokumente vor der Bereistellung als PDF-Datei mit einem Wasserzeichen versehen werden können. Die Erzeugung und Einbindung des Wasserzeichens ist im Artikel Wasserzeichen und Kopfzeilen mit dem ContentsCreatorPageSegment detailliert beschrieben.
Anforderung
Ein im Dokument das im jadice web toolkit angezeigt wird soll vor dem Download als PDF-Datei mit einem Wasserzeichen versehen werden. Als Wasserzeichen soll der Text "Entwurf" schräg über alle Seiten eingefügt werden.
Lösung
Zur Bereitstellung des Dokument als PDF-Datei wird die Klasse AbstractPDFStreamPrintingServerOperation erweitert.
In der überschriebenen Methode doPrint() wird auf jeder Seite des Dokuments ein Wasserzeichen aufgebracht und die manipulierte Seite in den StreamPrinter als Antwort geschrieben.
Anschließend wird das erzeugte Dokument in eine temporäre Datei geschrieben und zum Download zur Verfügung gestellt.
Implementierung
Die verwendete Klasse WatermarkExample und der darin referenzierte FontProvider könnnen rechts heruntergeladen werden.
Dieses Beispiel kann auch direkt im Showcase ausprobiert werden: https://webtoolkit.jadice.com/showcase/index.html?connection=longpoll#!DecorationPrintingServerOperationExample
package org.jadice.examples;
import java.io.File;
import java.io.FileOutputStream;
import com.jadice.web.print.stream.server.AbstractPDFStreamPrintingServerOperation;
import com.jadice.web.print.stream.server.printer.StreamPrinter;
import com.levigo.jadice.document.Document;
import com.levigo.jadice.document.DocumentLayer;
import com.levigo.jadice.document.Page;
import com.levigo.jadice.printer.PrintDecorations;
import com.levigo.jadice.web.demo.common.server.print.PrintStreamMeta;
import com.levigo.jadice.web.demo.common.server.print.PrintStreamRepository;
import com.levigo.jadice.web.demo.common.shared.print.StreamPrintingCompletedMessage;
import com.levigo.jadice.web.demo.showcase.shared.DecorationPDFPrintOperationParameters;
import com.levigo.jadice.web.shared.model.serveroperation.ServerOperationMessage;
/**
* A class which shows how page modifications can be made before offering them to be downloaded.
*/
public class DecorationPDFPrintOperation
extends
AbstractPDFStreamPrintingServerOperation<DecorationPDFPrintOperationParameters, ServerOperationMessage> {
// The repository where the created file is registered to allow clients to download it.
private final PrintStreamRepository registry;
/**
* Creates a new {@link DecorationPDFPrintOperation} which will register the created documents at
* the given {@link PrintStreamRepository}
*
* @param registry the {@link PrintStreamRepository} where created documents shall be registered
* so they can be downloaded.
*/
public DecorationPDFPrintOperation(PrintStreamRepository registry) {
this.registry = registry;
}
@Override
protected void doPrint(Request request, ResponseChannel responseChannel, StreamPrinter streamPrinter)
throws Exception {
// Get the document to add the watermark to from the request
Document document = request.getDocument();
// Elements in the layer POSTDECORATION cover document contents
DocumentLayer targetLayer = PrintDecorations.POSTDECORATION;
// Iterate over all pages of the document
for (Page p : document.getPages()) {
// Create a pageSegment containing the text "DRAFT" as big as the page. The watermark is
// created for each page in case the document contains pages with different dimensions
p.add(targetLayer, WatermarkHelper.createWatermarkPageSegment("DRAFT", p.getSize()));
// add the page to the streamPrinter
streamPrinter.getSourcePages().add(p);
}
// create a temporary file to which the
final File file = File.createTempFile("print-stream-", ".pdf");
final FileOutputStream output = new FileOutputStream(file);
// write the document to the temporary file
streamPrinter.print(output);
output.close();
// Register the file with its metadata at the PrintStreamRepository to allow clients to download
// the file
final PrintStreamMeta meta = new PrintStreamMeta(file);
meta.setMimeType("application/pdf");
final String id = registry.register(meta);
// notify the client about the finished streamPrinting
responseChannel.send(new StreamPrintingCompletedMessage(id));
}
}