を
PDFontTools.getFonts(doc)
を使用すると、フォントを列挙することができ、その後、「CreateFormの」方法でそれらのいずれかを使用DocumentFont
です。それらを直接作成することはできません。コンストラクタはパッケージプライベートですが、BaseFont.createFont(PRIndirectReference)
がそのトリックを行います。
だから、あなたが望むフォントにPRIndirectReference
を取得する必要があります。 "PR"はPdfReaderから来ました。
1)/Type /Font
でないものをすべて削除してから、PRStream
でないものすべてを除外して、すべてのオブジェクトをPdfReaderで列挙します。正しい名前のフォントを探しています。
public PRIndirectReference findNamedFont(PdfReader myReader, String desiredFontName) {
int objNum = 0;
PdfObject curObj;
do {
//The "Release" version doesn't keep a reference
//to the object so it can be GC'd later. Quite Handy
//when dealing with Really Big PDFs.
curObj = myReader.getPdfObjectRelease(objNum++);
if (curObj instanceof PRStream) {
PRStream stream = (PRStream)curObj;
PdfName type = stream.getAsName(PdfName.TYPE);
if (PdfName.FONT.equals(type)) {
PdfString fontName = stream.getAsString(PdfName.BASEFONT);
if (desiredFontName.equals(fontName.toString())) {
return curObj.getIndRef();
}
}
}
} while (curObj != null);
return null;
}
2)が正しい名前を持つフォントを探して、あなたのページのリソースディクショナリに/Font <<>>
dictsを調べます。 XObjectフォームリソースはあなたがチェックする必要があります自分自身のリソースを持っていることに留意してください:
public PRIndirectReference findFontInPage(PdfReader reader, String desiredName, int i) {
PdfDictionary page = reader.getPageN(i);
return findFontInResources(page.getAsDict(PdfName.RESOURCES), desiredName);
}
public PRIndirectReference findFontInResources(PdfDictionary resources, String desiredName) {
if (resources != null) {
PdfDictionary fonts = resources.getAsDict(PdfName.FONTS);
if (fonts != null) {
for (PdfName curFontName : fonts.keySet()) {
PRStream curFont (PRStream)= fonts.getAsStream(curFontName);
if (desiredName.equals(curFont.getAsString(PdfName.BASEFONT).toString()) {
return (PRIndirectReference) curFont.getIndirectReference();
}
}
}
PdfDictionary xobjs = resources.getAsDict(PdfName.XOBJECTS);
if (xobjs != null) {
for (PdfName curXObjName : xobjs.keySet()) {
PRStream curXObj = (PRStream)xobjs.getAsStream(curXObjName);
if (curXObj != null && PdfName.FORM.equals(curXObj.getAsName(PdfName.SUBTYPE)) {
PdfDictionary resources = curXObj.getAsDict(PdfName.RESOURCES);
PRIndirectReference ref = findFontInResources(resources, desiredName);
if (ref != null) {
return ref;
}
}
}
}
}
return null;
}
それらのいずれかは、あなたのPRIndirectReference
あなたは後にしているを取得します。その後、BaseFont.createFont(myPRRef)
に電話すると、DocumentFont
が必要になります。最初の方法はPDF内の任意のフォントを検索し、2番目の方法は実際に使用されるフォントのみを検索します。
また、サブセット化されたフォントは、フォント名の前に「6-random-letters-plus-sign」タグが付加されています。フォントサブセットを使用しないでください。使用している文字がサブセットにない場合があり、「arry ole」という問題につながります。それは素晴らしく、汚れているように聞こえますが、それは実際に私たちの販売員の名前でした: "Harry Vole"は大文字を欠いていました。
PS:フォームフィールドで使用する予定のフォントのサブセットを埋め込むことはありません。いいえブエノ。
通常、「ここの回答ボックスにすべてのコードを書いています」という免責事項が適用されますが、この種のコードはたくさん書かれていますので、そのまま使えます。あなたの指を渡す。 ;)
お返事ありがとうございます! iTextソリューションには何かありますか? – alexyz78
申し訳ありません - 私はiTextに精通していません。 ; '民間PRIndirectReference getFontIndirectRef(PdfReader pdfReader、文字列desiredFontName){ PRIndirectReferenceフォント= NULL:iTextの5.0.2を使用して – mtraut