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); } }
@Override public List<Element> end(WorkerContext ctx, Tag tag, List<Element> currentContent) { List<Element> elements = super.end(ctx, tag, currentContent); Paragraph paragraph = createParagraph(); paragraph.addAll(elements); String content = paragraph.getContent(); if (level == 1) { final Chapter chapter = configuration.createTitledChapter(content); return new ArrayList<Element>(1) {{ add(chapter); }}; } return elements; }
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 Chapter generateHeader() { Font font = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 16, Font.BOLDITALIC, BaseColor.BLACK); Chunk chunk = new Chunk(Localization.getString(Strings.REPORT_HEADLINE, date.toString("MMMM yyyy")), font); Chapter chapter = new Chapter(new Paragraph(chunk), 1); chapter.setNumberDepth(0); chapter.add(Chunk.NEWLINE); return chapter; }
public void addInvisibleChapterForHeader(Document document, Section section) { try { Paragraph paragraph=new Paragraph(section.getSectionName(), FontFactory.getFont(FontFactory.COURIER,1f,BaseColor.WHITE));//hide chapter Chapter chapter = new Chapter(paragraph,0); document.add(chapter); } catch (Exception e) { throw new RuntimeException(e); } }
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; }
@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}); }
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 void restoreChapter(Chapter chapter) { sections[1] = chapter; }
public Chapter currentChapter() { return (Chapter) sections[1]; }
public static Chapter createChapter(String chapterName, int chapterNumber) { Anchor anchor = new Anchor(chapterName, HEADER_FONT); anchor.setName(chapterName); Chapter chapter = new Chapter(new Paragraph(chapterName, CAT_FONT), chapterNumber); return chapter; }
private static void addContent(Document document,String msg) throws DocumentException { Paragraph preface = new Paragraph("Przebieg: ",smallBold); Chapter catPart = new Chapter(preface, 1); addEmptyLine(preface, 1); preface.add(msg); document.add(catPart); }