thanks for the hint, but how do I actually do this? I didn't find a way to add the meta information while keeping my bookmarks. Attached is my current code, which (of course) doesn't work :-(
Stephan
Paulo Soares wrote:
An equivalent to document.addCreator() can be done easily but not
watermarks. Well, to be fair, watermarks can be done but you must know the
pdf specs very well, it's not already done.
Best Regards,
Paulo Soares
----- Original Message -----
From: "Stephan Wiesner" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, December 25, 2002 9:28
Subject: Re: FW: [iText-questions] DocBook, FOP, Itext leads to no links and
no Bookmarks
Hi Paulo,
that doesn't really help me either. The encryption works and my
bookmarks are kept now, but I need to set a watermarkfile,
document.addCreator() and the like. How can I combine that? Or is this
really not possible?
Stephan
-----Original Message-----and
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]] On Behalf Of Paulo
Soares
Sent: Dienstag, 24. Dezember 2002 17:00
To: Stephan Wiesner; [EMAIL PROTECTED]
Subject: Re: [iText-questions] DocBook, FOP, Itext leads to no links
no Bookmarks
There are ways to keep all the doc structure when encrypting. See
http://www.geocities.com/itextpdf/encrypt_pdf.zip. It can easily be
changed to have a modified info dictionarie but not to have watermarks.
Best Regards,
Paulo Soares
----- Original Message -----
From: "Stephan Wiesner" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, December 24, 2002 6:28
Subject: [iText-questions] DocBook, FOP, Itext leads to no links and no
Bookmarks
Hi list,
I transform DocBook documents, using FOP. The resulting PDF is then
secured, using iText. This works fine, except that iText destroys the
links in the document and the bookmarks are no longer displayed in the
Acrobat reader. I tried iText 0.94 and 0.96 and got the same result.
I have attached the method that is responsible for the securing of the
PDF.
Any suggestions?
Stephan
package de.fhnon.digipub.transform;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import com.lowagie.text.*;
import org.apache.fop.apps.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.stream.*;
import javax.xml.transform.sax.SAXResult;
import com.lowagie.text.pdf.*;
import org.w3c.dom.*;
import java.util.*;
import java.io.*;
import de.fhnon.digipub.tools.*;
/**
@version 0.1
*/
public class PDFSecurityManager implements Serializable
{
private String title = "";
private StringBuffer authors = new StringBuffer();
private StringBuffer keywords = new StringBuffer();
private StringBuffer subjects = new StringBuffer();
private boolean allowCopying = false;
private boolean allowPrinting = false;
private boolean hideToolbar = false;
private boolean hideMenuebar = false;
private String userPassword = "";
private String ownerPassword = "";
private java.util.logging.Logger logger = LoggerInitiator.getLogger();
private String watermarkFile = null;
private Transformer transformer = null;
private Driver driver = null;
private SAXResult saxResult;
private ByteArrayOutputStream fopout;
private org.apache.avalon.framework.logger.Logger fopLogger = null;
public PDFSecurityManager()
{
logger.info("New PDFSecurityManager initialised");
}
public void setAllowPrinting(boolean allow)
{ this.allowPrinting = allow; }
public boolean getAllowPrinting() { return this.allowPrinting; }
public void setAllowCopying(boolean allow)
{
this.allowCopying = allow;
logger.info("AllowCopy:" + allow);
}
public boolean getAllowCopying() { return this.allowCopying; }
public void setHideToolbar(boolean hide)
{ this.hideToolbar = hide; }
public boolean getHideToolbar() { return this.hideToolbar; }
public void setHideMenuebar(boolean hide)
{ this.hideMenuebar = hide; }
public boolean getHideMenuebar() { return this.hideMenuebar; }
/** Sets the userpassword for the document.
@param password Can be an empty String (default)
*/
public void setUserPassword(String password)
{
this.userPassword = password;
logger.info("pass:" + password);
}
public String getUserPassword() { return this.userPassword; }
/**
Sets the ownerpassword for the document.
@param password The password can be null, in this case a random String
is generated.
*/
public void setOwnerPassword(String password)
{ this.ownerPassword = password; }
public String getOwnerPassword() { return this.ownerPassword; }
public void addAuthor(String name)
{
if (authors.length() > 1)
{ authors.append(", " + name.trim()); }
else { authors.append(name.trim()); }
}
public StringBuffer getAuthors() { return this.authors; }
public void setAuthors(StringBuffer authors)
{ this.authors = authors; }
public StringBuffer getKeywords() { return this.keywords; }
public void setKeywords(StringBuffer keywords)
{ this.keywords = keywords; }
public StringBuffer getSubjects() { return this.subjects; }
public void setSubjects(StringBuffer subjects)
{ this.subjects = subjects; }
/**
The path to the image which is used as a watermark. Set to null
if no longer needed.
*/
public void setWatermarkFile(String watermark)
{
this.watermarkFile = watermark;
logger.info("Watermark:" + watermark);
}
public String getWatermarkFile() { return this.watermarkFile; }
public void transform(byte[] pdfBytes, String outFile) throws Exception
{
FileOutputStream out = new FileOutputStream(outFile);
transform(pdfBytes, out);
}
public void transform(String pdfFile, String outFile) throws Exception
{
logger.info("Transforming:" + pdfFile + " to " + outFile);
FileOutputStream out = new FileOutputStream(outFile);
transform(pdfFile, out);
}
public void transform(String pdfFile, java.io.OutputStream out)
throws Exception
{
System.out.println("Entering transform");
logger.info("PDFFile:" + pdfFile + ", OutStream:" + out);
FileInputStream in = new FileInputStream(pdfFile);
BufferedInputStream inSt = new BufferedInputStream(in, 4096);
File f = new File(pdfFile);
long bytesSize = f.length();
byte[] bytes = new byte[(int)bytesSize + 1];
int read = 0;
while ( read != -1)
{ read = inSt.read(bytes); }
inSt.close();
transform(bytes, out);
}
public void transform(byte[] pdfBytes,
java.io.OutputStream out) throws Exception
{
logger.info("Starting to transform " + pdfBytes.length + " bytes to " + out);
PdfReader reader = new PdfReader(pdfBytes);
int allowances = 0;
if (allowCopying) { allowances += PdfWriter.AllowCopy; }
if (allowPrinting) { allowances += PdfWriter.AllowPrinting; }
logger.info("Allowances:" + allowances);
PdfEncryptor.encrypt(reader, out,
this.userPassword.getBytes(), this.ownerPassword.getBytes(),
allowances, true);
com.lowagie.text.Document document = new com.lowagie.text.Document(
reader.getPageSizeWithRotation(1));
PdfWriter writer = PdfWriter.getInstance(document, out);
int viewerPreferences = 0;
if (hideMenuebar) { viewerPreferences += PdfWriter.HideMenubar; }
if (hideToolbar) { viewerPreferences += PdfWriter.HideToolbar; }
writer.setViewerPreferences(viewerPreferences);
document.addCreator("digipub.fhnon.de");
document.addAuthor(authors.toString());
document.addKeywords(keywords.toString());
document.addTitle(title);
document.addSubject(subjects.toString());
if (this.watermarkFile != null)
{
if (!this.watermarkFile.equals(""))
{
Watermark watermark = new Watermark(
Image.getInstance(watermarkFile), 200, 420);
document.add(watermark);
}
}
document.open();
PdfContentByte cb = writer.getDirectContent();
// handling the pages
int rotation = 0;
int i = 0;
int n = reader.getNumberOfPages();
Rectangle rect = reader.getPageSizeWithRotation(1);
PdfImportedPage page = null;
while (i < n)
{
i++;
System.out.print("Processing page " + i);
document.setPageSize(reader.getPageSizeWithRotation(i));
document.newPage();
page = writer.getImportedPage(reader, i);
rotation = reader.getPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb.addTemplate(page, 0, -1f, 1f, 0, 0,
reader.getPageSizeWithRotation(i).height());
} else
{
cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
}
document.close();
logger.info("Finished PDF Metas");
out.close();
}
}
