本文整理汇总了Java中com.itextpdf.text.Image.setAbsolutePosition方法的典型用法代码示例。如果您正苦于以下问题:Java Image.setAbsolutePosition方法的具体用法?Java Image.setAbsolutePosition怎么用?Java Image.setAbsolutePosition使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.itextpdf.text.Image
的用法示例。
在下文中一共展示了Image.setAbsolutePosition方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: stampLayer
import com.itextpdf.text.Image; //导入方法依赖的package包/类
public static byte[] stampLayer(InputStream _pdfFile, Image iImage, int x, int y, String layername, boolean readLayers) throws IOException, DocumentException
{
PdfReader reader = new PdfReader(_pdfFile);
try ( ByteArrayOutputStream ms = new ByteArrayOutputStream() )
{
PdfStamper stamper = new PdfStamper(reader, ms);
//Don't delete otherwise the stamper flattens the layers
if (readLayers)
stamper.getPdfLayers();
PdfLayer logoLayer = new PdfLayer(layername, stamper.getWriter());
PdfContentByte cb = stamper.getUnderContent(1);
cb.beginLayer(logoLayer);
//300dpi
iImage.scalePercent(24f);
iImage.setAbsolutePosition(x, y);
cb.addImage(iImage);
cb.endLayer();
stamper.close();
return (ms.toByteArray());
}
}
示例2: onEndPage
import com.itextpdf.text.Image; //导入方法依赖的package包/类
@Override
public void onEndPage(PdfWriter writer, Document document) {
try {
footer = buildPdfTable(PRINT_PAGE_FOOTER);
if (footer != null) {
printPageFooterBand();
Rectangle page = document.getPageSize();
footer.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
footer.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(), writer.getDirectContent());
String image = bean.getReportLayout().getBackgroundImage();
if (image != null) {
byte[] imageBytes = getImage(image);
Image pdfImage = Image.getInstance(imageBytes);
pdfImage.setAbsolutePosition(0, 0);
writer.getDirectContentUnder().addImage(pdfImage);
}
}
} catch (Exception e) {
throw new ExceptionConverter(e);
} finally {
pageNo = writer.getPageNumber();
}
}
示例3: createNDAImage
import com.itextpdf.text.Image; //导入方法依赖的package包/类
/**
* Create itextPdf image object with visitor signature.
*
* @param file
* @param imgHeight
* @param imgWidth
* @return com.itextpdf.text.Image
*/
public static Image createNDAImage(File file, int imgHeight, int imgWidth) {
try {
Image image = Image.getInstance(file.getAbsolutePath());
if (imgHeight!=0 && imgWidth!=0) {
image.scaleAbsolute(imgWidth, imgHeight);
} else {
image.scaleAbsolute(230, 140);
} image.setAbsolutePosition(70, 450);
return image;
} catch (BadElementException | IOException e) {
logger.error("Exception while creating image for NDA file. ",e);
return null;
}
}
示例4: tiff2Pdf
import com.itextpdf.text.Image; //导入方法依赖的package包/类
public void tiff2Pdf(File singleTifFile, File pdfFile) {
// based on tiff2pdf from itext toolbox 0.0.2
// (cfr.http://itexttoolbox.sourceforge.net/doku.php?id=download&DokuWiki=
// ecde1bfec0b8cca87dd8c6c042183992)
try {
RandomAccessFileOrArray ra = new RandomAccessFileOrArray(
singleTifFile.getAbsolutePath());
// RandomAccessFileOrArray ra = new
// RandomAccessFileOrArray(tempByteArray);
int comps = TiffImage.getNumberOfPages(ra);
Document document = new Document(PageSize.A4);
float width = PageSize.A4.getWidth();
float height = PageSize.A4.getHeight();
Image img = TiffImage.getTiffImage(ra, 1);
document.setPageSize(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(pdfFile));
// pdf/a
// from
// http://www.opensubscriber.com/message/[email protected]
// .net/7593470.html
// check that it is really pdf/a:
// http://www.intarsys.de/produkte/pdf-a-live/pdf-a-check-1
// => 2 warnings
// Keine eindeutige ID gefunden
// Kein History-Eintrag vorhanden
writer.setPDFXConformance(PdfWriter.PDFA1B);
document.open();
PdfDictionary outi = new PdfDictionary(PdfName.OUTPUTINTENT);
outi.put(PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString(
"sRGB IEC61966-2.1"));
outi.put(PdfName.INFO, new PdfString("sRGB IEC61966-2.1"));
outi.put(PdfName.S, PdfName.GTS_PDFA1);
ICC_Profile icc = ICC_Profile.getInstance(Thread.currentThread()
.getContextClassLoader().getResourceAsStream(
"/srgb.profile"));
PdfICCBased ib = new PdfICCBased(icc);
ib.remove(PdfName.ALTERNATE);
outi.put(PdfName.DESTOUTPUTPROFILE, writer.addToBody(ib)
.getIndirectReference());
writer.getExtraCatalog().put(PdfName.OUTPUTINTENTS,
new PdfArray(outi));
PdfContentByte cb = writer.getDirectContent();
for (int c = 0; c < comps; ++c) {
img = TiffImage.getTiffImage(ra, c + 1);
if (img != null) {
document.setPageSize(PageSize.A4);
document.newPage();
img.setAbsolutePosition(0, 0);
img.scaleToFit(width, height);
cb.addImage(img);
logger.debug("Finished page " + (c + 1));
}
}
ra.close();
writer.createXmpMetadata();// pdfa
document.close();
} catch (Throwable e) {
// catch Throwable because we encountere a java.lang.InternalError
// cfr. http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6503430
// probably better to move to later java version for poller
logger.error("Pdf not created", e);
}
}
示例5: addImageToPdf
import com.itextpdf.text.Image; //导入方法依赖的package包/类
/**
* The <code>addImageToPdf</code> method is used to add image to pdf and make it searchable by adding image text in invisible mode
* w.r.t parameter 'isPdfSearchable' passed.
*
* @param pdfWriter {@link PdfWriter} writer of pdf in which image has to be added
* @param htmlUrl {@link HocrPage} corresponding html file for fetching text and coordinates
* @param imageUrl {@link String} url of image to be added in pdf
* @param isPdfSearchable true for searchable pdf else otherwise
* @param widthOfLine
*/
private void addImageToPdf(PdfWriter pdfWriter, HocrPage hocrPage, String imageUrl, boolean isPdfSearchable, final int widthOfLine) {
if (null != pdfWriter && null != imageUrl && imageUrl.length() > 0) {
try {
LOGGER.info("Adding image" + imageUrl + " to pdf using iText");
Image pageImage = Image.getInstance(imageUrl);
float dotsPerPointX = pageImage.getDpiX() / PDF_RESOLUTION;
float dotsPerPointY = pageImage.getDpiY() / PDF_RESOLUTION;
PdfContentByte pdfContentByte = pdfWriter.getDirectContent();
pageImage.scaleToFit(pageImage.getWidth() / dotsPerPointX, pageImage.getHeight() / dotsPerPointY);
pageImage.setAbsolutePosition(0, 0);
// Add image to pdf
pdfWriter.getDirectContentUnder().addImage(pageImage);
pdfWriter.getDirectContentUnder().add(pdfContentByte);
// If pdf is to be made searchable
if (isPdfSearchable) {
LOGGER.info("Adding invisible text for image: " + imageUrl);
float pageImagePixelHeight = pageImage.getHeight();
Font defaultFont = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.BOLD, CMYKColor.BLACK);
// Fetch text and coordinates for image to be added
Map<String, int[]> textCoordinatesMap = getTextWithCoordinatesMap(hocrPage, widthOfLine);
Set<String> ketSet = textCoordinatesMap.keySet();
// Add text at specific location
for (String key : ketSet) {
int[] coordinates = textCoordinatesMap.get(key);
float bboxWidthPt = (coordinates[2] - coordinates[0]) / dotsPerPointX;
float bboxHeightPt = (coordinates[3] - coordinates[1]) / dotsPerPointY;
pdfContentByte.beginText();
// To make text added as invisible
pdfContentByte.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE);
pdfContentByte.setLineWidth(Math.round(bboxWidthPt));
// Ceil is used so that minimum font of any text is 1
// For exception of unbalanced beginText() and endText()
if (bboxHeightPt > 0.0) {
pdfContentByte.setFontAndSize(defaultFont.getBaseFont(), (float) Math.ceil(bboxHeightPt));
} else {
pdfContentByte.setFontAndSize(defaultFont.getBaseFont(), 1);
}
float xCoordinate = (float) (coordinates[0] / dotsPerPointX);
float yCoordinate = (float) ((pageImagePixelHeight - coordinates[3]) / dotsPerPointY);
pdfContentByte.moveText(xCoordinate, yCoordinate);
pdfContentByte.showText(key);
pdfContentByte.endText();
}
}
pdfContentByte.closePath();
} catch (BadElementException badElementException) {
LOGGER
.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: "
+ badElementException.toString());
} catch (DocumentException documentException) {
LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: " + documentException.toString());
} catch (MalformedURLException malformedURLException) {
LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: "
+ malformedURLException.toString());
} catch (IOException ioException) {
LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: " + ioException.toString());
}
}
}
示例6: CreatePDF_3DPrinter
import com.itextpdf.text.Image; //导入方法依赖的package包/类
public CreatePDF_3DPrinter(File file, ArrayList<String> infos, ArrayList<Image> imagelist) throws DocumentException, MalformedURLException, IOException{
result = file;
MyPDF = new Document(PageSize.A4, 50, 50, 80, 80);
MyWriter = PdfWriter.getInstance(MyPDF,new FileOutputStream(result));
MyWriter.setInitialLeading(160);
MyPDF.open();
Font font = new Font(Font.FontFamily.TIMES_ROMAN, 40, Font.NORMAL);
Paragraph pg = new Paragraph();
pg.setAlignment(Element.ALIGN_CENTER);
pg.setSpacingAfter(150);
pg.setFont(font);
pg.add(infos.get(0));
pg.setLeading(2,1);
MyPDF.add(pg);
Image image = Image.getInstance(loadImage("/logo.png"), null);
image.setAbsolutePosition((MyPDF.getPageSize().getWidth()/2) - (image.getWidth()/2), 580f);
if(infos.get(0).length()<25){
image.setAbsolutePosition((MyPDF.getPageSize().getWidth()/2) - (image.getWidth()/2), 620f);
}
MyPDF.add(image);
MyPDF.add(createFrontPageTable(infos));
if(imagelist!=null){
tilingImages(imagelist);
}
MyPDF.close();
}
示例7: CreatePDF_LaserCutting
import com.itextpdf.text.Image; //导入方法依赖的package包/类
public CreatePDF_LaserCutting(File file, ArrayList<String> infos, ArrayList<Image> imagelist) throws DocumentException, MalformedURLException, IOException{
result = file;
MyPDF = new Document(PageSize.A4, 50, 50, 80, 80);
MyWriter = PdfWriter.getInstance(MyPDF,new FileOutputStream(result));
MyWriter.setInitialLeading(160);
MyPDF.open();
Font font = new Font(Font.FontFamily.TIMES_ROMAN, 40, Font.NORMAL);
Paragraph pg = new Paragraph();
pg.setAlignment(Element.ALIGN_CENTER);
pg.setSpacingAfter(150);
pg.setFont(font);
pg.add(infos.get(0));
pg.setLeading(2,1);
MyPDF.add(pg);
Image image = Image.getInstance(loadImage("/logo.png"), null);
image.setAbsolutePosition((MyPDF.getPageSize().getWidth()/2) - (image.getWidth()/2), 580f);
if(infos.get(0).length()<25){
image.setAbsolutePosition((MyPDF.getPageSize().getWidth()/2) - (image.getWidth()/2), 620f);
}
MyPDF.add(image);
MyPDF.add(createFrontPageTable(infos));
if(imagelist!=null){
tilingImages(imagelist);
}
MyPDF.close();
}
示例8: convertToIMG
import com.itextpdf.text.Image; //导入方法依赖的package包/类
public static void convertToIMG(String[] RESOURCES,String result,String path,int s) throws FileNotFoundException, DocumentException, BadElementException, IOException {
System.out.println(":)");
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(result));
// step 3
document.open();
// step 4
// Adding a series of images
Image img;
System.out.println(":) :)");
for (String image:RESOURCES) {
System.out.println(image);
img = Image.getInstance(path+image);
document.setPageSize(img);
document.newPage();
img.setAbsolutePosition(0, 0);
document.add(img);
System.out.println(":(");
}
System.out.println(":) :) :) Pdf is outo");
// step 5
document.close();
if(s==1){
JOptionPane.showMessageDialog(null,"Converted Successfully");
//JOptionPane.showMessageDialog(null, "Finished\nFile save in :\nC:\\DjVu++Task\\ImagestoPDF\\"+RESOURCES[0].substring(0,RESOURCES[0].lastIndexOf("."))+".pdf");
}
}
示例9: addLogo
import com.itextpdf.text.Image; //导入方法依赖的package包/类
public void addLogo(PdfContentByte cb, String imagePath, float xPos, float yPos) throws MalformedURLException, IOException, DocumentException {
URL imageUrl = getClass().getResource(imagePath + "/IPAT_logo_Relaunch2016_RZ_RGB.jpg");
final Image logoImage = Image.getInstance(imageUrl);
logoImage.setAbsolutePosition(xPos, yPos);
logoImage.scalePercent(3f, 3f);
cb.addImage(logoImage);
}
示例10: createPdf
import com.itextpdf.text.Image; //导入方法依赖的package包/类
/**
* Creates a PDF document.
* @param filename the path to the new PDF document, UPC SKU BinRack
* Can be null, Default values set statically
* @throws DocumentException
* @throws IOException
*/
public static void createPdf(String filename, String upc, String sku, String binRack)
throws IOException, DocumentException {
if(filename==null || filename.isEmpty())
filename = RESULT;
if(upc==null || upc.isEmpty()){
upc = getUpc();
}else{
setUpc(upc);
}
if(sku==null || sku.isEmpty()){
sku = getSku();
}else{
setSku(sku.toUpperCase());
}
if(binRack==null || binRack.isEmpty()){
getBinRack();
}
else{
setBinRack(binRack);
}
// step 1
// 1 inch = 72
// thus 2.25" x 1.25" = 162 x 90
Document document = new Document();
document.setPageSize(new Rectangle(WIDTH,HEIGHT));
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
PdfContentByte cb = writer.getDirectContent();
// UPC A
//Generate Image of UPC-A
Image barImage = generateUPC(cb,upc);
//barImage.setRotationDegrees(90f);
barImage.setAbsolutePosition(7,10);
barImage.setRotationDegrees(90f);
//Add this Image to Document
document.add(barImage);
//Coda128
barImage = generate128(cb,sku);
//barImage.setRotationDegrees(90f);
barImage.setAbsolutePosition(37,10);
barImage.setRotationDegrees(90f);
//Add this Image to Document
document.add(barImage);
//Text - BinRack
BaseFont bf = BaseFont.createFont();
cb.beginText();
cb.setFontAndSize(bf, 24);
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, binRack, 27, 90, 90f);
cb.endText();
// step 5
document.close();
}
示例11: convert
import com.itextpdf.text.Image; //导入方法依赖的package包/类
@Override
public void convert() throws Exception {
loading();
Dimension pgsize = processSlides();
processing();
double zoom = 2; // magnify it by 2 as typical slides are low res
AffineTransform at = new AffineTransform();
at.setToScale(zoom, zoom);
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, outStream);
document.open();
for (int i = 0; i < getNumSlides(); i++) {
BufferedImage bufImg = new BufferedImage((int)Math.ceil(pgsize.width*zoom), (int)Math.ceil(pgsize.height*zoom), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = bufImg.createGraphics();
graphics.setTransform(at);
//clear the drawing area
graphics.setPaint(getSlideBGColor(i));
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
try{
drawOntoThisGraphic(i, graphics);
} catch(Exception e){
//Just ignore, draw what I have
}
Image image = Image.getInstance(bufImg, null);
document.setPageSize(new Rectangle(image.getScaledWidth(), image.getScaledHeight()));
document.newPage();
image.setAbsolutePosition(0, 0);
document.add(image);
}
//Seems like I must close document if not output stream is not complete
document.close();
//Not sure what repercussions are there for closing a writer but just do it.
writer.close();
finished();
}