Java Tutorial/PDF/Paragraph
Содержание
- 1 Add a Paragraph to a document
- 2 Colored Paragraphs
- 3 Element.ALIGN_JUSTIFIED
- 4 Paragraph Alignment: ALIGN_JUSTIFIED
- 5 Paragraph Alignment: Element.ALIGN_RIGHT
- 6 Paragraph Indentation Left
- 7 Paragraph Indentation Right
- 8 Paragraph Positions
- 9 Paragraph Spacing After
- 10 Paragraph Spacing Before
- 11 Paragraph text
Add a Paragraph to a document
<source lang="java">
import java.io.FileOutputStream; import com.lowagie.text.Document; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.PdfWriter; public class CreateParagraph {
public static void main(String[] args) throws Exception { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf")); document.open(); document.add(new Paragraph("Hello World")); document.close(); }
}</source>
Colored Paragraphs
<source lang="java">
import java.awt.Color; import java.io.FileOutputStream; import com.lowagie.text.Document; import com.lowagie.text.Font; import com.lowagie.text.Image; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.CMYKColor; import com.lowagie.text.pdf.PatternColor; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfPatternPainter; import com.lowagie.text.pdf.PdfShading; import com.lowagie.text.pdf.PdfShadingPattern; import com.lowagie.text.pdf.PdfSpotColor; import com.lowagie.text.pdf.PdfWriter; import com.lowagie.text.pdf.ShadingColor; import com.lowagie.text.pdf.SpotColor; public class MainClass {
public static void main(String[] args) throws Exception { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf")); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfSpotColor psc_cmyk = new PdfSpotColor("iTextSpotColorCMYK", 0.25f, new CMYKColor(0.3f, .9f, .3f, .1f)); SpotColor sc_cmyk = new SpotColor(psc_cmyk); Image img = Image.getInstance("dog.jpg"); PdfPatternPainter img_pattern = cb.createPattern(img.scaledWidth(), img.scaledHeight(), img .scaledWidth(), img.scaledHeight()); img_pattern.addImage(img, img.scaledWidth(), 0f, 0f, img.scaledHeight(), 0f, 0f); img_pattern.setPatternMatrix(1f, 0f, 0f, 1f, 60f, 60f); document.add(new Paragraph("This is a paragraph painted using a SpotColor", new Font( Font.HELVETICA, 24, Font.BOLD, sc_cmyk))); document.close(); }
}</source>
Element.ALIGN_JUSTIFIED
<source lang="java">
import java.io.FileOutputStream; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.pdf.PdfWriter; public class MainClass {
public static void main(String[] args) throws Exception { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("2.pdf")); document.open(); Chunk space = new Chunk("asdf"); Paragraph paragraph = new Paragraph(); paragraph.add(space); paragraph.setAlignment(Element.ALIGN_JUSTIFIED); document.add(paragraph); document.close(); }
}</source>
Paragraph Alignment: ALIGN_JUSTIFIED
<source lang="java">
import java.io.FileOutputStream; import com.lowagie.text.Document; import com.lowagie.text.Element; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.PdfWriter; public class MainClass {
public static void main(String[] args) throws Exception { Document document = new Document(PageSize.A8.rotate()); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf")); document.open(); String text = "this is a test"; Paragraph paragraph = new Paragraph(text); paragraph.setAlignment(Element.ALIGN_JUSTIFIED); document.add(paragraph); document.newPage(); writer.setSpaceCharRatio(PdfWriter.NO_SPACE_CHAR_RATIO); document.add(paragraph); document.close(); }
}</source>
Paragraph Alignment: Element.ALIGN_RIGHT
<source lang="java">
import java.io.FileOutputStream; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.pdf.PdfWriter; public class MainClass {
public static void main(String[] args) throws Exception { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("2.pdf")); document.open(); Chunk space = new Chunk("asdf"); Paragraph paragraph = new Paragraph(); paragraph.add(space); paragraph.setAlignment(Element.ALIGN_RIGHT); document.add(paragraph); document.close(); }
}</source>
Paragraph Indentation Left
<source lang="java">
import java.io.FileOutputStream; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.pdf.PdfWriter; public class MainClass {
public static void main(String[] args) throws Exception { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("2.pdf")); document.open(); Chunk space = new Chunk("asdf"); Phrase phrase2 = new Phrase(new Chunk("asdf", new Font(Font.TIMES_ROMAN))); Paragraph paragraph = new Paragraph(); paragraph.add(space); paragraph.add(phrase2); paragraph.setIndentationLeft(20); paragraph.setAlignment(Element.ALIGN_JUSTIFIED); document.add(paragraph); document.close(); }
}</source>
Paragraph Indentation Right
<source lang="java">
import java.io.FileOutputStream; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.pdf.PdfWriter; public class MainClass {
public static void main(String[] args) throws Exception { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("2.pdf")); document.open(); Chunk space = new Chunk("asdf"); Phrase phrase2 = new Phrase(new Chunk("asdf", new Font(Font.TIMES_ROMAN))); Paragraph paragraph = new Paragraph(); paragraph.add(space); paragraph.add(phrase2); paragraph.setIndentationRight(20); document.add(paragraph); document.close(); }
}</source>
Paragraph Positions
<source lang="java">
import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.FileReader; import com.lowagie.text.Document; import com.lowagie.text.Element; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfWriter; public class MainClass {
public static void main(String[] args) throws Exception { Document document = new Document(PageSize.A4); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf")); document.open(); PdfContentByte cb = writer.getDirectContent(); BufferedReader reader = new BufferedReader(new FileReader("a.txt")); String line; Paragraph p; float pos; while ((line = reader.readLine()) != null) { p = new Paragraph(" " + line); p.setAlignment(Element.ALIGN_JUSTIFIED); document.add(p); pos = writer.getVerticalPosition(false); cb.moveTo(0, pos); cb.lineTo(PageSize.A4.width(), pos); cb.stroke(); if (pos < 90) document.newPage(); } reader.close(); document.close(); }
}</source>
Paragraph Spacing After
<source lang="java">
import java.io.FileOutputStream; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.pdf.PdfWriter; public class MainClass {
public static void main(String[] args) throws Exception { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("2.pdf")); document.open(); Chunk space = new Chunk("asdf"); Phrase phrase2 = new Phrase(new Chunk("asdf", new Font(Font.TIMES_ROMAN))); Paragraph paragraph = new Paragraph(); paragraph.add(space); paragraph.add(phrase2); paragraph.setSpacingBefore(20); paragraph.setSpacingAfter(10); paragraph.setAlignment(Element.ALIGN_JUSTIFIED); document.add(paragraph); document.close(); }
}</source>
Paragraph Spacing Before
<source lang="java">
import java.io.FileOutputStream; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.pdf.PdfWriter; public class MainClass {
public static void main(String[] args) throws Exception { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("2.pdf")); document.open(); Chunk space = new Chunk("asdf"); Paragraph paragraph = new Paragraph(); paragraph.add(space); paragraph.setSpacingBefore(20); paragraph.setAlignment(Element.ALIGN_JUSTIFIED); document.add(paragraph); document.close(); }
}</source>
Paragraph text
<source lang="java">
import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.FileReader; import com.lowagie.text.Document; import com.lowagie.text.Element; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.PdfWriter; public class MainClass {
public static void main(String[] args) throws Exception { Document document = new Document(PageSize.A4); PdfWriter.getInstance(document, new FileOutputStream("2.pdf")); document.open(); BufferedReader reader = new BufferedReader(new FileReader("a.txt")); String line; Paragraph p; while ((line = reader.readLine()) != null) { p = new Paragraph(" " + line); p.setAlignment(Element.ALIGN_JUSTIFIED); document.add(p); } reader.close(); document.close(); }
}</source>