List of usage examples for com.lowagie.text.pdf PdfImportedPage getWidth
public float getWidth()
From source file:br.gov.jfrj.itextpdf.Documento.java
License:Open Source License
public static byte[] stamp(byte[] abPdf, String sigla, boolean rascunho, boolean cancelado, boolean semEfeito, boolean internoProduzido, String qrCode, String mensagem, Integer paginaInicial, Integer paginaFinal, Integer cOmitirNumeracao, String instancia, String orgaoUsu) throws DocumentException, IOException { PdfReader pdfIn = new PdfReader(abPdf); Document doc = new Document(PageSize.A4, 0, 0, 0, 0); // final SimpleDateFormat sdf = new SimpleDateFormat( // "EEE MMM dd HH:mm:ss zzz yyyy"); // doc.add(new Meta("creationdate", sdf.format(new Date(0L)))); final ByteArrayOutputStream boA4 = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(doc, boA4); doc.open();/*from w w w . j a v a2 s. com*/ PdfContentByte cb = writer.getDirectContent(); // Resize every page to A4 size // // double thetaRotation = 0.0; for (int i = 1; i <= pdfIn.getNumberOfPages(); i++) { int rot = pdfIn.getPageRotation(i); float left = pdfIn.getPageSize(i).getLeft(); float bottom = pdfIn.getPageSize(i).getBottom(); float top = pdfIn.getPageSize(i).getTop(); float right = pdfIn.getPageSize(i).getRight(); PdfImportedPage page = writer.getImportedPage(pdfIn, i); float w = page.getWidth(); float h = page.getHeight(); // Logger.getRootLogger().error("----- dimensoes: " + rot + ", " + w // + ", " + h); doc.setPageSize((rot != 0 && rot != 180) ^ (w > h) ? PageSize.A4.rotate() : PageSize.A4); doc.newPage(); cb.saveState(); if (rot != 0 && rot != 180) { float swap = w; w = h; h = swap; } float pw = doc.getPageSize().getWidth(); float ph = doc.getPageSize().getHeight(); double scale = Math.min(pw / w, ph / h); // do my transformations : cb.transform(AffineTransform.getScaleInstance(scale, scale)); if (!internoProduzido) { cb.transform(AffineTransform.getTranslateInstance(pw * SAFETY_MARGIN, ph * SAFETY_MARGIN)); cb.transform(AffineTransform.getScaleInstance(1.0f - 2 * SAFETY_MARGIN, 1.0f - 2 * SAFETY_MARGIN)); } if (rot != 0) { double theta = -rot * (Math.PI / 180); if (rot == 180) { cb.transform(AffineTransform.getRotateInstance(theta, w / 2, h / 2)); } else { cb.transform(AffineTransform.getRotateInstance(theta, h / 2, w / 2)); } if (rot == 90) { cb.transform(AffineTransform.getTranslateInstance((w - h) / 2, (w - h) / 2)); } else if (rot == 270) { cb.transform(AffineTransform.getTranslateInstance((h - w) / 2, (h - w) / 2)); } } // Logger.getRootLogger().error( // "----- dimensoes: " + rot + ", " + w + ", " + h); // Logger.getRootLogger().error("----- page: " + pw + ", " + ph); // cb.transform(AffineTransform.getTranslateInstance( // ((pw / scale) - w) / 2, ((ph / scale) - h) / 2)); // put the page cb.addTemplate(page, 0, 0); // draw a red rectangle at the page borders // // cb.saveState(); // cb.setColorStroke(Color.red); // cb.rectangle(pdfIn.getPageSize(i).getLeft(), pdfIn.getPageSize(i) // .getBottom(), pdfIn.getPageSize(i).getRight(), pdfIn // .getPageSize(i).getTop()); // cb.stroke(); // cb.restoreState(); cb.restoreState(); } doc.close(); abPdf = boA4.toByteArray(); final ByteArrayOutputStream bo2 = new ByteArrayOutputStream(); final PdfReader reader = new PdfReader(abPdf); final int n = reader.getNumberOfPages(); final PdfStamper stamp = new PdfStamper(reader, bo2); // adding content to each page int i = 0; PdfContentByte under; PdfContentByte over; final BaseFont helv = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false); // Image img = Image.getInstance("watermark.jpg"); final BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED); byte maskr[] = { (byte) 0xff }; Image mask = Image.getInstance(1, 1, 1, 1, maskr); mask.makeMask(); mask.setInverted(true); while (i < n) { i++; // watermark under the existing page under = stamp.getUnderContent(i); over = stamp.getOverContent(i); final Barcode39 code39 = new Barcode39(); // code39.setCode(doc.getCodigo()); code39.setCode(sigla.replace("-", "").replace("/", "").replace(".", "")); code39.setStartStopText(false); final Image image39 = code39.createImageWithBarcode(over, null, null); Rectangle r = stamp.getReader().getPageSizeWithRotation(i); image39.setInitialRotation((float) Math.PI / 2.0f); image39.setAbsolutePosition( r.getWidth() - image39.getHeight() + (STAMP_BORDER_IN_CM - PAGE_BORDER_IN_CM) * CM_UNIT, BARCODE_HEIGHT_IN_CM * CM_UNIT); image39.setBackgroundColor(Color.green); image39.setBorderColor(Color.RED); image39.setBorderWidth(0.5f * CM_UNIT); image39.setImageMask(mask); over.setRGBColorFill(255, 255, 255); mask.setAbsolutePosition(r.getWidth() - image39.getHeight() - (PAGE_BORDER_IN_CM) * CM_UNIT, (BARCODE_HEIGHT_IN_CM - STAMP_BORDER_IN_CM) * CM_UNIT); mask.scaleAbsolute(image39.getHeight() + 2 * STAMP_BORDER_IN_CM * CM_UNIT, image39.getWidth() + 2 * STAMP_BORDER_IN_CM * CM_UNIT); over.addImage(mask); over.setRGBColorFill(0, 0, 0); over.addImage(image39); // over.addImage(mask, mask.getScaledWidth() * 8, 0, 0, // mask.getScaledHeight() * 8, 100, 450); if (qrCode != null) { java.awt.Image imgQRCode = createQRCodeImage(qrCode); Image imageQRCode = Image.getInstance(imgQRCode, Color.BLACK, true); imageQRCode.scaleAbsolute(QRCODE_SIZE_IN_CM * CM_UNIT, QRCODE_SIZE_IN_CM * CM_UNIT); imageQRCode.setAbsolutePosition(QRCODE_LEFT_MARGIN_IN_CM * CM_UNIT, PAGE_BORDER_IN_CM * CM_UNIT); over.setRGBColorFill(255, 255, 255); mask.setAbsolutePosition((QRCODE_LEFT_MARGIN_IN_CM - STAMP_BORDER_IN_CM) * CM_UNIT, (PAGE_BORDER_IN_CM - STAMP_BORDER_IN_CM) * CM_UNIT); mask.scaleAbsolute((QRCODE_SIZE_IN_CM + 2 * STAMP_BORDER_IN_CM) * CM_UNIT, (QRCODE_SIZE_IN_CM + 2 * STAMP_BORDER_IN_CM) * CM_UNIT); over.addImage(mask); over.setRGBColorFill(0, 0, 0); over.addImage(imageQRCode); } if (mensagem != null) { PdfPTable table = new PdfPTable(1); table.setTotalWidth(r.getWidth() - image39.getHeight() - (QRCODE_LEFT_MARGIN_IN_CM + QRCODE_SIZE_IN_CM + 4 * STAMP_BORDER_IN_CM + PAGE_BORDER_IN_CM) * CM_UNIT); PdfPCell cell = new PdfPCell(new Paragraph(mensagem, FontFactory.getFont(FontFactory.HELVETICA, 8, Font.NORMAL, Color.BLACK))); cell.setBorderWidth(0); table.addCell(cell); over.setRGBColorFill(255, 255, 255); mask.setAbsolutePosition( (QRCODE_LEFT_MARGIN_IN_CM + QRCODE_SIZE_IN_CM + STAMP_BORDER_IN_CM) * CM_UNIT, (PAGE_BORDER_IN_CM - STAMP_BORDER_IN_CM) * CM_UNIT); mask.scaleAbsolute(2 * STAMP_BORDER_IN_CM * CM_UNIT + table.getTotalWidth(), 2 * STAMP_BORDER_IN_CM * CM_UNIT + table.getTotalHeight()); over.addImage(mask); over.setRGBColorFill(0, 0, 0); table.writeSelectedRows(0, -1, (QRCODE_LEFT_MARGIN_IN_CM + QRCODE_SIZE_IN_CM + 2 * STAMP_BORDER_IN_CM) * CM_UNIT, table.getTotalHeight() + PAGE_BORDER_IN_CM * CM_UNIT, over); } if (cancelado) { over.saveState(); final PdfGState gs = new PdfGState(); gs.setFillOpacity(0.5f); over.setGState(gs); over.setColorFill(Color.GRAY); over.beginText(); over.setFontAndSize(helv, 72); over.showTextAligned(Element.ALIGN_CENTER, "CANCELADO", r.getWidth() / 2, r.getHeight() / 2, 45); over.endText(); over.restoreState(); } else if (rascunho) { over.saveState(); final PdfGState gs = new PdfGState(); gs.setFillOpacity(0.5f); over.setGState(gs); over.setColorFill(Color.GRAY); over.beginText(); over.setFontAndSize(helv, 72); over.showTextAligned(Element.ALIGN_CENTER, "MINUTA", r.getWidth() / 2, r.getHeight() / 2, 45); over.endText(); over.restoreState(); } else if (semEfeito) { over.saveState(); final PdfGState gs = new PdfGState(); gs.setFillOpacity(0.5f); over.setGState(gs); over.setColorFill(Color.GRAY); over.beginText(); over.setFontAndSize(helv, 72); over.showTextAligned(Element.ALIGN_CENTER, "SEM EFEITO", r.getWidth() / 2, r.getHeight() / 2, 45); over.endText(); over.restoreState(); } // if (!rascunho // && request.getRequestURL().indexOf("http://laguna/") == -1) { if (!rascunho && !cancelado && !semEfeito && ((!Contexto.resource("isVersionTest").equals("false")) || (!Contexto.resource("isBaseTest").equals("false")))) { over.saveState(); final PdfGState gs = new PdfGState(); gs.setFillOpacity(0.5f); over.setGState(gs); over.setColorFill(Color.GRAY); over.beginText(); over.setFontAndSize(helv, 72); over.showTextAligned(Element.ALIGN_CENTER, "INVLIDO", r.getWidth() / 2, r.getHeight() / 2, 45); over.endText(); over.restoreState(); } // Imprime um circulo com o numero da pagina dentro. if (paginaInicial != null) { String sFl = String.valueOf(paginaInicial + i - 1); // Se for a ultima pagina e o numero nao casar, acrescenta "-" e // pagina final if (n == i) { if (paginaFinal != paginaInicial + n - 1) { sFl = sFl + "-" + String.valueOf(paginaFinal); } } if (i > cOmitirNumeracao) { // Raio do circulo interno final float radius = 18f; // Distancia entre o circulo interno e o externo final float circleInterspace = Math.max(helv.getAscentPoint(instancia, TEXT_HEIGHT), helv.getAscentPoint(orgaoUsu, TEXT_HEIGHT)) - Math.min(helv.getDescentPoint(instancia, TEXT_HEIGHT), helv.getDescentPoint(orgaoUsu, TEXT_HEIGHT)) + 2 * TEXT_TO_CIRCLE_INTERSPACE; // Centro do circulo float xCenter = r.getWidth() - 1.8f * (radius + circleInterspace); float yCenter = r.getHeight() - 1.8f * (radius + circleInterspace); over.saveState(); final PdfGState gs = new PdfGState(); gs.setFillOpacity(1f); over.setGState(gs); over.setColorFill(Color.BLACK); over.saveState(); over.setColorStroke(Color.black); over.setLineWidth(1f); over.setColorFill(Color.WHITE); // Circulo externo over.circle(xCenter, yCenter, radius + circleInterspace); over.fill(); over.circle(xCenter, yCenter, radius + circleInterspace); over.stroke(); // Circulo interno over.circle(xCenter, yCenter, radius); over.stroke(); over.restoreState(); { over.saveState(); over.beginText(); over.setFontAndSize(helv, TEXT_HEIGHT); // Escreve o texto superior do carimbo float fDescent = helv.getDescentPoint(instancia, TEXT_HEIGHT); showTextOnArc(over, instancia, helv, TEXT_HEIGHT, xCenter, yCenter, radius - fDescent + TEXT_TO_CIRCLE_INTERSPACE, true); // Escreve o texto inferior float fAscent = helv.getAscentPoint(orgaoUsu, TEXT_HEIGHT); showTextOnArc(over, orgaoUsu, helv, TEXT_HEIGHT, xCenter, yCenter, radius + fAscent + TEXT_TO_CIRCLE_INTERSPACE, false); over.endText(); over.restoreState(); } over.beginText(); int textHeight = 23; // Diminui o tamanho do font ate que o texto caiba dentro do // circulo interno while (helv.getWidthPoint(sFl, textHeight) > (2 * (radius - TEXT_TO_CIRCLE_INTERSPACE))) textHeight--; float fAscent = helv.getAscentPoint(sFl, textHeight) + helv.getDescentPoint(sFl, textHeight); over.setFontAndSize(helv, textHeight); over.showTextAligned(Element.ALIGN_CENTER, sFl, xCenter, yCenter - 0.5f * fAscent, 0); over.endText(); over.restoreState(); } } } stamp.close(); return bo2.toByteArray(); }
From source file:it.eng.spagobi.engines.documentcomposition.exporterUtils.PdfCreator.java
License:Mozilla Public License
private float[] getTransformationMatrix(PdfImportedPage page, int xPos, int yPos, int tableWidth, int tableHeight) { float pageWidth = page.getWidth(); float pageHeight = page.getHeight(); float scaleX = tableWidth / pageWidth; float scaleY = tableHeight / pageHeight; float scale = Math.min(scaleX, scaleY); //float[] toReturn = {scale, 0f, 0f, scale, xPos, docHeight - tableHeight}; float dX = (float) xPos; float dY = yPos - tableHeight; float[] toReturn = { scale, 0f, 0f, scale, dX, dY }; return toReturn; }
From source file:lucee.runtime.tag.PDF.java
License:Open Source License
private void doActionAddWatermark() throws PageException, IOException, DocumentException { required("pdf", "addWatermark", "source", source); if (copyFrom == null && image == null) throw new ApplicationException( "at least one of the following attributes must be defined " + "[copyFrom,image]"); if (destination != null && destination.exists() && !overwrite) throw new ApplicationException("destination file [" + destination + "] already exists"); // image/* w w w . j a v a 2 s .c o m*/ Image img = null; if (image != null) { lucee.runtime.img.Image ri = lucee.runtime.img.Image.createImage(pageContext, image, false, false, true, null); img = Image.getInstance(ri.getBufferedImage(), null, false); } // copy From else { byte[] barr; try { Resource res = Caster.toResource(pageContext, copyFrom, true); barr = IOUtil.toBytes(res); } catch (ExpressionException ee) { barr = Caster.toBinary(copyFrom); } img = Image.getInstance(PDFUtil.toImage(barr, 1).getBufferedImage(), null, false); } // position float x = UNDEFINED, y = UNDEFINED; if (!StringUtil.isEmpty(position)) { int index = position.indexOf(','); if (index == -1) throw new ApplicationException("attribute [position] has an invalid value [" + position + "]," + "value should follow one of the following pattern [40,50], [40,] or [,50]"); String strX = position.substring(0, index).trim(); String strY = position.substring(index + 1).trim(); if (!StringUtil.isEmpty(strX)) x = Caster.toIntValue(strX); if (!StringUtil.isEmpty(strY)) y = Caster.toIntValue(strY); } PDFDocument doc = toPDFDocument(source, password, null); doc.setPages(pages); PdfReader reader = doc.getPdfReader(); reader.consolidateNamedDestinations(); boolean destIsSource = destination != null && doc.getResource() != null && destination.equals(doc.getResource()); java.util.List bookmarks = SimpleBookmark.getBookmark(reader); ArrayList master = new ArrayList(); if (bookmarks != null) master.addAll(bookmarks); // output OutputStream os = null; if (!StringUtil.isEmpty(name) || destIsSource) { os = new ByteArrayOutputStream(); } else if (destination != null) { os = destination.getOutputStream(); } try { int len = reader.getNumberOfPages(); PdfStamper stamp = new PdfStamper(reader, os); if (len > 0) { if (x == UNDEFINED || y == UNDEFINED) { PdfImportedPage first = stamp.getImportedPage(reader, 1); if (y == UNDEFINED) y = (first.getHeight() - img.getHeight()) / 2; if (x == UNDEFINED) x = (first.getWidth() - img.getWidth()) / 2; } img.setAbsolutePosition(x, y); //img.setAlignment(Image.ALIGN_JUSTIFIED); ration geht nicht anhand mitte } // rotation if (rotation != 0) { img.setRotationDegrees(rotation); } Set _pages = doc.getPages(); for (int i = 1; i <= len; i++) { if (_pages != null && !_pages.contains(Integer.valueOf(i))) continue; PdfContentByte cb = foreground ? stamp.getOverContent(i) : stamp.getUnderContent(i); PdfGState gs1 = new PdfGState(); //print.out("op:"+opacity); gs1.setFillOpacity(opacity); //gs1.setStrokeOpacity(opacity); cb.setGState(gs1); cb.addImage(img); } if (bookmarks != null) stamp.setOutlines(master); stamp.close(); } finally { IOUtil.closeEL(os); if (os instanceof ByteArrayOutputStream) { if (destination != null) IOUtil.copy(new ByteArrayInputStream(((ByteArrayOutputStream) os).toByteArray()), destination, true);// MUST overwrite if (!StringUtil.isEmpty(name)) { pageContext.setVariable(name, new PDFDocument(((ByteArrayOutputStream) os).toByteArray(), password)); } } } }
From source file:org.efaps.esjp.common.file.FileUtil_Base.java
License:Apache License
/** * Resize.// ww w.j av a 2s . com * * @param _parameter Parameter as passed by the eFaps API * @param _file the file * @param _fileName the file name * @param _pageSize the page size * @return the file * @throws EFapsException on error */ public File resizePdf(final Parameter _parameter, final File _file, final String _fileName, final String _pageSize) throws EFapsException { final Document document = new Document(); final File ret = getFile(_fileName, "pdf"); try { final File destFile = new File(_file.getPath() + ".tmp"); FileUtils.copyFile(_file, destFile); final OutputStream outputStream = new FileOutputStream(ret); final PdfReader pdfReader = new PdfReader(new FileInputStream(destFile)); // Create a writer for the outputstream final PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); PdfImportedPage page; final PdfContentByte cb = writer.getDirectContent(); int pageOfCurrentReaderPDF = 0; // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); document.setPageSize(page.getWidth() <= page.getHeight() ? PageSize.getRectangle(_pageSize) : PageSize.getRectangle(_pageSize).rotate()); final float widthFactor = document.getPageSize().getWidth() / page.getWidth(); final float heightFactor = document.getPageSize().getHeight() / page.getHeight(); final float factor = Math.min(widthFactor, heightFactor); final float offsetX = (document.getPageSize().getWidth() - page.getWidth() * factor) / 2; final float offsetY = (document.getPageSize().getHeight() - page.getHeight() * factor) / 2; cb.addTemplate(page, factor, 0, 0, factor, offsetX, offsetY); } pageOfCurrentReaderPDF = 0; outputStream.flush(); document.close(); outputStream.close(); } catch (final FileNotFoundException e) { LOG.error("FileNotFoundException", e); } catch (final IOException e) { LOG.error("IOException", e); } catch (final DocumentException e) { LOG.error("DocumentException", e); } return ret; }
From source file:org.lucee.extension.pdf.tag.PDF.java
License:Open Source License
private void doActionAddWatermark() throws PageException, IOException, DocumentException { required("pdf", "addWatermark", "source", source); if (copyFrom == null && image == null) throw engine.getExceptionUtil().createApplicationException( "at least one of the following attributes must be defined " + "[copyFrom,image]"); if (destination != null && destination.exists() && !overwrite) throw engine.getExceptionUtil() .createApplicationException("destination file [" + destination + "] already exists"); // image//from w w w . ja v a2 s . c o m Image img = null; if (image != null) { // TODO lucee.runtime.img.Image ri = lucee.runtime.img.Image.createImage(pageContext,image,false,false,true,null); // TODO img=Image.getInstance(ri.getBufferedImage(),null,false); } // copy From else { byte[] barr; try { Resource res = copyFrom instanceof String ? engine.getResourceUtil().toResourceExisting(pageContext, (String) copyFrom) : engine.getCastUtil().toResource(copyFrom); barr = PDFUtil.toBytes(res); } catch (PageException ee) { barr = engine.getCastUtil().toBinary(copyFrom); } img = Image.getInstance(PDFUtil.toImage(barr, 1), null, false); } // position float x = UNDEFINED, y = UNDEFINED; if (!Util.isEmpty(position)) { int index = position.indexOf(','); if (index == -1) throw engine.getExceptionUtil() .createApplicationException("attribute [position] has an invalid value [" + position + "]," + "value should follow one of the following pattern [40,50], [40,] or [,50]"); String strX = position.substring(0, index).trim(); String strY = position.substring(index + 1).trim(); if (!Util.isEmpty(strX)) x = engine.getCastUtil().toIntValue(strX); if (!Util.isEmpty(strY)) y = engine.getCastUtil().toIntValue(strY); } PDFStruct doc = toPDFDocument(source, password, null); doc.setPages(pages); PdfReader reader = doc.getPdfReader(); reader.consolidateNamedDestinations(); java.util.List bookmarks = SimpleBookmark.getBookmark(reader); ArrayList master = new ArrayList(); if (bookmarks != null) master.addAll(bookmarks); // output boolean destIsSource = destination != null && doc.getResource() != null && destination.equals(doc.getResource()); OutputStream os = null; if (!Util.isEmpty(name) || destIsSource) { os = new ByteArrayOutputStream(); } else if (destination != null) { os = destination.getOutputStream(); } try { int len = reader.getNumberOfPages(); PdfStamper stamp = new PdfStamper(reader, os); if (len > 0) { if (x == UNDEFINED || y == UNDEFINED) { PdfImportedPage first = stamp.getImportedPage(reader, 1); if (y == UNDEFINED) y = (first.getHeight() - img.getHeight()) / 2; if (x == UNDEFINED) x = (first.getWidth() - img.getWidth()) / 2; } img.setAbsolutePosition(x, y); // img.setAlignment(Image.ALIGN_JUSTIFIED); ration geht nicht anhand mitte } // rotation if (rotation != 0) { img.setRotationDegrees(rotation); } Set _pages = doc.getPages(); for (int i = 1; i <= len; i++) { if (_pages != null && !_pages.contains(Integer.valueOf(i))) continue; PdfContentByte cb = foreground ? stamp.getOverContent(i) : stamp.getUnderContent(i); PdfGState gs1 = new PdfGState(); // print.out("op:"+opacity); gs1.setFillOpacity(opacity); // gs1.setStrokeOpacity(opacity); cb.setGState(gs1); cb.addImage(img); } if (bookmarks != null) stamp.setOutlines(master); stamp.close(); } finally { Util.closeEL(os); if (os instanceof ByteArrayOutputStream) { if (destination != null) engine.getIOUtil().copy(new ByteArrayInputStream(((ByteArrayOutputStream) os).toByteArray()), destination, true);// MUST overwrite if (!Util.isEmpty(name)) { pageContext.setVariable(name, new PDFStruct(((ByteArrayOutputStream) os).toByteArray(), password)); } } } }
From source file:org.mapfish.print.PDFCustomBlocks.java
License:Open Source License
private void addBackground(PdfWriter writer, Document document, PdfContentByte dc) { if (backgroundPdf != null) { try {/*w ww . j a va2s . c o m*/ PdfImportedPage page = backgroundPdfs.get(backgroundPdf); if (page == null) { PdfReader reader = new PdfReader(backgroundPdf); page = writer.getImportedPage(reader, 1); backgroundPdfs.put(backgroundPdf, page); } final Rectangle pageSize = document.getPageSize(); final boolean rotate = (page.getWidth() < page.getHeight()) ^ (pageSize.getWidth() < pageSize.getHeight()); if (rotate) { dc.addTemplate(page, 0, -1, 1, 0, 0, pageSize.getHeight()); } else { dc.addTemplate(page, 0, 0); } } catch (IOException e) { addError(e); } } }
From source file:questions.importpages.NameCards.java
public static void createSheet(int p) throws DocumentException, IOException { Rectangle rect = new Rectangle(PageSize.A4); Document document = new Document(rect); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(SHEET[p - 1])); document.open();//from w w w .j a va2s . c om PdfContentByte canvas = writer.getDirectContentUnder(); PdfReader reader = new PdfReader(NameCard.CARD); PdfImportedPage front = writer.getImportedPage(reader, p); float x = rect.getWidth() / 2 - front.getWidth(); float y = (rect.getHeight() - (front.getHeight() * 5)) / 2; canvas.setLineWidth(0.5f); canvas.moveTo(x, y - 15); canvas.lineTo(x, y); canvas.lineTo(x - 15, y); canvas.moveTo(x + front.getWidth(), y - 15); canvas.lineTo(x + front.getWidth(), y); canvas.moveTo(x + front.getWidth() * 2, y - 15); canvas.lineTo(x + front.getWidth() * 2, y); canvas.lineTo(x + front.getWidth() * 2 + 15, y); canvas.stroke(); for (int i = 0; i < 5; i++) { for (int j = 0; j < 2; j++) { canvas.addTemplate(front, x, y); x += front.getWidth(); } x = rect.getWidth() / 2 - front.getWidth(); y += front.getHeight(); canvas.moveTo(x, y); canvas.lineTo(x - 15, y); canvas.moveTo(x + front.getWidth() * 2, y); canvas.lineTo(x + front.getWidth() * 2 + 15, y); canvas.stroke(); } canvas.moveTo(x, y + 15); canvas.lineTo(x, y); canvas.moveTo(x + front.getWidth(), y + 15); canvas.lineTo(x + front.getWidth(), y); canvas.moveTo(x + front.getWidth() * 2, y + 15); canvas.lineTo(x + front.getWidth() * 2, y); canvas.stroke(); document.close(); }