1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.SimpleNamedDestination;
public class TocBuilder {
protected Log logger = LogFactory.getLog(getClass());
// to build TOC
private int tocVerticalPosition = 0;
private int tocVerticalDelta = 0;
private int tocHorizontalPosition = 0;
private int noTitlePage = 7;
/**
* Create the TOC
*
* @param urlPDF
* @throws Exception
*/
public void addToc(String path, int initialPage) {
String tocName = "TOC_TEMP";
try {
if (logger.isDebugEnabled())
logger.debug("Creating the TOC.....");
// make a copy of the file
FileUtils.copyFile(new File(path), new File(path+tocName));
PdfReader r = new PdfReader(path+tocName);
PdfStamper stp = new PdfStamper(r, new FileOutputStream(path));
Map map = SimpleNamedDestination.getNamedDestination(r, false);
List<Integer> pages = listTOC(map);
Collections.sort(pages);
// initial page
int page = initialPage;
int i = 1;
// get the content of the initial page
PdfContentByte cb = stp.getOverContent(page);
for (Iterator iter = pages.iterator(); iter.hasNext();) {
// if we reached the number of title for one page, we have to pass to the next page
// and come back to the initial y position
if (i == noTitlePage) {
cb = stp.getOverContent(++page);
i = 1;
}
// get the first token and insert the content into the phrase
Phrase phrase = new Phrase(new StringTokenizer(iter.next().toString(), " ").nextToken(),
new Font(Font.HELVETICA, 8, Font.NORMAL));
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, phrase, tocHorizontalPosition,
tocVerticalPosition - (i++ * (tocVerticalDelta)), 0);
}
stp.close();
// delete the copy
FileUtils.forceDelete(new File(path+tocName));
if (logger.isDebugEnabled())
logger.debug("Done!");
}
catch (Exception e) {
logger.error("Error while creating the TOC:", e);
throw new Exception(e);
}
}
/**
* Transform a map of TOC into a list of page number
*
* @param map
* @return
*/
protected List<Integer> listTOC(Map map) {
List<Integer> list = new ArrayList<Integer>();
for (Object value : map.values())
list.add(new Integer(new StringTokenizer(value.toString(), " ").nextToken()));
return list;
}
public void setTocVerticalPosition(int tocVerticalPosition) {
this.tocVerticalPosition = tocVerticalPosition;
}
public void setTocVerticalDelta(int tocVerticalDelta) {
this.tocVerticalDelta = tocVerticalDelta;
}
public void setTocHorizontalPosition(int tocHorizontalPosition) {
this.tocHorizontalPosition = tocHorizontalPosition;
}
public void setNoTitlePage(int noTitlePage) {
this.noTitlePage = noTitlePage;
}
} |
Partager