itextpdf paragraph使用
*****************
相关类与接口
Paragraph:文本段落
public class Paragraph extends BlockElement<Paragraph> {
protected DefaultAccessibilityProperties tagProperties;
public Paragraph() {
public Paragraph(String text) {
public Paragraph(Text text) {
public Paragraph add(String text) {
public Paragraph add(ILeafElement element) {
public Paragraph add(IBlockElement element) {
public <T2 extends ILeafElement> Paragraph addAll(List<T2> elements) {
public Paragraph addTabStops(TabStop... tabStops) {
public Paragraph addTabStops(List<TabStop> tabStops) {
public Paragraph removeTabStop(float tabStopPosition) {
public Paragraph setFirstLineIndent(float indent) {
public Paragraph setOrphansControl(ParagraphOrphansControl orphansControl) {
public Paragraph setWidowsControl(ParagraphWidowsControl widowsControl) {
public Paragraph setFixedLeading(float leading) {
public Paragraph setMultipliedLeading(float leading) {
public <T1> T1 getDefaultProperty(int property) {
public AccessibilityProperties getAccessibilityProperties() {
protected IRenderer makeNewRenderer() {
private void addTabStopsAsProperty(List<TabStop> newTabStops) {
Text:文本内容
public class Text extends AbstractElement<Text> implements ILeafElement, IAccessibleElement {
protected String text;
protected DefaultAccessibilityProperties tagProperties;
public Text(String text) {
public void setText(String text) {
public Text setTextRise(float textRise) {
public Text setSkew(float alpha, float beta) {
public Text setHorizontalScaling(float horizontalScaling) {
public String getText() {
public float getTextRise() {
public Float getHorizontalScaling() {
public AccessibilityProperties getAccessibilityProperties() {
protected IRenderer makeNewRenderer() {
Div:将paragraph放进div中,进行文本布局
public class Div extends BlockElement<Div> {
protected DefaultAccessibilityProperties tagProperties;
public Div() {
}
public Div add(IBlockElement element) {
public Div add(Image element) {
public Div add(AreaBreak areaBreak) {
public AccessibilityProperties getAccessibilityProperties() {
public Div setFillAvailableArea(boolean fillArea) {
public Div setFillAvailableAreaOnSplit(boolean fillAreaOnSplit) {
protected IRenderer makeNewRenderer() {
BlockElement:内外边距、行宽高等设置
public abstract class BlockElement<T extends IElement> extends AbstractElement<T> implements IAccessibleElement, IBlockElement {
protected BlockElement() {
}
public <T1> T1 getDefaultProperty(int property) {
switch(property) {
case 103:
case 104:
return OverflowPropertyValue.FIT;
default:
return super.getDefaultProperty(property);
}
}
***********
内边距
public T setPaddingLeft(float value) {
public T setPaddingRight(float value) {
public T setPaddingTop(float value) {
public T setPaddingBottom(float value) {
public UnitValue getPaddingLeft() {
public UnitValue getPaddingRight() {
public UnitValue getPaddingTop() {
public UnitValue getPaddingBottom() {
public T setPadding(float commonPadding) {
public T setPaddings(float paddingTop, float paddingRight, float paddingBottom, float paddingLeft) {
***********
页边距
public T setMarginLeft(float value) {
public T setMarginRight(float value) {
public T setMarginTop(float value) {
public T setMarginBottom(float value) {
public UnitValue getMarginLeft() {
public UnitValue getMarginRight() {
public UnitValue getMarginTop() {
public UnitValue getMarginBottom() {
public T setMargin(float commonMargin) {
public T setMargins(float marginTop, float marginRight, float marginBottom, float marginLeft) {
***********
宽度、高度
public T setWidth(float width) {
public T setWidth(UnitValue width) {
public UnitValue getWidth() {
public T setHeight(UnitValue height) {
public T setHeight(float height) {
public UnitValue getHeight() {
public T setMaxHeight(float maxHeight) {
public T setMaxHeight(UnitValue maxHeight) {
public T setMinHeight(UnitValue minHeight) {
public T setMinHeight(float minHeight) {
public T setMaxWidth(UnitValue maxWidth) {
public T setMaxWidth(float maxWidth) {
public T setMinWidth(UnitValue minWidth) {
public T setMinWidth(float minWidth) {
***********
其余操作
public T setVerticalAlignment(VerticalAlignment verticalAlignment) {
public T setSpacingRatio(float ratio) {
public Boolean isKeepTogether() {
public T setKeepTogether(boolean keepTogether) {
public Boolean isKeepWithNext() {
public T setKeepWithNext(boolean keepWithNext) {
public T setRotationAngle(float angleInRadians) {
public T setRotationAngle(double angleInRadians) {
AbstractElement:样式、页面动作等设置
public abstract class AbstractElement<T extends IElement> extends ElementPropertyContainer<T> implements IAbstractElement {
protected IRenderer nextRenderer;
protected List<IElement> childElements = new ArrayList();
protected Set<Style> styles;
public AbstractElement() {
}
public IRenderer createRendererSubTree() {
public void setNextRenderer(IRenderer renderer) {
public IRenderer getRenderer() {
public T addStyle(Style style) { //添加样式
public T setAction(PdfAction action) { //页面动作:页面跳转、超链接等
public T setPageNumber(int pageNumber) { //设置页码
public boolean isEmpty() {
public List<IElement> getChildren() {
public boolean hasProperty(int property) {
public <T1> T1 getProperty(int property) {
protected abstract IRenderer makeNewRenderer();
}
Style:样式
public class Style extends ElementPropertyContainer<Style> {
public Style() {
public Style(Style style) {
**********
页边距
public Style setMarginTop(float value) {
public Style setMarginLeft(float value) {
public Style setMarginRight(float value) {
public Style setMarginBottom(float value) {
public UnitValue getMarginTop() {
public UnitValue getMarginLeft() {
public UnitValue getMarginRight() {
public UnitValue getMarginBottom() {
public Style setMargin(float commonMargin) {
public Style setMargins(float marginTop, float marginRight, float marginBottom, float marginLeft) {
**********
内边距
public Style setPaddingTop(float value) {
public Style setPaddingLeft(float value) {
public Style setPaddingRight(float value) {
public Style setPaddingBottom(float value) {
public UnitValue getPaddingTop() {
public UnitValue getPaddingLeft() {
public UnitValue getPaddingRight() {
public UnitValue getPaddingBottom() {
public Style setPadding(float commonPadding) {
public Style setPaddings(float paddingTop, float paddingRight, float paddingBottom, float paddingLeft) {
**********
高度、宽度
public Style setWidth(float width) {
public Style setWidth(UnitValue width) {
public UnitValue getWidth() {
public Style setHeight(UnitValue height) {
public Style setHeight(float height) {
public UnitValue getHeight() {
public Style setMaxHeight(float maxHeight) {
public Style setMaxHeight(UnitValue maxHeight) {
public Style setMinHeight(UnitValue minHeight) {
public Style setMinHeight(float minHeight) {
public Style setMaxWidth(UnitValue maxWidth) {
public Style setMaxWidth(float maxWidth) {
public Style setMinWidth(UnitValue minWidth) {
public Style setMinWidth(float minWidth) {
**********
其余操作
public Boolean isKeepTogether() {
public Style setKeepTogether(boolean keepTogether) {
public Style setRotationAngle(float radAngle) {
public Style setRotationAngle(double angle) {
public Style setSpacingRatio(float ratio) {
public Style setVerticalAlignment(VerticalAlignment verticalAlignment) {
ElementPropertyContainer:位置、字体、边框、背景等设置
public abstract class ElementPropertyContainer<T extends IPropertyContainer> extends AbstractIdentifiableElement implements IPropertyContainer {
protected Map<Integer, Object> properties = new HashMap();
public ElementPropertyContainer() {
}
*********
property
public boolean hasProperty(int property) {
public <T1> T1 getProperty(int property) {
public void setProperty(int property, Object value) {
public boolean hasOwnProperty(int property) {
public <T1> T1 getOwnProperty(int property) {
public void deleteOwnProperty(int property) {
public <T1> T1 getDefaultProperty(int property) {
*********
position
public T setRelativePosition(float left, float top, float right, float bottom) {
public T setFixedPosition(float left, float bottom, float width) {
public T setFixedPosition(float left, float bottom, UnitValue width) {
public T setFixedPosition(int pageNumber, float left, float bottom, float width) {
public T setFixedPosition(int pageNumber, float left, float bottom, UnitValue width) {
*********
font
public T setFont(PdfFont font) {
public T setFontFamily(String... fontFamilyNames) {
public T setFontFamily(List<String> fontFamilyNames) {
public T setFontColor(Color fontColor) {
public T setFontColor(Color fontColor, float opacity) {
public T setFontSize(float fontSize) {
public T setFontKerning(FontKerning fontKerning) {
public T setFontScript(UnicodeScript script) {
public T setBold() { //字体加粗
public T setItalic() { //字体倾斜
*********
background
public T setBackgroundColor(Color backgroundColor) {
public T setBackgroundColor(Color backgroundColor, float opacity) {
public T setBackgroundColor(Color backgroundColor, float extraLeft, float extraTop, float extraRight, float extraBottom) {
public T setBackgroundColor(Color backgroundColor, float opacity, float extraLeft, float extraTop, float extraRight, float extraBottom) {
public T setBackgroundImage(BackgroundImage image) {
public T setBackgroundImage(List<BackgroundImage> imagesList) {
*********
border
public T setBorder(Border border) {
public T setBorderTop(Border border) {
public T setBorderLeft(Border border) {
public T setBorderRight(Border border) {
public T setBorderBottom(Border border) {
public T setBorderRadius(BorderRadius borderRadius) {
public T setBorderTopLeftRadius(BorderRadius borderRadius) {
public T setBorderTopRightRadius(BorderRadius borderRadius) {
public T setBorderBottomRightRadius(BorderRadius borderRadius) {
public T setBorderBottomLeftRadius(BorderRadius borderRadius) {
public T setStrokeColor(Color strokeColor) {
public Color getStrokeColor() { //边框颜色
public T setStrokeWidth(float strokeWidth) {
public Float getStrokeWidth() { //边框宽度
*********
underline
public T setUnderline() { //文本加下划线
public T setLineThrough() { //文本加删除线
public T setUnderline(float thickness, float yPosition) {
public T setUnderline(Color color, float thickness, float thicknessMul, float yPosition, float yPositionMul, int lineCapStyle) {
public T setUnderline(Color color, float opacity, float thickness, float thicknessMul, float yPosition, float yPositionMul, int lineCapStyle) {
*********
其余操作
public T setHorizontalAlignment(HorizontalAlignment horizontalAlignment) {
public T setTextAlignment(TextAlignment alignment) { //文本对齐方式
public T setCharacterSpacing(float charSpacing) {
public T setWordSpacing(float wordSpacing) { //单词间距
public T setTextRenderingMode(int textRenderingMode) {
public Integer getTextRenderingMode() { //文本渲染方式
public T setSplitCharacters(ISplitCharacters splitCharacters) {
public ISplitCharacters getSplitCharacters() { //文本分隔符
public T setBaseDirection(BaseDirection baseDirection) {
public T setDestination(String destination) {
public T setOpacity(Float opacity) { //文本透明度
public T setHyphenation(HyphenationConfig hyphenationConfig) { //设置连字符
DeviceRgb:自定义颜色
public class DeviceRgb extends Color {
public static final Color BLACK = new DeviceRgb(0, 0, 0);
public static final Color WHITE = new DeviceRgb(255, 255, 255);
public static final Color RED = new DeviceRgb(255, 0, 0);
public static final Color GREEN = new DeviceRgb(0, 255, 0);
public static final Color BLUE = new DeviceRgb(0, 0, 255);
public DeviceRgb(int r, int g, int b) {
public DeviceRgb(float r, float g, float b) {
public DeviceRgb(java.awt.Color color) {
public DeviceRgb() {
this(0.0F, 0.0F, 0.0F);
}
public static DeviceRgb makeLighter(DeviceRgb rgbColor) {
public static DeviceRgb makeDarker(DeviceRgb rgbColor) {
ColorConstants
public class ColorConstants {
public static final Color BLACK;
public static final Color BLUE;
public static final Color CYAN;
public static final Color DARK_GRAY;
public static final Color GRAY;
public static final Color GREEN;
public static final Color LIGHT_GRAY;
public static final Color MAGENTA;
public static final Color ORANGE;
public static final Color PINK;
public static final Color RED;
public static final Color WHITE;
public static final Color YELLOW;
public ColorConstants() {
}
static {
BLACK = DeviceRgb.BLACK;
BLUE = DeviceRgb.BLUE;
CYAN = new DeviceRgb(0, 255, 255);
DARK_GRAY = new DeviceRgb(64, 64, 64);
GRAY = new DeviceRgb(128, 128, 128);
GREEN = DeviceRgb.GREEN;
LIGHT_GRAY = new DeviceRgb(192, 192, 192);
MAGENTA = new DeviceRgb(255, 0, 255);
ORANGE = new DeviceRgb(255, 200, 0);
PINK = new DeviceRgb(255, 175, 175);
RED = DeviceRgb.RED;
WHITE = DeviceRgb.WHITE;
YELLOW = new DeviceRgb(255, 255, 0);
}
}
*****************
示例
public class Test4 {
private static final String dest = "./test/4.pdf";
private static final String dest2 = "./test/5.pdf";
private static final String fontPath = "./fonts/simkai.ttf";
public static void fun() throws Exception{
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest));
pdfDocument.setDefaultPageSize(PageSize.A4);
Document document = new Document(pdfDocument);
PdfFont font = PdfFontFactory.createFont(fontPath);
document.setFont(font);
Div div = new Div();
div.setTextAlignment(TextAlignment.CENTER);
Paragraph paragraph = new Paragraph();
paragraph.setFirstLineIndent(2)
.add(new Text("静夜思\n").setTextAlignment(TextAlignment.CENTER)
.setFontColor(ColorConstants.ORANGE))
.add(new Text("床前明月光,疑是地上霜。\n举头望明月,低头思故乡。")
.setTextAlignment(TextAlignment.CENTER)
.setFontColor(new DeviceRgb(204, 102, 160)));
div.add(paragraph);
document.add(div);
document.add(new AreaBreak(AreaBreakType.NEXT_AREA));
//区域间隔要在document上添加,在div上添加无效
Div div2 = new Div();
div2.setTextAlignment(TextAlignment.CENTER);
Paragraph paragraph2 = new Paragraph();
paragraph2.addTabStops(new TabStop(20))
.add(new Text("春晓\n"))
.add(new Text("春眠不觉晓,处处闻啼鸟。\n夜来风雨声,花落知多少。"));
div2.add(paragraph2);
document.add(div2);
document.close();
}
public static void fun2() throws Exception{
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest2));
pdfDocument.setDefaultPageSize(PageSize.A4);
Document document = new Document(pdfDocument);
PdfFont font = PdfFontFactory.createFont(fontPath);
document.setFont(font);
Div div = new Div();
Paragraph paragraph = new Paragraph();
paragraph.setFirstLineIndent(25) //首行缩进
.add(new Text("这长达数千公里的槽脊,就是一段大气长波,它的周期大约是3到5天左右," +
"所以冬季冷空气活动也因此具有一定规律性。冷空气的周期和强度都会受槽脊强弱影响," +
"所以如果某次冷空气刚好赶上了周末,在下个周末又撞上新一股冷空气,也是可能的。"));
div.add(paragraph);
document.add(div);
document.close();
}
public static void main(String[] args) throws Exception{
fun();
fun2();
}
}
fun
fun2