Thanks! that worked.


Mat Gessel wrote:

At 07:39 AM 8/2/2004, Joris Verschoor wrote:

Hi,

I'd like to know how I can reuse a font that is used in a template pdf (after adding the PdfImportedPage)



Based on a previous reply from Paulo you have to use PdfStamper. I was able to get it to work using the following code.


-= Mat

void copyTemplateAndWriteText()
{
    // copy template
    PdfReader reader = new PdfReader(TEMPLATE_FILE.getAbsolutePath());
    PdfStamper stamper = new PdfStamper(reader, OUTPUT_STREAM);

    // add new text
    PdfContentByte content = stamper.getOverContent(PAGE_NUM);
    BaseFont font = findfont(FONT_NAME, reader, PAGE_NUM);
    assert font != null; "font not found in template: " + FONT_NAME;
    content.beginText();
    content.setFontAndSize(font, TYPESIZE);
    content.setTextMatrix(X, Y);
    content.showText(TEXT);
    content.endText();
}

static BaseFont findfont(String fontName, PdfReader reader, int pageNum)
{
BaseFont result = null;
PdfDictionary pages = (PdfDictionary) PdfReader.getPdfObject(reader.getCatalog().get(PdfName.PAGES));
PdfArray kids = (PdfArray) PdfReader.getPdfObject(pages.get(PdfName.KIDS));
PdfDictionary pageDictionary = (PdfDictionary) PdfReader.getPdfObject((PdfObject) kids.getArrayList().get(pageNum - 1));
PdfDictionary pageResourcesDict = (PdfDictionary) PdfReader.getPdfObject(pageDictionary.get(PdfName.RESOURCES));
PdfDictionary pageFontsDict = (PdfDictionary) PdfReader.getPdfObject(pageResourcesDict.get(PdfName.FONT));
if (pageFontsDict != null)
{
Set pageFontNames = pageFontsDict.getKeys();
for (Iterator iter = pageFontNames.iterator(); iter.hasNext();)
{
PdfName internalFontName = (PdfName) iter.next();
PdfDictionary fontDict = (PdfDictionary) PdfReader.getPdfObject(pageFontsDict.get(internalFontName));
PdfName baseFontName = (PdfName) PdfReader.getPdfObject(fontDict.get(PdfName.BASEFONT));
// compare names, ignoring the initial '/' in the baseFontName
if (fontName.regionMatches(0, baseFontName.toString(), 1, fontName.length()))
{
PRIndirectReference iRef = (PRIndirectReference) pageFontsDict.get(internalFontName);
if (iRef != null)
{
result = BaseFont.createFont(iRef);
}
}
}
}
return result;
}




-------------------------------------------------------
This SF.Net email is sponsored by OSTG. Have you noticed the changes on
Linux.com, ITManagersJournal and NewsForge in the past few weeks? Now,
one more big change to announce. We are now OSTG- Open Source Technology
Group. Come see the changes on the new OSTG site. www.ostg.com
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions





------------------------------------------------------- This SF.Net email is sponsored by OSTG. Have you noticed the changes on Linux.com, ITManagersJournal and NewsForge in the past few weeks? Now, one more big change to announce. We are now OSTG- Open Source Technology Group. Come see the changes on the new OSTG site. www.ostg.com _______________________________________________ iText-questions mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to