use of com.itextpdf.tool.xml.XMLWorkerFontProvider in project trainning by fernandotomasio.
the class DOC003PDF method makeReport.
@Override
public void makeReport() {
teachingDocumentsService = (TeachingDocumentsService) services.get("teachingDocumentsService");
document = documentManager.getDocumentPortrait();
os = documentManager.prepareDocument(document);
writer = documentManager.getWritter();
ChapterSectionTOC eventHandler = new ChapterSectionTOC();
writer.setPageEvent(eventHandler);
document.open();
CSSResolver cssResolver = new StyleAttrCSSResolver();
CssFile cssFile = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String cssStylePath = classLoader.getResource("doc3-styles.css").getPath();
cssFile = XMLWorkerHelper.getCSS(classLoader.getResourceAsStream("doc3-styles.css"));
cssResolver.addCss(cssFile);
XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
String fontFilePath = classLoader.getResource("MyriadPro-Regular.otf").getPath();
// fontProvider.register(fontFilePath);
CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
XMLWorker worker = new XMLWorker(css, true);
p = new XMLParser(worker);
try {
String apostilaIdParam = (String) params.get("apostilaId");
Long apostilaId = Long.parseLong(apostilaIdParam);
ApostilaDTO apostila = teachingDocumentsService.findApostila(apostilaId);
CurriculoMinimoDTO curriculo = teachingDocumentsService.findCurriculoMinimo(apostila.getDisciplina().getCurriculoMinimo().getId());
buildCover(document, curriculo, apostila);
CapituloApostilaDTO[] capitulos = teachingDocumentsService.findAllCapitulosApostila(apostilaId);
for (CapituloApostilaDTO capitulo : capitulos) {
Chapter ch = buildCapitulo(capitulo, p, document);
document.add(ch);
}
document.add(buildReferencias(apostilaId));
document.add(buildSumario(eventHandler.titles));
} catch (DocumentException e) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, e);
throw new CoreException(e.getMessage());
} catch (IOException ex) {
Logger.getLogger(DOC003PDF.class.getName()).log(Level.SEVERE, null, ex);
} finally {
document.close();
}
try {
PdfReader reader = new PdfReader(os.toByteArray());
int n = reader.getNumberOfPages();
String pagesInterval = String.format("1-2,%d, 3-%d", n, n - 1);
reader.selectPages(pagesInterval);
os = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(reader, os);
stamper.close();
reader.close();
} catch (IOException | DocumentException ex) {
Logger.getLogger(DOC003PDF.class.getName()).log(Level.SEVERE, null, ex);
}
}
use of com.itextpdf.tool.xml.XMLWorkerFontProvider in project trainning by fernandotomasio.
the class DOC005PDF method makeReport.
@Override
public void makeReport() {
teachingDocumentsService = (TeachingDocumentsService) services.get("teachingDocumentsService");
document = documentManager.getDocumentPortrait();
os = documentManager.prepareDocument(document);
writer = documentManager.getWritter();
ChapterSectionTOC eventHandler = new ChapterSectionTOC();
writer.setPageEvent(eventHandler);
document.open();
CSSResolver cssResolver = new StyleAttrCSSResolver();
CssFile cssFile = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String cssStylePath = classLoader.getResource("doc3-styles.css").getPath();
cssFile = XMLWorkerHelper.getCSS(classLoader.getResourceAsStream("doc3-styles.css"));
cssResolver.addCss(cssFile);
XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
String fontFilePath = classLoader.getResource("MyriadPro-Regular.otf").getPath();
// fontProvider.register(fontFilePath);
CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
XMLWorker worker = new XMLWorker(css, true);
p = new XMLParser(worker);
try {
String secaoIdParam = (String) params.get("secaoId");
document.add(Chunk.NEXTPAGE);
SecaoApostilaDTO secao = teachingDocumentsService.findSecaoApostila(Long.parseLong(secaoIdParam));
Chapter ch = new Chapter(0);
ch.setNumberDepth(0);
if (secao.getContent() != null) {
buildSection(ch, secao, p, document);
}
document.add(ch);
} catch (DocumentException e) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, e);
throw new CoreException(e.getMessage());
} catch (IOException ex) {
Logger.getLogger(DOC005PDF.class.getName()).log(Level.SEVERE, null, ex);
} finally {
document.close();
}
}
Aggregations