use of com.itextpdf.text.pdf.PdfContentByte in project trainning by fernandotomasio.
the class ReportUtil method setBackground.
public byte[] setBackground(byte[] documentSource, String url) {
PdfReader reader = null;
PdfStamper stamp = null;
ByteArrayOutputStream byteArray = null;
try {
byteArray = new ByteArrayOutputStream();
reader = new PdfReader(documentSource);
int n = reader.getNumberOfPages();
// Create a stamper that will copy the document to a new file
stamp = new PdfStamper(reader, byteArray);
int i = 1;
PdfContentByte under;
PdfContentByte over;
Image image = Image.getInstance(url);
image.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight());
image.setAbsolutePosition(0, 0);
// image.setAbsolutePosition(200, 400);
while (i <= n) {
// Watermark under the existing page
under = stamp.getUnderContent(i);
under.addImage(image);
// Text over the existing page
// over = stamp.getOverContent(i);
// over.beginText();
// over.setFontAndSize(bf, 18);
// over.showText("page " + i);
// over.endText();
i++;
}
stamp.close();
} catch (DocumentException ex) {
Logger.getLogger(ReportUtil.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException e) {
e.printStackTrace();
}
return byteArray.toByteArray();
}
use of com.itextpdf.text.pdf.PdfContentByte in project TranskribusCore by Transkribus.
the class TrpPdfDocument method addTextAndImage.
private void addTextAndImage(PcGtsType pc, int cutoffLeft, int cutoffTop, Image img, boolean imageOnly, ExportCache cache) throws DocumentException, IOException {
lineAndColorList.clear();
PdfContentByte cb = writer.getDirectContentUnder();
cb.setColorFill(BaseColor.BLACK);
cb.setColorStroke(BaseColor.BLACK);
// BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, "UTF-8", BaseFont.NOT_EMBEDDED);
if (!imageOnly) {
cb.beginLayer(ocrLayer);
cb.setFontAndSize(bfArial, 32);
List<TrpRegionType> regions = pc.getPage().getTextRegionOrImageRegionOrLineDrawingRegion();
/*
* use reading order comparator for sorting since at this time reading order is more trustable
* other sorting is not transitive and seldomly produces "Comparison violates its general contract" exception
*/
Collections.sort(regions, new TrpElementReadingOrderComparator<RegionType>(true));
for (RegionType r : regions) {
// TODO add paths for tables etc.
if (r instanceof TrpTableRegionType) {
exportTable(r, cb, cutoffLeft, cutoffTop, false, cache);
} else if (r instanceof TextRegionType) {
TextRegionType tr = (TextRegionType) r;
// PageXmlUtils.buildPolygon(tr.getCoords().getPoints()).getBounds().getMinX();
addTextFromTextRegion(tr, cb, cutoffLeft, cutoffTop, bfArial, cache);
}
}
// scale after calculating lineMeanHeightForAllRegions
// lineMeanHeight = lineMeanHeight/scaleFactorX;
cb.endLayer();
}
cb.beginLayer(imgLayer);
cb.addImage(img);
cb.endLayer();
if (highlightTags) {
highlightAllTagsOnImg(lineAndColorList, cb, cutoffLeft, cutoffTop);
}
/*
* draw tag lines
*/
// addTocLinks(doc, page,cutoffTop);
}
use of com.itextpdf.text.pdf.PdfContentByte in project gephi by gephi.
the class ArrowRenderer method render.
@Override
public void render(final Item item, final RenderTarget target, final PreviewProperties properties) {
final Helper h = new Helper(item, properties);
final Color color = EdgeRenderer.getColor(item, properties);
if (target instanceof G2DTarget) {
Graphics2D graphics = ((G2DTarget) target).getGraphics();
graphics.setColor(color);
final GeneralPath gpath = new GeneralPath();
gpath.moveTo(h.p1.x, h.p1.y);
gpath.lineTo(h.p2.x, h.p2.y);
gpath.lineTo(h.p3.x, h.p3.y);
gpath.closePath();
graphics.fill(gpath);
} else if (target instanceof SVGTarget) {
final SVGTarget svgTarget = (SVGTarget) target;
final Element arrowElem = svgTarget.createElement("polyline");
arrowElem.setAttribute("points", String.format(Locale.ENGLISH, "%f,%f %f,%f %f,%f", h.p1.x, h.p1.y, h.p2.x, h.p2.y, h.p3.x, h.p3.y));
arrowElem.setAttribute("class", String.format("%s %s", SVGUtils.idAsClassAttribute(((Node) h.sourceItem.getSource()).getId()), SVGUtils.idAsClassAttribute(((Node) h.targetItem.getSource()).getId())));
arrowElem.setAttribute("fill", svgTarget.toHexString(color));
arrowElem.setAttribute("fill-opacity", (color.getAlpha() / 255f) + "");
arrowElem.setAttribute("stroke", "none");
svgTarget.getTopElement(SVGTarget.TOP_ARROWS).appendChild(arrowElem);
} else if (target instanceof PDFTarget) {
final PDFTarget pdfTarget = (PDFTarget) target;
final PdfContentByte cb = pdfTarget.getContentByte();
cb.moveTo(h.p1.x, -h.p1.y);
cb.lineTo(h.p2.x, -h.p2.y);
cb.lineTo(h.p3.x, -h.p3.y);
cb.closePath();
cb.setRGBColorFill(color.getRed(), color.getGreen(), color.getBlue());
if (color.getAlpha() < 255) {
cb.saveState();
float alpha = color.getAlpha() / 255f;
PdfGState gState = new PdfGState();
gState.setFillOpacity(alpha);
cb.setGState(gState);
}
cb.fill();
if (color.getAlpha() < 255) {
cb.restoreState();
}
}
}
use of com.itextpdf.text.pdf.PdfContentByte in project Java-Matrix-Benchmark by lessthanoptimal.
the class UtilPlotPdf method saveAsPdf.
public static void saveAsPdf(LegendTitle legend, String FILENAME, int width, int height) {
Document document = new Document(new Rectangle(width, height));
try {
FileOutputStream file = new FileOutputStream(FILENAME);
PdfWriter writer = PdfWriter.getInstance(document, file);
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(width, height);
Graphics2D g2d = tp.createGraphics(width, height, new DefaultFontMapper());
Rectangle2D r2d = new Rectangle2D.Double(0, 0, width, height);
legend.draw(g2d, r2d);
g2d.dispose();
cb.addTemplate(tp, 0, 0);
document.close();
g2d.dispose();
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations