use of com.itextpdf.layout.Canvas in project i7js-highlevel by itext.
the class CanvasFontMethods method createPdf.
public void createPdf(String dest) throws IOException {
// Initialize PDF document
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
// Initialize document
PdfPage page = pdf.addNewPage();
PdfCanvas pdfCanvas = new PdfCanvas(page);
Rectangle rectangle = new Rectangle(36, 36, 523, 770);
Canvas canvas = new Canvas(pdfCanvas, rectangle);
Paragraph p;
p = new Paragraph("Testing font methods");
canvas.add(p);
PdfFont font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
canvas.setFont(font);
p = new Paragraph("Testing font methods: changed font");
canvas.add(p);
canvas.setFontSize(18);
p = new Paragraph("Testing font methods: changed font size");
canvas.add(p);
canvas.setFontColor(ColorConstants.BLUE);
p = new Paragraph("Testing font methods: changed color");
canvas.add(p);
canvas.setBold();
p = new Paragraph("Testing font methods: to bold");
canvas.add(p);
canvas.setItalic();
p = new Paragraph("Testing font methods: to italic");
canvas.add(p);
canvas.setProperty(Property.BOLD_SIMULATION, false);
canvas.setProperty(Property.ITALIC_SIMULATION, false);
canvas.setProperty(Property.FONT_COLOR, null);
p = new Paragraph("Testing font methods: resetting style and color");
canvas.add(p);
canvas.setLineThrough();
p = new Paragraph("Testing font methods: line through (default)");
canvas.add(p);
canvas.setProperty(Property.UNDERLINE, null);
canvas.setUnderline();
p = new Paragraph("Testing font methods: underline (default)");
canvas.add(p);
canvas.setProperty(Property.UNDERLINE, null);
canvas.setUnderline(2, 4);
canvas.setUnderline(ColorConstants.BLUE, 5, 0.1f, 2, -0.5f, PdfCanvasConstants.LineCapStyle.ROUND);
p = new Paragraph("Testing font methods: underline (custom)");
canvas.add(p);
canvas.setProperty(Property.UNDERLINE, null);
canvas.setTextRenderingMode(PdfCanvasConstants.TextRenderingMode.STROKE);
p = new Paragraph("Testing font methods: change text rendering mode");
canvas.add(p);
canvas.setStrokeWidth(0.1f);
canvas.setStrokeColor(ColorConstants.BLUE);
p = new Paragraph("Testing font methods: change stroke width and color");
canvas.add(p);
// Close document
pdf.close();
}
use of com.itextpdf.layout.Canvas in project pdf-editor by Pdf-Creator.
the class PDFDocument method addRectangleWithTableItem.
public void addRectangleWithTableItem(TableItem tableItem) {
PdfCanvas canvas = new PdfCanvas(currentPage);
Rectangle rect = new Rectangle(tableItem.getX(), tableItem.getY(), tableItem.getW(), tableItem.getH());
canvas.setStrokeColor(tableItem.getRectangleStrokeColor()).setFillColor(tableItem.getRectangleFillColor()).rectangle(rect).fill().stroke();
Table table = new Table(tableItem.getCols());
table.setFontColor(tableItem.getFontColor());
for (String cellContent : tableItem.getCellContents()) {
table.addCell(cellContent);
}
new Canvas(canvas, rect).add(table);
}
use of com.itextpdf.layout.Canvas in project pdf-editor by Pdf-Creator.
the class PDFDocument method addRectangleWithFormulaItem.
public void addRectangleWithFormulaItem(FormulaItem formulaItem) throws IOException {
PdfCanvas canvas = new PdfCanvas(currentPage);
Rectangle rect = new Rectangle(formulaItem.getX(), formulaItem.getY(), formulaItem.getW(), formulaItem.getW());
canvas.setStrokeColor(formulaItem.getRectangleStrokeColor()).setFillColor(formulaItem.getRectangleStrokeColor()).rectangle(rect).fill().stroke();
// very strange method
TeXFormula tf = new TeXFormula(formulaItem.getFormula());
TeXIcon ti = tf.createTeXIcon(TeXConstants.STYLE_DISPLAY, formulaItem.getFontSize());
BufferedImage bimg = new BufferedImage(ti.getIconWidth(), ti.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
// -- painting the formula --
Graphics2D g2d = bimg.createGraphics();
g2d.setColor(Color.white);
g2d.fillRect(0, 0, ti.getIconWidth(), ti.getIconHeight());
JLabel jl = new JLabel();
jl.setForeground(new Color(0, 0, 0));
ti.paintIcon(jl, g2d, 0, 0);
// --------------------------
// -- adding it to pdf --
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(bimg, "png", byteArrayOutputStream);
ImageData data = ImageDataFactory.create(byteArrayOutputStream.toByteArray());
Image image = new Image(data);
image.setFixedPosition(formulaItem.getX(), formulaItem.getY());
image.scaleAbsolute(formulaItem.getW(), formulaItem.getH());
new Canvas(canvas, rect).add(image);
}
use of com.itextpdf.layout.Canvas in project i7j-pdfsweep by itext.
the class PdfCleanUpTool method drawOverlayText.
private void drawOverlayText(PdfCanvas canvas, String overlayText, Rectangle annotRect, PdfBoolean repeat, PdfString defaultAppearance, int justification) throws IOException {
Map<String, List> parsedDA;
try {
parsedDA = parseDAParam(defaultAppearance);
} catch (NullPointerException npe) {
throw new PdfException(CleanupExceptionMessageConstant.DEFAULT_APPEARANCE_NOT_FOUND);
}
PdfFont font;
float fontSize = 12;
List fontArgs = parsedDA.get("Tf");
PdfDictionary formDictionary = pdfDocument.getCatalog().getPdfObject().getAsDictionary(PdfName.AcroForm);
if (fontArgs != null && formDictionary != null) {
font = getFontFromAcroForm((PdfName) fontArgs.get(0));
fontSize = ((PdfNumber) fontArgs.get(1)).floatValue();
} else {
font = PdfFontFactory.createFont();
}
if (pdfDocument.isTagged()) {
canvas.openTag(new CanvasArtifact());
}
Canvas modelCanvas = new Canvas(canvas, annotRect, false);
Paragraph p = new Paragraph(overlayText).setFont(font).setFontSize(fontSize).setMargin(0);
TextAlignment textAlignment = TextAlignment.LEFT;
switch(justification) {
case 1:
textAlignment = TextAlignment.CENTER;
break;
case 2:
textAlignment = TextAlignment.RIGHT;
break;
default:
}
p.setTextAlignment(textAlignment);
List strokeColorArgs = parsedDA.get("StrokeColor");
if (strokeColorArgs != null) {
p.setStrokeColor(getColor(strokeColorArgs));
}
List fillColorArgs = parsedDA.get("FillColor");
if (fillColorArgs != null) {
p.setFontColor(getColor(fillColorArgs));
}
modelCanvas.add(p);
if (repeat != null && repeat.getValue()) {
boolean hasFull = modelCanvas.getRenderer().hasProperty(Property.FULL);
boolean isFull = hasFull ? (boolean) modelCanvas.getRenderer().getPropertyAsBoolean(Property.FULL) : false;
while (!isFull) {
p.add(overlayText);
LayoutArea previousArea = modelCanvas.getRenderer().getCurrentArea().clone();
modelCanvas.relayout();
if (modelCanvas.getRenderer().getCurrentArea().equals(previousArea)) {
// Avoid infinite loop. This might be caused by the fact that the font does not support the text we want to show
break;
}
hasFull = modelCanvas.getRenderer().hasProperty(Property.FULL);
isFull = hasFull ? (boolean) modelCanvas.getRenderer().getPropertyAsBoolean(Property.FULL) : false;
}
}
modelCanvas.getRenderer().flush();
if (pdfDocument.isTagged()) {
canvas.closeTag();
}
}
use of com.itextpdf.layout.Canvas in project axelor-open-suite by axelor.
the class TableFooterEventHandler method handleEvent.
@SuppressWarnings("resource")
@Override
public void handleEvent(Event event) {
PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
PdfDocument pdfDoc = docEvent.getDocument();
PdfPage page = docEvent.getPage();
PdfCanvas canvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc);
Rectangle rect1 = new Rectangle(pdfDoc.getDefaultPageSize().getX() + doc.getLeftMargin(), pdfDoc.getDefaultPageSize().getBottom() - doc.getBottomMargin(), 100, 90);
new Canvas(canvas, pdfDoc, rect1).add(tableFooter);
}
Aggregations