pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Evan Williams <evan.willi...@zapprx.com>
Subject Re: Profound Sadness Trying To Embed Fonts For Form FIelds
Date Tue, 09 May 2017 14:54:06 GMT
Thank you Maruan!

Thank you for your workaround and confirmation of the bug (although it is a
quite obscure use case, but that is where the bugs hide I suppose).

I will open an issue.

I appreciate your help very much.

On Tue, May 9, 2017 at 10:15 AM, Maruan Sahyoun <sahyoun@fileaffairs.de>
wrote:

> Hi,
> > Am 09.05.2017 um 14:53 schrieb Evan Williams <evan.williams@zapprx.com>:
> >
> > Thank you Tilman,
> >
> > It turns out that I just sent the wrong file to you. The embedding issue
> is
> > very real.
> >
> > A form that actually shows the problem is here:
> >
> > https://dl.dropboxusercontent.com/u/25802656/CF-CVS-Revised.pdf
> >
> > The form fields are empty in this example but the font is not embedded
> and
> > the form will show no-glyph rectangles on any machine that doesn't have
> > 'Noto Sans' installed.
> >
> > I have been trying experiments and it seems like, for the code I wrote,
> if
> > you try to set a font ONLY for the AcroForm where there is no text using
> > the same font in the document, the font will not embed. This is either
> > because:
> >
> >   1. My code is wrong (likely)
> >   2. There is a PDFBox bug (unpossible!)
> >
> > I believe that I can hack around this by writing tiny white text
> somewhere
> > in the document using the font that I would like to use for the form, bur
> > that is, to use a technical term, crappy.
> >
> > What can you advise?
> >
>
> you need to at least set a value with a single character for a form field.
> After that you can set the value of the form field back.
>
> The font is only embedded if it's actually used - please open an issue at
> https://issues.apache.org/jira/browse/PDFBOX/ for that.
>
> BR
> Maruan
>
>
> >
> > On Fri, Apr 21, 2017 at 12:52 AM, Tilman Hausherr <THausherr@t-online.de
> >
> > wrote:
> >
> >> Is the file you uploaded the result before or after your operation?
> >> Because everything works nicely with Adobe. The font Noto Sans is
> embedded,
> >> I don't have it on my machine.
> >>
> >> If it is the result "before" or "with Adobe", then please upload the
> >> result "after PDFBox" (and if possible, screenshots of "own machine" and
> >> "other machine without the font") and if possible use an unusual font,
> and
> >> also try
> >>
> >>
> >> resFont = PDType0Font.load(doc, is, false);
> >>
> >>
> >> I haven't tried running your code yet.
> >>
> >> Tilman
> >>
> >>
> >>
> >>
> >> Am 20.04.2017 um 18:59 schrieb Evan Williams:
> >>
> >>> I am trying to change all of the fields in a PDF form to use a TrueType
> >>> font that I am embedding. Or trying to embed more accurately.
> >>>
> >>> I have had good success with embedding fonts went I am drawing text in
> a
> >>> document, but for form fields, I have been unable to programmatically
> >>> force
> >>> the font to embed.
> >>>
> >>> I load the font in the constructor like this:
> >>>
> >>> *        final PDFont formFont = loadFontResourceWithDef(pdf,
> >>> DEFAULT_F**O*
> >>> *NT_PATH,*
> >>> *                DEF_DOCUMENT_FONT);*
> >>> *        final PDResources resources = acroForm.getDefaultResources()
> ;*
> >>> *        final String fontName = resources.add(formFont).getName();*
> >>> *        acroForm.setDefaultResources(resources);*
> >>> *        this.defaultAppearanceString = "/" + fontName + " 0 Tf 0 g";*
> >>>
> >>> *    }*
> >>>
> >>> *    public static PDFont loadFontResourceWithDef(PDDocument doc,*
> >>> *            String fontpath, PDFont defFont) {*
> >>> *        PDType0Font resFont = null;*
> >>> *        try {*
> >>> *            final InputStream is =
> >>> this.getClass().getResourceAsStream(fontpath);*
> >>> *            if (is != null) {*
> >>> *                resFont = PDType0Font.load(doc, is);*
> >>> *            }*
> >>> *        } catch (IOException e) {*
> >>> *            log("Error loading font " + fontpath, e);*
> >>> *        }*
> >>> *        return resFont != null ? resFont : defFont;*
> >>> *    }*
> >>>
> >>> Presume that I have verified that the font actually is loaded into the
> >>> formFont variable.
> >>>
> >>> Some time later, I iterate over the fields in the acroform and, for
> each
> >>> field, do this:
> >>>
> >>> *            if (field instanceof PDTextField) {*
> >>> *                final PDTextField textfield = (PDTextField) field;*
> >>> *                textfield.setDefaultAppearance
> >>> (defaultAppearanceString);*
> >>> *            }*
> >>>
> >>> The result of this is that the appearances for the text fields are all
> >>> correct, but the font is never embedded, so it looks OK on my machine
> >>> (because the font that I am loading is available locally), but I get
> >>> charming empty rectangles on machines where the font is not present.
> If I
> >>> open the PDF in acrobat and manually change the font on one field and
> >>> change it back, the font them embeds and everything works fine.
> >>>
> >>> I have uploaded an example to
> >>> https://dl.dropboxusercontent.com/u/25802656/ofev_rx_3338.pdf
> >>>
> >>> Any thoughts would be helpful. I would rather not do a manual
> operation on
> >>> more than one hundred files.
> >>>
> >>> Thank you, you the always helpful and wonderful PDFBox community.
> >>>
> >>
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> >> For additional commands, e-mail: users-help@pdfbox.apache.org
> >>
> >>
> >
> >
> > --
> > *Evan Williams*
> > Principal Software Engineer
> > evan.williams@zapprx.com
> >
> > *www.ZappRx.com <http://www.zapprx.com/>*
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>


-- 
*Evan Williams*
Principal Software Engineer
evan.williams@zapprx.com

*www.ZappRx.com <http://www.zapprx.com/>*

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message