pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Vassallo, Fabio" <Fabio.Vassa...@wuerth-phoenix.com>
Subject RE: Embedded vs. non-embedded fonts (PDFBox & iText)
Date Fri, 14 Apr 2017 08:50:14 GMT
Thank you very much, Tilman.
Please, ignore my sentence about the empty file: I had an (unrelated) exception is another
point of my code. Sorry.

The font removal works (the code is at the end of this mail).

Now the viewer issues an error like:
Cannot find or create the font 'AAAEOG+ArialMT'.
And obviously the file isn't correctly displayed.

With iText I obtain /Encoding entries referring to /BaseFont /Helvetica, also for the special
characters outside the WinAnsi range, and the viewer correctly opens the file.
I wonder if it's possible to manipulate the file with PDFBox, so that it refers to Helvetica
as well.

Thank you in advance,
Fabio


---

    private void clearAllFontDescriptors() throws IOException
    {
        for(PDPage page : document.getPages())
        {
            PDResources pageResources = page.getResources();
            for(COSName fontName : pageResources.getFontNames())
            {
                clearFontDescriptor(pageResources.getFont(fontName));
            }
        }
    }

    public void clearFontDescriptor(PDFont pdFont)
    {
        PDFontDescriptor fontDescriptor = pdFont.getFontDescriptor();
        COSDictionary cosObj = fontDescriptor.getCOSObject();
        Set<COSName> fontNameSet = new HashSet<>();
        for(COSName name : cosObj.keySet())
        {
            if(name.getName().startsWith("FontFile"))
            {
                fontNameSet.add(name);
            }
        }

        for(COSName name : fontNameSet)
        {
            cosObj.removeItem(name);
        }
    }


Fabio Vassallo
Product Development (CIS)


Würth Phoenix S.r.l.
via Kravogl 4, 39100 Bolzano
T: +39 0471 564 116
F: +39 0471 564 122
Website | e-Mail | Map

          


-----Original Message-----
From: Tilman Hausherr [mailto:THausherr@t-online.de] 
Sent: Friday, April 14, 2017 9:52 AM
To: users@pdfbox.apache.org
Subject: Re: Embedded vs. non-embedded fonts (PDFBox & iText)

Your code looks good.

Does "clearAllFontDescriptors" have access to your current document object?

Does "the resulting file is empty" mean that the file is empty, or that the display is blank?

I don't have further ideas without seeing more of the code, and maybe look at the files with
pdfdebugger ... maybe try removing "clearAllFontDescriptors" to see whether there's a problem
with your logic.

Tilman

Am 14.04.2017 um 09:37 schrieb Vassallo, Fabio:
> Actually I don't save the file: i display it in the screen, and then the user can choose
to save it or not.
> So I currently create a byte[] containing the PDDocument data and I use it to display
the file:
>
> ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
> PDDocument document = ...
>
> <... build the PDF content ...>
>
> try
> {
>      document.save(outputStream);
>      document.close();
>      return outputStream.toByteArray(); } catch(IllegalStateException 
> e) {
>      ...
> }
>
> I tried to do this:
>
> try
> {
>      document.save(outputStream);
>      document.close();
>      document = PDDocument.load(outputStream.toByteArray());
>      clearAllFontDescriptors();
>      outputStream = new ByteArrayOutputStream();
>      document.save(outputStream);
>      document.close();
>      return outputStream.toByteArray(); } catch(IllegalStateException 
> e) {
>      ...
> }
>
> Where clearAllFontDescriptors() should perform the cosObj.removeItem(fontName). But apparently
the PDDocument.load(...) doesn't work as I expected, and the resulting file is empty.
>
> Fabio Vassallo
> Product Development (CIS)
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org

Mime
View raw message