?? overlay.java
字號:
PDPage page = (PDPage)pagesIter.next();
COSBase contents = page.getCOSDictionary().getDictionaryObject( COSName.CONTENTS );
PDResources resources = page.findResources();
if( resources == null )
{
resources = new PDResources();
page.setResources( resources );
}
COSDictionary res = resources.getCOSDictionary();
if( contents instanceof COSStream )
{
COSStream stream = (COSStream) contents;
Map objectNameMap = new TreeMap();
stream = makeUniqObjectNames(objectNameMap, stream);
layoutPages.add(new LayoutPage(stream, res, objectNameMap));
}
else if( contents instanceof COSArray )
{
throw new UnsupportedOperationException("Layout pages with COSArray currently not supported.");
// layoutPages.add(new LayoutPage(contents, res));
}
else
{
throw new IOException( "Contents are unknown type:" + contents.getClass().getName() );
}
}
}
private COSStream makeUniqObjectNames(Map objectNameMap, COSStream stream) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream(10240);
byte[] buf = new byte[10240];
int read;
InputStream is = stream.getUnfilteredStream();
while ((read = is.read(buf)) > -1)
{
baos.write(buf, 0, read);
}
buf = baos.toByteArray();
baos = new ByteArrayOutputStream(buf.length + 100);
StringBuffer sbObjectName = new StringBuffer(10);
boolean bInObjectIdent = false;
boolean bInText = false;
boolean bInEscape = false;
for (int i = 0; i<buf.length; i++)
{
byte b = buf[i];
if (!bInEscape)
{
if (!bInText && b == '(')
{
bInText = true;
}
if (bInText && b == ')')
{
bInText = false;
}
if (b == '\\')
{
bInEscape = true;
}
if (!bInText && !bInEscape)
{
if (b == '/')
{
bInObjectIdent = true;
}
else if (bInObjectIdent && Character.isWhitespace((char) b))
{
bInObjectIdent = false;
// System.err.println(sbObjectName);
// String object = sbObjectName.toString();
String objectName = sbObjectName.toString().substring(1);
String newObjectName = objectName + "overlay";
baos.write('/');
baos.write(newObjectName.getBytes());
objectNameMap.put(objectName, COSName.getPDFName(newObjectName));
sbObjectName.delete(0, sbObjectName.length());
}
}
if (bInObjectIdent)
{
sbObjectName.append((char) b);
continue;
}
}
else
{
bInEscape = false;
}
baos.write(b);
}
COSDictionary streamDict = new COSDictionary();
streamDict.setItem(COSName.LENGTH, new COSInteger(baos.size()));
COSStream output = new COSStream(streamDict, pdfDocument.getDocument().getScratchFile());
output.setFilters(stream.getFilters());
OutputStream os = output.createUnfilteredStream();
baos.writeTo(os);
os.close();
return output;
}
private void processPages( List pages ) throws IOException
{
Iterator pageIter = pages.iterator();
while( pageIter.hasNext() )
{
PDPage page = (PDPage)pageIter.next();
COSDictionary pageDictionary = page.getCOSDictionary();
COSBase contents = pageDictionary.getDictionaryObject( COSName.CONTENTS );
if( contents instanceof COSStream )
{
COSStream contentsStream = (COSStream)contents;
// System.err.println("stream");
pageCount++;
COSArray array = new COSArray();
array.add(contentsStream);
mergePage( array, page );
pageDictionary.setItem(COSName.CONTENTS, array);
}
else if( contents instanceof COSArray )
{
COSArray contentsArray = (COSArray)contents;
mergePage( contentsArray, page );
}
else
{
throw new IOException( "Contents are unknown type:" + contents.getClass().getName() );
}
}
}
private void mergePage(COSArray array, PDPage page )
{
int layoutPageNum = pageCount % layoutPages.size();
LayoutPage layoutPage = (LayoutPage) layoutPages.get(layoutPageNum);
PDResources resources = page.findResources();
if( resources == null )
{
resources = new PDResources();
page.setResources( resources );
}
COSDictionary docResDict = resources.getCOSDictionary();
COSDictionary layoutResDict = layoutPage.res;
mergeArray(PROC_SET, docResDict, layoutResDict);
mergeDictionary(COSName.FONT, docResDict, layoutResDict, layoutPage.objectNameMap);
mergeDictionary(XOBJECT, docResDict, layoutResDict, layoutPage.objectNameMap);
mergeDictionary(EXT_G_STATE, docResDict, layoutResDict, layoutPage.objectNameMap);
//we are going to wrap the existing content around some save/restore
//graphics state, so the result is
//
//<save graphics state>
//<all existing content streams>
//<restore graphics state>
//<overlay content>
array.add(0, saveGraphicsStateStream );
array.add( restoreGraphicsStateStream );
array.add(layoutPage.contents);
}
/**
* merges two dictionaries.
*
* @param dest
* @param source
*/
private void mergeDictionary(COSName name, COSDictionary dest, COSDictionary source, Map objectNameMap)
{
COSDictionary destDict = (COSDictionary) dest.getDictionaryObject(name);
COSDictionary sourceDict = (COSDictionary) source.getDictionaryObject(name);
if (destDict == null)
{
destDict = new COSDictionary();
dest.setItem(name, destDict);
}
if( sourceDict != null )
{
Iterator iterKeys = sourceDict.keyList().iterator();
while (iterKeys.hasNext())
{
COSName key = (COSName) iterKeys.next();
COSName mappedKey = (COSName) objectNameMap.get(key.getName());
if (mappedKey == null)
{
// object not needet
continue;
}
destDict.setItem(mappedKey, sourceDict.getItem(key));
}
}
}
/**
* merges two arrays.
*
* @param dest
* @param source
*/
private void mergeArray(COSName name, COSDictionary dest, COSDictionary source)
{
COSArray destDict = (COSArray) dest.getDictionaryObject(name);
COSArray sourceDict = (COSArray) source.getDictionaryObject(name);
if (destDict == null)
{
destDict = new COSArray();
dest.setItem(name, destDict);
}
for (int sourceDictIdx = 0; sourceDict != null && sourceDictIdx<sourceDict.size(); sourceDictIdx++)
{
COSBase key = sourceDict.get(sourceDictIdx);
if (key instanceof COSName)
{
COSName keyname = (COSName) key;
boolean bFound = false;
for (int destDictIdx = 0; destDictIdx<destDict.size(); destDictIdx++)
{
COSBase destkey = destDict.get(destDictIdx);
if (destkey instanceof COSName)
{
COSName destkeyname = (COSName) destkey;
if (destkeyname.equals(keyname))
{
bFound = true;
break;
}
}
}
if (!bFound)
{
destDict.add(keyname);
}
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -