[ 
https://issues.apache.org/jira/browse/PDFBOX-1895?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13997707#comment-13997707
 ] 

Pat Hickey commented on PDFBOX-1895:
------------------------------------

The PDType0Font object has the decrypted /Ordering and /Registry values 
attached to the CMap. It might be easier as a published fix to just decrypt the 
values in the COSObjects directly, but the workaround I used was to copy the 
registry and ordering from the PDType0Font object. This was a little tricky, 
since the CMap is not exposed by PDType0Font.
*USE*
{code}
               // after document.openProtection( new 
StandardDecryptionMaterial( "" );
               PDDocumentCatalog catalog = document.getDocumentCatalog();
                List<?> pages = catalog.getAllPages();
                for ( Object obj : pages ) {
                        PDPage page = (PDPage)obj;
                        PDResources resources = page.findResources();
                        Map< String, PDFont > fonts = resources.getFonts();
                        for ( Map.Entry< String, PDFont > entry : 
fonts.entrySet() ) {
                                PDFont font = entry.getValue();
                                FixType0FontFromCMap.fixType0FontFromCMap( font 
);
                        }
                }
{code}

*WORKAROUND*
{code}
package com.bloomberg.glodas.pdf.util;

import org.apache.fontbox.cmap.CMap;
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;

public class FixType0FontFromCMap extends PDType0Font {

        public FixType0FontFromCMap() {
                super();
        }

        public FixType0FontFromCMap( COSDictionary fontDictionary ) {
                super( fontDictionary );
        }

        private static final String cid_font_type0 = 
COSName.CID_FONT_TYPE0.getName();
        private static final String cid_font_type2 = 
COSName.CID_FONT_TYPE2.getName();
        
        public static void fixType0FontFromCMap( PDFont font ) {
                if ( font instanceof PDType0Font ) {
                        COSBase cos = font.getCOSObject();
                        if ( cos == null || ! ( cos instanceof COSDictionary ) 
) {
                                return;
                        }
                        COSDictionary dict = (COSDictionary)cos;
                        FixType0FontFromCMap vis = new FixType0FontFromCMap( 
dict );
                        CMap cmap = vis.cmap;
                        String decrypted_ordering = cmap.getOrdering();
                        String decrypted_registry = cmap.getRegistry();
                        cos = dict.getDictionaryObject( 
COSName.DESCENDANT_FONTS );
                        if ( cos == null ) {
                                return;
                        }
                        COSArray array = (COSArray)cos;
                        for ( int ix = 0; ix < array.size(); ++ix ) {
                                cos = array.getObject( ix );
                                if ( cos != null && cos instanceof 
COSDictionary ) {
                                        COSDictionary descendants = 
(COSDictionary)cos;
                                        String type = 
descendants.getNameAsString( COSName.TYPE );
                                        String subtype = 
descendants.getNameAsString( COSName.SUBTYPE );
                                        if ( type == null || ! type.equals( 
COSName.FONT.getName() ) ) {
                                                continue;
                                        }
                                        if ( subtype == null || ! ( 
subtype.equals( cid_font_type0 ) || subtype.equals( cid_font_type2 ) ) ) {
                                           continue;
                                        }
                                        cos = descendants.getDictionaryObject( 
COSName.CIDSYSTEMINFO );
                                        if ( cos == null || ! ( cos instanceof 
COSDictionary ) ) {
                                                continue;
                                        }
                                        COSDictionary info = (COSDictionary)cos;
                                        info.setString( COSName.REGISTRY, 
decrypted_registry );
                                        info.setString( COSName.ORDERING, 
decrypted_ordering );
                                }
                        }
                }
        }
}
{code} 

> Type0 settings /Registry and /Ordering are not decrypted when writing document
> ------------------------------------------------------------------------------
>
>                 Key: PDFBOX-1895
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-1895
>             Project: PDFBox
>          Issue Type: Bug
>          Components: Writing
>    Affects Versions: 1.8.3, 1.8.4
>            Reporter: Pat Hickey
>
> When re-writing a document with font descriptions, Adobe Reader is unable to 
> display the fonts in the document.  Reader can display the fonts in the 
> original document. The difference is that in the original document, the font 
> descriptions are in lower object numbers than the font references; in the 
> output document, the font descriptions are in higher object numbers than the 
> font references.  Is there a quick way to re-order them?
> Update: the PDF file in question is actually corrupt, but somehow modifying 
> it with PDFBox causes it to no longer be readable with Adobe Reader.



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Reply via email to