Java Tutorial/PDF/Character
Версия от 17:44, 31 мая 2010; (обсуждение)
implements SplitCharacter
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.SplitCharacter;
import com.lowagie.text.pdf.PdfChunk;
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.HELVETICA, 18);
String text = "this is a test";
String url = "http://www.jexp.ru";
document.add(new Paragraph("Default split behavior."));
Paragraph p = new Paragraph(24, new Chunk(text, font));
Chunk urlChunk = new Chunk(url, font);
p.add(urlChunk);
document.add(p);
document.add(new Paragraph("this is a test."));
p = new Paragraph(24, new Chunk(text, font));
urlChunk = new Chunk(url, font);
urlChunk.setSplitCharacter(new MySplitCharacter());
p.add(urlChunk);
document.add(p);
document.close();
}
}
class MySplitCharacter implements SplitCharacter {
public boolean isSplitCharacter(int start, int current, int end, char[] cc, PdfChunk[] ck) {
char c;
if (ck == null)
c = cc[current];
else
c = ck[Math.min(current, ck.length - 1)].getUnicodeEquivalent(cc[current]);
return (c == "/" || c == " ");
}
}
Set Character Spacing
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfTextArray;
import com.lowagie.text.pdf.PdfWriter;
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();
String text = "jexp";
PdfContentByte cb = writer.getDirectContent();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
cb.beginText();
cb.moveText(36, 806);
cb.setFontAndSize(bf, 24);
cb.moveTextWithLeading(0, -36);
cb.showText(text);
cb.newlineText();
PdfTextArray array = new PdfTextArray("J");
array.add(120);
array.add("a");
array.add(120);
array.add("v");
array.add(95);
array.add("a2s");
cb.showText(array);
cb.setWordSpacing(0);
cb.setCharacterSpacing(0);
cb.endText();
document.close();
}
}