Java Tutorial/PDF/Chunk
Содержание
- 1 Background of a chunk
- 2 Chunk in bold
- 3 Chunk.NEWLINE
- 4 Chunk Text Render Mode: PdfContentByte.TEXT_RENDER_MODE_FILL
- 5 Chunk Text Render Mode: PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE
- 6 Chunk Text Render Mode: PdfContentByte.TEXT_RENDER_MODE_INVISIBLE
- 7 Chunk Text Render Mode:PdfContentByte.TEXT_RENDER_MODE_STROKE
- 8 Chunk Underline
- 9 Chunk with action
- 10 Chunk with anchor
- 11 Create chunk from string
- 12 Create Phase from chunk
- 13 Local Destination
- 14 PDF file with hyphenated text
- 15 Scale Chunk
- 16 Skew a chunk
- 17 Use TextRise of a chunk to create supscript and subscript
- 18 Width of a chunk
Background of a chunk
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
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();
Font font = new Font(Font.COURIER, 10, Font.BOLD);
font.setColor(new Color(0xFF, 0xFF, 0xFF));
Chunk fox = new Chunk("this is a", font);
fox.setBackground(new Color(0xa5, 0x2a, 0x2a));
Phrase p = new Phrase(fox);
p.add(" test");
Chunk dog = new Chunk(" another test", new Font(Font.TIMES_ROMAN, 14, Font.ITALIC));
dog.setBackground(new Color(0xFF, 0x00, 0x00), 10, -30, 20, -10);
p.add(dog);
document.add(p);
document.close();
}
}
Chunk in bold
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
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();
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
Chunk bold = new Chunk("This looks like Font.BOLD");
bold.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 0.5f, null);
document.add(bold);
document.close();
}
}
Chunk.NEWLINE
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
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();
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
Chunk c = new Chunk("this is a test");
float w = c.getWidthPoint();
Paragraph p = new Paragraph("The width of the chunk: "");
p.add(c);
p.add("" is ");
p.add(String.valueOf(w));
p.add(" points or ");
p.add(String.valueOf(w / 72f));
p.add(" inches or ");
p.add(String.valueOf(w / 72f * 2.54f));
p.add(" cm.");
document.add(p);
document.add(Chunk.NEWLINE);
document.add(c);
document.close();
}
}
Chunk Text Render Mode: PdfContentByte.TEXT_RENDER_MODE_FILL
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
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();
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
Font font = new Font(Font.COURIER, 20);
Chunk chunk = new Chunk("this is a test.", font);
chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL, 0f, new Color(0xFF, 0x00, 0x00));
document.add(new Paragraph(chunk));
document.close();
}
}
Chunk Text Render Mode: PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
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();
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
Font font = new Font(Font.COURIER, 20);
Chunk chunk = new Chunk("this is a test.", font);
chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 0f, new Color(0xFF, 0x00, 0x00));
document.add(new Paragraph(chunk));
document.close();
}
}
Chunk Text Render Mode: PdfContentByte.TEXT_RENDER_MODE_INVISIBLE
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
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();
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
Font font = new Font(Font.COURIER, 20);
Chunk chunk = new Chunk("this is a test.", font);
chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE, 0f, new Color(0xFF, 0x00, 0x00));
document.add(new Paragraph(chunk));
document.close();
}
}
Chunk Text Render Mode:PdfContentByte.TEXT_RENDER_MODE_STROKE
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
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();
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
Font font = new Font(Font.COURIER, 20);
Chunk chunk = new Chunk("this is a test.", font);
chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_STROKE, 0f, new Color(0xFF, 0x00, 0x00));
document.add(new Paragraph(chunk));
document.close();
}
}
Chunk Underline
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
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();
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
Font font = new Font(Font.COURIER, 10, Font.BOLD);
font.setColor(new Color(0xFF, 0xFF, 0xFF));
Chunk fox = new Chunk("this is a ", font);
Chunk jumps = new Chunk(" test ", new Font());
Chunk dog = new Chunk(" another ", new Font(Font.TIMES_ROMAN, 14, Font.ITALIC));
dog.setUnderline(new Color(0xFF, 0x00, 0x00), 3.0f, 0.0f, -5.0f , 0.0f,
PdfContentByte.LINE_CAP_ROUND);
document.add(fox);
document.add(jumps);
document.add(dog);
document.close();
}
}
Chunk with action
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.PdfAction;
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();
Font font = new Font();
font.setStyle(Font.UNDERLINE);
Chunk chunk = new Chunk("jexp", font);
chunk.setAction(new PdfAction("http://www.jexp.ru"));
document.add(chunk);
document.close();
}
}
Chunk with anchor
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
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();
Font font = new Font();
font.setStyle(Font.UNDERLINE);
Chunk chunk = new Chunk("jexp", font);
chunk.setAnchor("http://www.jexp.ru");
document.add(chunk);
document.close();
}
}
Create chunk from string
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);
document.add(paragraph);
document.close();
}
}
Create Phase from chunk
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.setAlignment(Element.ALIGN_JUSTIFIED);
document.add(paragraph);
document.close();
}
}
Local Destination
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
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();
PdfWriter writerA = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
PdfWriter writerB = PdfWriter.getInstance(document, new FileOutputStream("3.pdf"));
document.open();
Font font = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.UNDERLINE, new Color(0, 0, 255));
Paragraph p1 = new Paragraph("1");
Chunk chunk = new Chunk(" 2", font);
chunk.setRemoteGoto("dog.pdf", "2");
p1.add(chunk);
Paragraph p3 = new Paragraph("test");
p3.add(new Chunk("test.", font).setLocalDestination("2"));
writerB.pause();
document.add(p1);
writerB.resume();
writerA.pause();
document.add(p3);
writerB.pause();
document.newPage();
document.add(new Paragraph("page 2"));
document.newPage();
document.add(new Paragraph("page 3"));
writerB.resume();
document.close();
}
}
PDF file with hyphenated text
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.HyphenationAuto;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document(PageSize.A6);
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
document.setMarginMirroring(true);
Chunk ck = new Chunk("this is a test");
HyphenationAuto auto = new HyphenationAuto("en", "GB", 2, 2);
ck.setHyphenation(auto);
Paragraph p = new Paragraph(ck);
p.setAlignment(Paragraph.ALIGN_JUSTIFIED);
document.add(p);
document.close();
}
}
Scale Chunk
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
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();
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
Chunk c = new Chunk("this is a test");
float w = c.getWidthPoint();
Paragraph p = new Paragraph("The width of the chunk: "");
p.add(c);
p.add("" is ");
p.add(String.valueOf(w));
p.add(" points or ");
p.add(String.valueOf(w / 72f));
p.add(" inches or ");
p.add(String.valueOf(w / 72f * 2.54f));
p.add(" cm.");
document.add(p);
document.add(c);
c.setHorizontalScaling(0.5f);
document.add(c);
document.close();
}
}
Skew a chunk
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
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();
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
Paragraph p;
Chunk chunk;
p = new Paragraph();
chunk = new Chunk(" this ");
chunk.setSkew(15f, -30f);
p.add(chunk);
chunk = new Chunk(" is a ");
chunk.setSkew(15f, 15f);
p.add(chunk);
chunk = new Chunk(" test.");
chunk.setSkew(-30f, 15f);
p.add(chunk);
document.add(p);
document.close();
}
}
Use TextRise of a chunk to create supscript and subscript
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
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();
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
float textrise = 6.0f;
Chunk c;
for (String s : "this is a test".split(" ")) {
c = new Chunk(s);
c.setTextRise(textrise);
c.setUnderline(new Color(0xC0, 0xC0, 0xC0), 0.2f, 0.0f, 0.0f, 0.0f,
PdfContentByte.LINE_CAP_BUTT);
document.add(c);
textrise -= 2.0f;
}
document.close();
}
}
Width of a chunk
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
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();
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
Chunk c = new Chunk("this is a test");
float w = c.getWidthPoint();
Paragraph p = new Paragraph("The width of the chunk: "");
p.add(c);
p.add("" is ");
p.add(String.valueOf(w));
p.add(" points or ");
p.add(String.valueOf(w / 72f));
p.add(" inches or ");
p.add(String.valueOf(w / 72f * 2.54f));
p.add(" cm.");
document.add(p);
document.add(c);
document.close();
}
}