|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2020 [Open Lowcode SAS](https://openlowcode.com/) |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0 . |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + ********************************************************************************/ |
| 10 | + |
| 11 | +package org.openlowcode.tools.richtext; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.util.ArrayList; |
| 15 | + |
| 16 | +import org.openlowcode.tools.pdf.BulletText; |
| 17 | +import org.openlowcode.tools.pdf.PDFPageBand; |
| 18 | +import org.openlowcode.tools.pdf.PDFPageBandSection; |
| 19 | +import org.openlowcode.tools.pdf.ParagraphHeader; |
| 20 | +import org.openlowcode.tools.pdf.SectionText; |
| 21 | + |
| 22 | +/** |
| 23 | + * An utility to print in a page band a rich text content |
| 24 | + * |
| 25 | + * @author <a href="https://openlowcode.com/" rel="nofollow">Open Lowcode |
| 26 | + * SAS</a> |
| 27 | + * |
| 28 | + */ |
| 29 | +public class PDFPageBandRichTextUtility { |
| 30 | + |
| 31 | + /** |
| 32 | + * Inserts in the page band the rich text content |
| 33 | + * |
| 34 | + * @param pageband page band to print into |
| 35 | + * @param sourcetext source text formatted as rich text |
| 36 | + * @throws IOException |
| 37 | + */ |
| 38 | + public static void insertRichTextInPageBand(PDFPageBand pageband,String sourcetext) throws IOException { |
| 39 | + PDFPageBandSection[] sectionsforrichtext = PDFPageBandRichTextUtility.generateSectionFromRichText(sourcetext); |
| 40 | + if (sectionsforrichtext!=null) for (int i=0;i<sectionsforrichtext.length;i++) |
| 41 | + pageband.printNewSection(sectionsforrichtext[i]); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Parses the rich text and generates section for printing |
| 46 | + * @param sourcetext source text formatted in rich text |
| 47 | + * @return list of page band sections |
| 48 | + */ |
| 49 | + public static PDFPageBandSection[] generateSectionFromRichText(String sourcetext) { |
| 50 | + ArrayList<RichTextSection> structuredtext = RichTextParser.simplifyforblack(RichTextParser.parseText(sourcetext)); |
| 51 | + return parseSections(structuredtext).toArray(new PDFPageBandSection[0]); |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + private static ArrayList<PDFPageBandSection> parseSections(ArrayList<RichTextSection> structuredtext) { |
| 56 | + ArrayList<PDFPageBandSection> sections = new ArrayList<PDFPageBandSection>(); |
| 57 | + for (int i = 0; i < structuredtext.size(); i++) { |
| 58 | + RichTextSection thistextsection = structuredtext.get(i); |
| 59 | + if (thistextsection.isNormalText()) { |
| 60 | + SectionText thissectiontext = new SectionText(thistextsection.getText()); |
| 61 | + sections.add(thissectiontext); |
| 62 | + } |
| 63 | + if (thistextsection.isBullet()) { |
| 64 | + BulletText thisbullettext = new BulletText(thistextsection.getText(), 1); |
| 65 | + sections.add(thisbullettext); |
| 66 | + } |
| 67 | + if (thistextsection.isSectiontitle()) { |
| 68 | + ParagraphHeader header = new ParagraphHeader(thistextsection.getText()); |
| 69 | + sections.add(header); |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | + return sections; |
| 74 | + } |
| 75 | +} |
| 76 | + |
0 commit comments