Chris Wilson wrote:
The text I need to add needs to be in a cursive (handwriting) font, I'm told. I don't think any of the standard 14 fonts will do.

No, the standard 14 fonts won't do.

I've read the PdfStamper example and used it to add the correct text in the correct place, but I can't seem to make it use my desired (local) font. I downloaded a free font for this purpose, and I know it can be embedded because the UsingFontFactory example works and shows the font correctly.

It would help if we knew what type of font it is.
Can you embed the font in a Hello World document
that is generated from scratch?

I'm not sure it's even possible to embed a font using PdfStamper to access the PdfContentByte objects with getOverContent

I read in the mailing list archive a post from 2003 which says that iText does not allow embedding new fonts in an existing document? Please tell me that this isn't the case any more?

Meanwhile you can add text using an embedded font
with PdfStamper.

I don't like to guess what's going wrong, so instead
I send you a code sample that shows how I added text
to an existing PDF document. I found this pseudo
handwritten font VinerHandITC on my system; replace
the reference to this font with the path to the font
you are using.
br,
Bruno

Attachment: without_handwritten_font.pdf
Description: Adobe PDF document

Attachment: with_handwritten_font.pdf
Description: Adobe PDF document

package test;

import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;

/**
 * This example was written by Bruno Lowagie. It is part of the book 'iText in
 * Action' by Manning Publications. 
 * ISBN: 1932394796
 * http://itext.ugent.be/itext-in-action/ 
 * http://www.manning.com/lowagie/
 */

public class StampTextEmbeddedFont {

        public static void main(String[] args) {
                createPdf("without_handwritten_font.pdf");
                try {
                        PdfReader reader = new 
PdfReader("without_handwritten_font.pdf");
                        PdfStamper stamper = new PdfStamper(reader, new 
FileOutputStream("with_handwritten_font.pdf"));
                        PdfContentByte cb = stamper.getOverContent(1);
                        BaseFont bf = BaseFont.createFont(
                                        "c:/windows/fonts/vineritc.ttf", 
BaseFont.WINANSI,
                                        BaseFont.EMBEDDED);
                        cb.beginText();
                        cb.setFontAndSize(bf, 12);
                        cb.showTextAligned(Element.ALIGN_LEFT, 
"abcdefghijklmnopqrstuvwxyz", 36, 770, 0);
                        cb.endText();
                        stamper.close();
                } catch (IOException e) {
                        e.printStackTrace();
                } catch (DocumentException e) {
                        e.printStackTrace();
                }
        }
        
        public static void createPdf(String filename) {
                // step 1
                Document document = new Document();
                try {
                        // step 2
                        PdfWriter.getInstance(document, new 
FileOutputStream(filename));
                        // step 3: we open the document
                        document.open();
                        // step 4:
                        document.add(new 
Paragraph("abcdefghijklmnopqrstuvwxyz"));
                } catch (DocumentException de) {
                        System.err.println(de.getMessage());
                } catch (IOException ioe) {
                        System.err.println(ioe.getMessage());
                }
                // step 5: we close the document
                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/

Reply via email to