Hello, I am trying to use iText for adding page numbers to the already existed PDF using PDFStamper class. I have problem. PDFs, which I create using Apache FOP 0.95, cause Exception as shown below, although they look OK in Adobe Acrobat Reader 8.1.2. I guess, that this is problem of missing or wrong encoding somewhere (but where?). Attached to this e-mail are: FO source, resulted PDF, Java code for iText. I am using Sun Java SE 1.6.0_10 on MS Windows 2000. Please, can You point me, where I am making miskate?
Thank You. Stepan
L:\Documents\Capitol\vypisyKv1\_other>L:\RunFiles\jdk\jre\bin\java -cp ".;iText-
2.1.3.jar" AddPageNumbersToExistingPageNumberPDF
ExceptionConverter: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequ
enceException: Invalid byte 1 of 1-byte UTF-8 sequence.
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(Unk
nown Source)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(Unknown So
urce)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(Unknown
Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.arrangeCapac
ity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipString(U
nknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineD
ocVersion(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(U
nknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(U
nknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown So
urce)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown So
urce)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unk
nown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at com.lowagie.text.xml.xmp.XmpReader.<init>(Unknown Source)
at com.lowagie.text.pdf.PdfStamperImp.close(Unknown Source)
at com.lowagie.text.pdf.PdfStamper.close(Unknown Source)
at AddPageNumbersToExistingPageNumberPDF.main(AddPageNumbersToExistingPa
geNumberPDF.java:30)
<?xml version="1.0" encoding="UTF-8"?> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <fo:layout-master-set> <fo:simple-page-master master-name="A4-portrait" page-height="29.7cm" page-width="21.0cm" margin="2cm"> <fo:region-body margin-top="6cm" /> <fo:region-before extent="6cm" /> <fo:region-after extent="1.5cm" /> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="A4-portrait" initial-page-number="1" force-page-count="no-force"> <fo:flow flow-name="xsl-region-body"> <fo:block font-family="Arial Narrow">příliš žluťoučký kůň úpěl ďábelské ódy</fo:block> <fo:block font-family="Arial Narrow">PŘÍLIŠ ŽLUŤOUČKÝ KŮŇ ÚPĚL ĎÁBELSKÉ ÓDY</fo:block> </fo:flow> </fo:page-sequence> </fo:root>
source.pdf
Description: Adobe PDF document
import java.io.FileOutputStream;
import com.lowagie.text.Element;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
public class AddPageNumbersToExistingPageNumberPDF {
public static void main(String[] args) {
try {
PdfReader reader = new PdfReader("source.pdf");
int n = reader.getNumberOfPages();
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("result.pdf"));
int i = 0;
PdfContentByte over;
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
while (i < n) {
i++;
over = stamp.getOverContent(i);
over.beginText();
over.setFontAndSize(bf, 18);
over.setTextMatrix(10, 10);
over.showText("page " + i + " from " + n + " pages");
over.endText();
}
stamp.close();
} catch (Exception de) {
de.printStackTrace();
}
}
}------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions Buy the iText book: http://www.1t3xt.com/docs/book.php
