Eric Summkeller wrote:
Sorry but I need your help again!
The reason why I am reluctant to help is because what you are
trying to achieve is generally considered to be of very bad taste.
However, you'll find an example in attachment where I created
a page saying "page 1 of 10" with "10" added as a PdfTemplate.
In this (VERY SIMPLE) example, I assume this template is the
only template in the page. I get the content stream of the
template, I alter it (TO THE OTHER SUBSCRIBERS: PLEASE DON'T
DO THIS UNLESS YOU'RE DESPERATE), and then replace the original
content stream in the PdfTemplate. The end result is a page
saying "page 1 of 11".
This example took some shortcuts, IT IS NOT MEANT TO USE IN
A PRODUCTION ENVIRONMENT.
br,
Bruno
package test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PRStream;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfObject;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
public class ReplacePdfTemplate {
public static void main(String[] args) {
try {
createPdf("pdf_with_pdftemplate.pdf");
PdfReader reader = new PdfReader("pdf_with_pdftemplate.pdf");
PdfDictionary pagedict = reader.getPageN(1);
PdfDictionary resources = (PdfDictionary)pagedict.get(PdfName.RESOURCES);
PdfDictionary xobjects = (PdfDictionary)resources.get(PdfName.XOBJECT);
PdfObject xobject = null;
for (Iterator i = xobjects.getKeys().iterator(); i.hasNext(); ) {
xobject = xobjects.get((PdfName)i.next());
}
PRStream template = (PRStream)PdfReader.getPdfObject(xobject);
String oldtemplate = new String(PdfReader.getStreamBytes(template));
int pos = oldtemplate.indexOf("(10)");
String newtemplate = oldtemplate.substring(0, pos);
newtemplate += "(11)";
newtemplate += oldtemplate.substring(pos + 4);
template.setData(newtemplate.getBytes());
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("pdf_with_altered_pdftemplate.pdf"));
stamper.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void createPdf(String filename) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(
document, new FileOutputStream(filename));
document.open();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
PdfContentByte directcontent = writer.getDirectContent();
PdfTemplate template = directcontent.createTemplate(20, 20);
template.beginText();
template.setFontAndSize(bf, 12);
template.showTextAligned(Element.ALIGN_LEFT, "10", 0, 0, 0);
template.endText();
directcontent.beginText();
directcontent.setFontAndSize(bf, 12);
directcontent.showTextAligned(Element.ALIGN_RIGHT, "Page 1 of ", 200, 800, 0);
directcontent.endText();
directcontent.addTemplate(template, 200, 800);
document.close();
}
}
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/