private void addContent(PdfWriter writer) throws DocumentException, PdfFormatierungsException { Anchor anchor = new Anchor("Lernentwicklungsbericht", lernentwicklungsberichtUeberschriftFont); Chapter chapterLEB = new Chapter(new Paragraph(anchor), 1); chapterLEB.setNumberDepth(0); Paragraph paragraphHeader = new Paragraph(); paragraphHeader.setLeading(FIXED_LEADING_TEXT, 1); sectionCount += 1; Section headerSection = chapterLEB.addSection(paragraphHeader); headerSection.setNumberDepth(0); paragraphHeader.add(Chunk.NEWLINE); paragraphHeader.add(PdfFormatHelper.buildHeaderNameLine(lebData.getSchuelername() , headerFont)); paragraphHeader.add(Chunk.NEWLINE); paragraphHeader.add(PdfFormatHelper.buildHeaderKlassendatenLine(lebData, headerFont)); headerSection.add(Chunk.NEWLINE); headerSection.add(Chunk.NEWLINE); document.add(chapterLEB); insertDummyLineIfNecessary(writer); addKlassenbrief(chapterLEB, writer); addIndividuelleEinschaetzung(chapterLEB, writer); addFacheinschaetzungen(chapterLEB, writer); }
private void addIndividuelleEinschaetzung(Chapter chapterLEB, PdfWriter writer) throws DocumentException, PdfFormatierungsException { if (!lebData.getIndividuelleEinschaetzung().isEmpty()) { sectionCount += 1; breakHurenkind(writer); breakSchusterjunge(writer); Paragraph paragraphIndividuelleEinschaetzung = new Paragraph(); Section individuelleEinschaetzungsTextSection = chapterLEB.addSection(paragraphIndividuelleEinschaetzung); individuelleEinschaetzungsTextSection.setNumberDepth(0); Paragraph schuelereinschaetzungParapgraph = new Paragraph(lebData.getIndividuelleEinschaetzung().replace('\t', '\0'), standardTextFont); schuelereinschaetzungParapgraph.setAlignment(Element.ALIGN_JUSTIFIED); schuelereinschaetzungParapgraph.setLeading(FIXED_LEADING_TEXT, zeilenabstandsfaktor); individuelleEinschaetzungsTextSection.add(schuelereinschaetzungParapgraph); document.add(individuelleEinschaetzungsTextSection); document.add(getKlassenlehrerunterschrift(chapterLEB)); alertHurenkind(writer); insertDummyLineIfNecessary(writer); } }
private void addKlassenbrief(Chapter chapterLEB, PdfWriter writer) throws DocumentException, PdfFormatierungsException { if (!lebData.getKlassenbrief().isEmpty()) { sectionCount += 1; breakSchusterjunge(writer); Paragraph paragraphKlassenbrief = new Paragraph(); Section klassenbriefSection = chapterLEB.addSection(paragraphKlassenbrief); klassenbriefSection.setNumberDepth(0); Paragraph klasseneinschaetzungParapgraph = new Paragraph(lebData.getKlassenbrief().replace('\t', '\0'), standardTextFont); klasseneinschaetzungParapgraph.setAlignment(Element.ALIGN_JUSTIFIED); klasseneinschaetzungParapgraph.setLeading(FIXED_LEADING_TEXT, zeilenabstandsfaktor); klasseneinschaetzungParapgraph.add(Chunk.NEWLINE); if (lebData.getIndividuelleEinschaetzung().isEmpty()) { klassenbriefSection.add(klasseneinschaetzungParapgraph); document.add(klassenbriefSection); document.add(getKlassenlehrerunterschrift(chapterLEB)); } else { klasseneinschaetzungParapgraph.add(Chunk.NEWLINE); klassenbriefSection.add(klasseneinschaetzungParapgraph); document.add(klassenbriefSection); } alertLonelyHeader(writer); insertDummyLineIfNecessary(writer); } }
public void createPdf(String filename) throws DocumentException, IOException { Document document = new Document(PageSize.LETTER); PdfWriter.getInstance(document, new FileOutputStream(filename)); document.open(); // step 4 - add content into document for (int i = 1; i <= 10; i++) { Chapter chapter = new Chapter(String.format("Chapter %s", i), i); Section section = chapter.addSection("Section"); section.setIndentation(18); section.add(new Paragraph("TestTestTestTestTestTestTestTestTestTestTestTest")); for (int j = 1; j <= 3; j++) { Section subSection = section.addSection("Sub Section"); subSection.add(new Paragraph("TestTestTestTestTestTestTestTestTestTestTestTest")); } document.add(chapter); } document.close(); }
private static void createList(Section subCatPart) { List list = new List(true, false, 10); list.add(new ListItem("First point")); list.add(new ListItem("Second point")); list.add(new ListItem("Third point")); subCatPart.add(list); }
private Section getKlassenlehrerunterschrift(Section chapterLEB) { Paragraph unterschriftParagraph = new Paragraph(); Section klassenbriefUnterschriftSection = chapterLEB.addSection(unterschriftParagraph); klassenbriefUnterschriftSection.setNumberDepth(0); unterschriftParagraph.add(new Phrase(lebData.getKlassenlehrerUnterschrift().replace('\t', '\0'), standardTextFont)); unterschriftParagraph.setAlignment(Element.ALIGN_RIGHT); unterschriftParagraph.add(Chunk.NEWLINE); unterschriftParagraph.add(Chunk.NEWLINE); return klassenbriefUnterschriftSection; }
public Section newSection(Paragraph sectionTitle, int hLevel, boolean numbered) { if (hLevel < 1) throw new IllegalArgumentException("Section hLevel starts at 1 (H1, H2, H3...)"); Arrays.fill(sections, hLevel, sections.length, null); Section section; if (hLevel == 1) { if (numbered) // only increase chapter number if the number is used chapterCount++; Chapter chapter = new Chapter(sectionTitle, chapterCount); sections[hLevel] = chapter; chapters.add(chapter); section = chapter; } else { Section parent = sections[hLevel - 1]; if (parent == null) { throw new IllegalStateException("No parent section (depth H" + (hLevel - 1) + ") found"); } sectionTitle.setSpacingBefore(20f); section = parent.addSection(10.0f, sectionTitle); sections[hLevel] = section; } if (!numbered) section.setNumberDepth(0); return section; }
public Section currentSection() { Section prev = null; for (int i = 1; i < sections.length; i++) { Section section = sections[i]; if (section != null) { prev = section; } else { break; } } return prev; }
@Test public void h1_should_be_chapter() { Section section = sections.newSection(new Paragraph("Introduction"), 1); assertThat(section).isInstanceOf(Chapter.class); assertThat(sections.currentSection()).isSameAs(section); assertThat(sections.sections()).isEqualTo(new Section[]{null, section, null, null, null, null, null, null, null, null}); }
@Test public void h2_should_be_section() { Section chapter = sections.newSection(new Paragraph("Getting Started"), 1); Section section = sections.newSection(new Paragraph("Introduction"), 2); assertThat(section).isInstanceOf(Section.class); assertThat(sections.currentSection()).isSameAs(section); assertThat(sections.sections()).isEqualTo(new Section[]{null, chapter, section, null, null, null, null, null, null, null}); }
@Test public void h3_should_be_section() { Section chapter = sections.newSection(new Paragraph("Organizational Challenges"), 1); Section sectionChapter = sections.newSection(new Paragraph("Cultural Challenges"), 2); Section section1 = sections.newSection(new Paragraph("Organizational culture"), 3); assertThat(section1).isInstanceOf(Section.class); assertThat(sections.currentSection()).isSameAs(section1); assertThat(sections.sections()).isEqualTo(new Section[]{null, chapter, sectionChapter, section1, null, null, null, null, null, null}); }
@Test public void new_h3_should_replace_previous_h3() { Section chapter = sections.newSection(new Paragraph("Organizational Challenges"), 1); Section sectionChapter = sections.newSection(new Paragraph("Cultural Challenges"), 2); Section section1 = sections.newSection(new Paragraph("Organizational culture"), 3); Section section2 = sections.newSection(new Paragraph("Barrier to success"), 3); assertThat(section2).isInstanceOf(Section.class); assertThat(sections.currentSection()).isSameAs(section2); assertThat(sections.sections()).isEqualTo(new Section[]{null, chapter, sectionChapter, section2, null, null, null, null, null, null}); }
@Test public void new_h2_should_discard_previous_h3() { Section chapter = sections.newSection(new Paragraph("Organizational Challenges"), 1); Section sectionChapter1 = sections.newSection(new Paragraph("Cultural Challenges"), 2); Section section1 = sections.newSection(new Paragraph("Organizational culture"), 3); Section section2 = sections.newSection(new Paragraph("Barrier to success"), 3); Section sectionChapter2 = sections.newSection(new Paragraph("Team Logistics"), 2); assertThat(sections.currentSection()).isSameAs(sectionChapter2); assertThat(sections.sections()).isEqualTo(new Section[]{null, chapter, sectionChapter2, null, null, null, null, null, null, null}); }
public void createPdf(String filename) throws DocumentException, IOException { Document document = new Document(PageSize.LETTER); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); // TODO: 3. get imported page PdfReader stationery = new PdfReader(T11_ColumnText.RESULT); PdfImportedPage page = writer.getImportedPage(stationery, 1); writer.setPageEvent(new HeaderFooter(page)); document.open(); // step 4 - add content into document for (int i = 1; i <= 10; i++) { Chapter chapter = new Chapter(String.format("Chapter %s", i), i); Section section = chapter.addSection("Section"); section.add(new Paragraph("TestTestTestTestTestTestTestTestTestTestTestTest")); for (int j = 1; j <= 3; j++) { Section subSection = section.addSection("Sub Section"); subSection.add(new Paragraph("TestTestTestTestTestTestTestTestTestTestTestTest")); } document.add(chapter); } document.close(); }
/** * 直接把网页内容转为PDF文件 * * @param fileName * @throws Exception */ public static void parseURL2PDFFile(String pdfFile, String blogURL) throws Exception { BaseFont bfCN = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false); // 中文字体定义 Font chFont = new Font(bfCN, 14, Font.NORMAL, BaseColor.BLUE); Font secFont = new Font(bfCN, 12, Font.NORMAL, new BaseColor(0, 204, 255)); Font textFont = new Font(bfCN, 12, Font.NORMAL, BaseColor.BLACK); Document document = new Document(); PdfWriter pdfwriter = PdfWriter.getInstance(document, new FileOutputStream(pdfFile)); pdfwriter.setViewerPreferences(PdfWriter.HideToolbar); document.open(); String[] blogInfo = extractBlogInfo(blogURL); int chNum = 1; Chapter chapter = new Chapter(new Paragraph("URL转PDF测试", chFont), chNum++); Section section = chapter .addSection(new Paragraph(blogInfo[0], secFont)); section.setIndentation(10); section.setIndentationLeft(10); section.setBookmarkOpen(false); section.setNumberStyle(Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT); section.add(new Chunk("分类:" + blogInfo[1] + " 日期:" + blogInfo[2], textFont)); LineSeparator line = new LineSeparator(1, 100, new BaseColor(204, 204, 204), Element.ALIGN_CENTER, -2); Paragraph p_line = new Paragraph(" "); p_line.add(line); section.add(p_line); section.add(Chunk.NEWLINE); document.add(chapter); // html文件 XMLWorkerHelper.getInstance().parseXHtml(pdfwriter, document, parse2Stream(blogInfo[3])); document.close(); }
private void addFacheinschaetzungen(Chapter chapterLEB, PdfWriter writer) throws DocumentException, PdfFormatierungsException { for (LebFacheinschaetzungData facheinschaetzungsdaten : lebData.getFacheinschaetzungsdaten()) { sectionCount += 1; breakHurenkind(writer); breakSchusterjunge(writer); Paragraph paragraphFacheinschaetzung = new Paragraph(); Section facheinschaetzungsTextSection = chapterLEB.addSection(paragraphFacheinschaetzung); facheinschaetzungsTextSection.setNumberDepth(0); Collection<String> fachbezeichnungen = facheinschaetzungsdaten.getFachbezeichnungen(); fachbezeichnungen.add(facheinschaetzungsdaten.getFachname()); String facheinschaetzung = facheinschaetzungsdaten.getFacheinschaetzung(); Integer firstIndex = null; String boldedWord = ""; for (String fachbezeichnung : fachbezeichnungen) { int index = facheinschaetzung.toLowerCase().indexOf(fachbezeichnung.toLowerCase()); if (index != -1 && (firstIndex == null || firstIndex > index)) { firstIndex = index; boldedWord = fachbezeichnung; } } Paragraph facheinschaetzungParapgraph = new Paragraph(); facheinschaetzungParapgraph.setAlignment(Element.ALIGN_JUSTIFIED); facheinschaetzungParapgraph.setLeading(FIXED_LEADING_TEXT, zeilenabstandsfaktor); if (firstIndex == null) { facheinschaetzungParapgraph.add(new Phrase(facheinschaetzungsdaten.getFacheinschaetzung().replace('\t', '\0'), standardTextFont)); } else { String beforeBoldWord = facheinschaetzung.substring(0, firstIndex); facheinschaetzungParapgraph.add(new Phrase(beforeBoldWord.replace('\t', '\0'), standardTextFont)); String boldWord = facheinschaetzung.substring(firstIndex, firstIndex + boldedWord.length()); facheinschaetzungParapgraph.add(new Phrase(boldWord, standardTextBoldFont)); String afterBoldWord = facheinschaetzung.substring(firstIndex + boldedWord.length()); facheinschaetzungParapgraph.add(new Phrase(afterBoldWord.replace('\t', '\0'), standardTextFont)); } facheinschaetzungParapgraph.setFont(standardTextFont); facheinschaetzungsTextSection.add(facheinschaetzungParapgraph); Paragraph unterschriftParagraph = new Paragraph(); document.add(facheinschaetzungsTextSection); Section facheinschaetzungsUnterschriftSection = chapterLEB.addSection(unterschriftParagraph); facheinschaetzungsUnterschriftSection.setNumberDepth(0); unterschriftParagraph.add(new Phrase(facheinschaetzungsdaten.getUnterschrift().replace('\t', '\0'), standardTextFont)); unterschriftParagraph.setAlignment(Element.ALIGN_RIGHT); unterschriftParagraph.add(Chunk.NEWLINE); unterschriftParagraph.add(Chunk.NEWLINE); document.add(facheinschaetzungsUnterschriftSection); alertHurenkind(writer); insertDummyLineIfNecessary(writer); } }
public Section newSection(String title, int hLevel) { return newSection(title, hLevel, true); }
public Section newSection(String title, int hLevel, boolean numbered) { Font font = sectionTitlePrimaryFont(hLevel); Paragraph pTitle = new Paragraph(title, font); return newSection(pTitle, hLevel, numbered); }
public Section newSection(Paragraph sectionTitle, int hLevel) { return newSection(sectionTitle, hLevel, true); }
@VisibleForTesting Section[] sections() { return sections; }