List of usage examples for com.itextpdf.text.pdf PdfStamper getOverContent
public PdfContentByte getOverContent(final int pageNum)
PdfContentByte
to write over the page of the original document. From source file:org.h819.commons.file.MyPDFUtils.java
/** * ??/* w w w .j a v a 2 s . c o m*/ * * @param srcPdf ? * @param destPdf * @param waterMarkText ? * @param waterMarkImage ? */ public static void addWaterMarkFile(File srcPdf, File destPdf, String waterMarkText, File waterMarkImage) throws IOException, DocumentException { if (waterMarkText == null && waterMarkImage == null) throw new FileNotFoundException(waterMarkText + " " + waterMarkImage + " all null."); if (srcPdf == null || !srcPdf.exists() || !srcPdf.isFile()) throw new FileNotFoundException("pdf file : '" + srcPdf + "' does not exsit."); if (!FilenameUtils.getExtension(srcPdf.getAbsolutePath()).toLowerCase().equals("pdf")) return; if (waterMarkImage != null) { if (!waterMarkImage.exists() || !waterMarkImage.isFile()) throw new FileNotFoundException("img file : '" + srcPdf + "' does not exsit."); if (!FilenameUtils.getExtension(waterMarkImage.getAbsolutePath()).toLowerCase().equals("png")) throw new FileNotFoundException("image file '" + srcPdf + "' not png.(???? pdf )"); } PdfReader reader = getPdfReader(srcPdf); int n = reader.getNumberOfPages(); PdfStamper stamper = getPdfStamper(srcPdf, destPdf); // // HashMap<String, String> moreInfo = new HashMap<String, String>(); // moreInfo.put("Author", "H819 create"); // moreInfo.put("Producer", "H819 Producer"); // Key = CreationDate, Value = D:20070425182920 // Key = Producer, Value = TH-OCR 2000 (C++/Win32) // Key = Author, Value = TH-OCR 2000 // Key = Creator, Value = TH-OCR PDF Writer // stamp.setMoreInfo(moreInfo); // text Phrase text = null; if (waterMarkText != null) { // Font bfont = getPdfFont(); bfont.setSize(35); bfont.setColor(new BaseColor(192, 192, 192)); text = new Phrase(waterMarkText, bfont); } // image watermark Image img = null; float w = 0; float h = 0; if (waterMarkImage != null) { img = Image.getInstance(waterMarkImage.getAbsolutePath()); w = img.getScaledWidth(); h = img.getScaledHeight(); // img. img.setRotationDegrees(45); } // transparency PdfGState gs1 = new PdfGState(); gs1.setFillOpacity(0.5f); // properties PdfContentByte over; Rectangle pageSize; float x, y; // loop over every page for (int i = 1; i <= n; i++) { pageSize = reader.getPageSizeWithRotation(i); x = (pageSize.getLeft() + pageSize.getRight()) / 2; y = (pageSize.getTop() + pageSize.getBottom()) / 2; // pdf pdf ??? over = stamper.getOverContent(i); // ? // over = stamp.getUnderContent(i); // ?? over.beginText(); over.endText(); ? // ,?,:???? over.saveState(); //?? over.setGState(gs1); if (waterMarkText != null && waterMarkImage != null) { // if (i % 2 == 1) { ColumnText.showTextAligned(over, Element.ALIGN_CENTER, text, x, y, 45); } else over.addImage(img, w, 0, 0, h, x - (w / 2), y - (h / 2)); } else if (waterMarkText != null) { //? ColumnText.showTextAligned(over, Element.ALIGN_CENTER, text, x, y, 45); //?? ,?, :????? // ... } else { //? over.addImage(img, w, 0, 0, h, x - (w / 2), y - (h / 2)); } over.restoreState();//??? } stamper.close(); reader.close(); }
From source file:org.jfree.chart.swt.ChartPdf.java
License:Open Source License
public static void saveChartAsPDF(File file, JFreeChart chart, int width, int height) throws DocumentException, FileNotFoundException, IOException { if (chart != null) { boolean success = false; String old = null;/*from w w w .ja va 2s .c om*/ File oldFile = null; boolean append = file.exists(); if (append) { old = file.getAbsolutePath() + ".old"; //$NON-NLS-1$ oldFile = new File(old); oldFile.delete(); file.renameTo(oldFile); } try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file))) { // convert chart to PDF with iText: Rectangle pagesize = new Rectangle(width, height); if (append) { PdfReader reader = new PdfReader(old); PdfStamper stamper = new PdfStamper(reader, out); try { int n = reader.getNumberOfPages() + 1; stamper.insertPage(n, pagesize); PdfContentByte overContent = stamper.getOverContent(n); writeChart(chart, width, height, overContent); ColumnText ct = new ColumnText(overContent); ct.setSimpleColumn(width - 50, 50, width - 12, height, 150, Element.ALIGN_RIGHT); Paragraph paragraph = new Paragraph(String.valueOf(n), new Font(FontFamily.HELVETICA, 9, Font.NORMAL, BaseColor.DARK_GRAY)); paragraph.setAlignment(Element.ALIGN_RIGHT); ct.addElement(paragraph); ct.go(); success = true; } finally { stamper.close(); reader.close(); oldFile.delete(); } } else { Document document = new Document(pagesize, 50, 50, 50, 50); document.addCreationDate(); document.addCreator(Constants.APPLICATION_NAME); document.addAuthor(System.getProperty("user.name")); //$NON-NLS-1$ try { PdfWriter writer = PdfWriter.getInstance(document, out); document.open(); writeChart(chart, width, height, writer.getDirectContent()); success = true; } finally { document.close(); } } } if (!success) { file.delete(); if (oldFile != null) oldFile.renameTo(file); } } }
From source file:pdf.alterLetter.java
public static void main(String args[]) { try {//from w w w. ja v a 2s . c o m PdfReader pdfReader; pdfReader = new PdfReader("C:\\Users\\asus\\Desktop\\web\\Appointment letter.pdf"); //pdfReader = new PdfReader("C:\\Users\\asus\\Desktop\\TFMsystem\\Appointment letter.pdf"); //Create PdfStamper instance. PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream( "C:\\Users\\asus\\Desktop\\TFMsystem\\web\\Modified appointment letter.pdf")); //new FileOutputStream("C:\\Users\\asus\\Desktop\\TFMsystem\\Modified appointment letter.pdf")); //Create BaseFont instance. BaseFont baseFont = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1257, BaseFont.NOT_EMBEDDED); //Get the number of pages in pdf. int pages = pdfReader.getNumberOfPages(); //Iterate the pdf through pages. for (int i = 1; i <= pages; i++) { //Contain the pdf data. PdfContentByte pageContentByte = pdfStamper.getOverContent(i); pageContentByte.beginText(); //Set text font and size. pageContentByte.setFontAndSize(baseFont, 12); //Write text pageContentByte.setTextMatrix(120, 706); pageContentByte.showText("[no rujukan(enter by admin/opai)]"); pageContentByte.setTextMatrix(500, 706); pageContentByte.showText("[current date]"); //address pageContentByte.setTextMatrix(46, 641); pageContentByte.showText("[name]"); pageContentByte.setTextMatrix(46, 629); pageContentByte.showText("[position]"); pageContentByte.setTextMatrix(46, 617); pageContentByte.showText("[department]"); pageContentByte.setTextMatrix(155, 493); pageContentByte.showText("[status(penyelaras/ahli),taskforce name]"); pageContentByte.setTextMatrix(178, 433); pageContentByte.showText("[start date]"); pageContentByte.setTextMatrix(290, 433); pageContentByte.showText("[end date] ."); pageContentByte.setTextMatrix(46, 248); pageContentByte.showText("[name]"); pageContentByte.setTextMatrix(46, 236); pageContentByte.showText("[post]"); pageContentByte.setTextMatrix(46, 224); pageContentByte.showText("[faculty]"); pageContentByte.setTextMatrix(46, 212); pageContentByte.showText("[email]"); pageContentByte.endText(); } //Close the pdfStamper. pdfStamper.close(); System.out.println("PDF modified successfully."); } catch (Exception e) { e.printStackTrace(); } }
From source file:pdf.letter.java
public boolean AlterLetter(String rujukan, String name, String position, String department, String gStatus, String sDate, String eDate, String taskName, String postHolderName, String postHolderEmail, String postName) {/*from w w w . ja v a 2 s . c o m*/ DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); Date today = Calendar.getInstance().getTime(); String currDate = df.format(today); try { PdfReader pdfReader; pdfReader = new PdfReader("C:\\Users\\on\\Desktop\\AD\\TFMsystem\\web\\Appointment letter.pdf"); //C:\\Users\\on\\Desktop\\AD\\TFMsystem\\web\\Appointment letter.pdf //Create PdfStamper instance. PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream( "C:\\Users\\on\\Desktop\\AD\\TFMsystem\\web\\Modified appointment letter.pdf")); //C:\\Users\\on\\Desktop\\AD\\TFMsystem\\web\\Modified Appointment letter.pdf //Create BaseFont instance. BaseFont baseFont = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1257, BaseFont.NOT_EMBEDDED); //Get the number of pages in pdf. int pages = pdfReader.getNumberOfPages(); //Iterate the pdf through pages. for (int i = 1; i <= pages; i++) { //Contain the pdf data. PdfContentByte pageContentByte = pdfStamper.getOverContent(i); pageContentByte.beginText(); //Set text font and size. pageContentByte.setFontAndSize(baseFont, 11); //Write text pageContentByte.setTextMatrix(120, 706); pageContentByte.showText(rujukan); pageContentByte.setTextMatrix(500, 706); pageContentByte.showText(currDate); //address pageContentByte.setTextMatrix(46, 641); pageContentByte.showText(name); pageContentByte.setTextMatrix(46, 629); pageContentByte.showText(position); pageContentByte.setTextMatrix(46, 617); pageContentByte.showText(department); String gstatus; pageContentByte.setTextMatrix(157, 493); String changeCase = gStatus + ", " + taskName; pageContentByte.showText(changeCase.toUpperCase()); pageContentByte.setTextMatrix(250, 444); pageContentByte.showText(gStatus + " " + taskName + " ."); pageContentByte.setTextMatrix(180, 432); pageContentByte.showText(sDate); pageContentByte.setTextMatrix(290, 432); pageContentByte.showText(eDate + " ."); pageContentByte.setTextMatrix(46, 248); pageContentByte.showText(postHolderName); pageContentByte.setTextMatrix(46, 236); pageContentByte.showText(postName); pageContentByte.setTextMatrix(46, 224); pageContentByte.showText("Fakulti Komputeran"); pageContentByte.setTextMatrix(46, 212); pageContentByte.showText(postHolderEmail); pageContentByte.endText(); } //Close the pdfStamper. pdfStamper.close(); System.out.println("PDF modified successfully."); return true; } catch (Exception e) { e.printStackTrace(); return false; } }
From source file:printom.PDFCreator.java
public static void createLabel(int aLabelType, int aJobNum, char aJobIdentifier, String aCustName, String aItemName, String aItemCode, String aDate, String aPOrderNum, int aInputPcs) { String myJobNum = String.valueOf(aJobNum); char myJobIdentifier = aJobIdentifier; String myCustName = aCustName; String myItemName = aItemName; String myItemCode = aItemCode; String myDate = aDate;//from ww w .ja v a2 s . c o m String myPOrderNum = aPOrderNum; String myInputPcs = String.valueOf(aInputPcs); try { String src = ""; if (aLabelType == 1) { src = CTNLABEL; } String dest = RESULTLABEL; Font timesJob = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD, BaseColor.WHITE); Font timesDef = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.NORMAL, BaseColor.BLACK); PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); PdfContentByte canvas = stamper.getOverContent(1); for (int k = 0; k < 3; k++) { //Positions int[] x = { 298, 350, 125, 80, 80, 80, 80, 120 }; int[] y = { 562, 562, 518, 498, 479, 459, 440, 420 }; if (k == 1) { for (int j = 0; j < 8; j++) { y[j] = y[j] - 186; } } if (k == 2) { for (int j = 0; j < 8; j++) { y[j] = y[j] - 372; } } for (int i = 0; i < 2; i++) { if (i == 1) { for (int j = 0; j < 8; j++) { x[j] = x[j] + 372; } } ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myJobNum, timesJob), x[0], y[0], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(String.valueOf(myJobIdentifier), timesDef), x[1], y[1], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myCustName, timesDef), x[2], y[2], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myItemName, timesDef), x[3], y[3], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myItemCode, timesDef), x[4], y[4], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myDate, timesDef), x[5], y[5], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myPOrderNum, timesDef), x[6], y[6], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myInputPcs, timesDef), x[7], y[7], 0); } } stamper.close(); reader.close(); } catch (IOException | DocumentException ex) { Logger.getLogger(PDFCreator.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:printom.PDFCreator.java
public static void createReport(int aJobNum, char aJobIdentifier, String aCustName, String aItemName, String aItemCode, String aDate, String aPOrderNum, int aInputPcs, int aQtyWithOvers, int aNumContainers, String aInputContainer) { String myJobNum = String.valueOf(aJobNum); char myJobIdentifier = aJobIdentifier; String myCustName = aCustName; String myItemName = aItemName; String myItemCode = aItemCode; String myDate = aDate;//from w w w .ja v a2 s. c o m String myPOrderNum = aPOrderNum; String myInputPcs = String.valueOf(aInputPcs); int myQtyWithOvers = aQtyWithOvers; int myNumContainers = aNumContainers; String myInputContainer = aInputContainer; double amount = myQtyWithOvers; DecimalFormat formatter = new DecimalFormat("#,###"); String myStrQtyWithOvers = formatter.format(amount); try { String src = REPORT; String dest = RESULTREPORT; Font timesJob = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD, BaseColor.WHITE); Font timesDef = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.NORMAL, BaseColor.BLACK); PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); PdfContentByte canvas = stamper.getOverContent(1); int[] x = { 441, 510, 426, 87, 87, 87, 426, 307, 218, 325, 426 }; int[] y = { 547, 547, 473, 450, 428, 473, 450, 325, 385, 385, 428 }; ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myJobNum, timesJob), x[0], y[0], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(String.valueOf(myJobIdentifier), timesDef), x[1], y[1], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myCustName, timesDef), x[2], y[2], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myItemName, timesDef), x[3], y[3], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myItemCode, timesDef), x[4], y[4], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myDate, timesDef), x[5], y[5], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myPOrderNum, timesDef), x[6], y[6], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myInputPcs, timesDef), x[7], y[7], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myStrQtyWithOvers, timesDef), x[8], y[8], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("(" + String.valueOf(myNumContainers) + " cases with overs)", timesDef), x[9], y[9], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myInputContainer, timesDef), x[10], y[10], 0); stamper.close(); reader.close(); } catch (IOException | DocumentException ex) { Logger.getLogger(PDFCreator.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:se.inera.intyg.intygstyper.fk7263.pdf.PdfAbstractGenerator.java
License:Open Source License
protected void createSignatureNotRequiredField(PdfStamper pdfStamper, int lastPage) throws DocumentException, IOException { PdfContentByte addOverlay;//from w ww .j av a 2 s .c om addOverlay = pdfStamper.getOverContent(lastPage); addOverlay.saveState(); addOverlay.setColorFill(SIGNATURE_NOT_REQUIRED_COLOR); addOverlay.setColorStroke(CMYKColor.BLACK); addOverlay.rectangle(SIGNATURE_NOT_REQUIRED_START_X, SIGNATURE_NOT_REQUIRED_START_Y, SIGNATURE_NOT_REQUIRED_WIDTH, SIGNATURE_NOT_REQUIRED_HEIGHT); addOverlay.setLineWidth(LINE_WIDTH); addOverlay.fillStroke(); addOverlay.restoreState(); // Do text addOverlay = pdfStamper.getOverContent(lastPage); addOverlay.saveState(); BaseFont bf = BaseFont.createFont(); addOverlay.beginText(); addOverlay.setFontAndSize(bf, SIGNATURE_NOT_REQUIRED_FONT_SIZE); addOverlay.setTextMatrix(SIGNATURE_NOT_REQUIRED_START_X + SIGNATURE_NOT_REQUIRED_PADDING_LEFT, SIGNATURE_NOT_REQUIRED_START_Y + SIGNATURE_NOT_REQUIRED_PADDING_BOTTOM); addOverlay.showText(SIGNATURE_NOT_REQUIRED_TEXT); addOverlay.endText(); addOverlay.restoreState(); }
From source file:se.inera.intyg.intygstyper.fk7263.pdf.PdfAbstractGenerator.java
License:Open Source License
protected void maskSendToFkInformation(PdfStamper pdfStamper) { PdfContentByte addOverlay;/*from w w w.ja v a 2 s .co m*/ addOverlay = pdfStamper.getOverContent(1); addOverlay.saveState(); addOverlay.setColorFill(CMYKColor.WHITE); addOverlay.setColorStroke(CMYKColor.WHITE); addOverlay.rectangle(MASK_START_X, MASK_START_Y, MASK_WIDTH, MASK_HEIGTH); addOverlay.fillStroke(); addOverlay.restoreState(); }
From source file:se.inera.intyg.intygstyper.fk7263.pdf.PdfAbstractGenerator.java
License:Open Source License
protected void mark(PdfStamper pdfStamper, String watermarkText, int startX, int startY, int height, int width) throws DocumentException, IOException { PdfContentByte addOverlay;// w w w .j av a 2 s. co m addOverlay = pdfStamper.getOverContent(1); addOverlay.saveState(); addOverlay.setColorFill(CMYKColor.WHITE); addOverlay.setColorStroke(CMYKColor.RED); addOverlay.rectangle(startX, startY, width, height); addOverlay.stroke(); addOverlay.restoreState(); // Do text addOverlay = pdfStamper.getOverContent(1); ColumnText ct = new ColumnText(addOverlay); BaseFont bf = BaseFont.createFont(); Font font = new Font(bf, WATERMARK_FONTSIZE); int llx = startX + WATERMARK_TEXT_PADDING; int lly = startY + WATERMARK_TEXT_PADDING; int urx = llx + width - 2 * WATERMARK_TEXT_PADDING; int ury = lly + height - 2 * WATERMARK_TEXT_PADDING; Phrase phrase = new Phrase(watermarkText, font); ct.setSimpleColumn(phrase, llx, lly, urx, ury, WATERMARK_FONTSIZE, Element.ALIGN_LEFT | Element.ALIGN_TOP); ct.go(); }
From source file:se.inera.intyg.intygstyper.fk7263.pdf.PdfAbstractGenerator.java
License:Open Source License
protected void createRightMarginText(PdfStamper pdfStamper, int numberOfPages, String id, String text) throws DocumentException, IOException { PdfContentByte addOverlay;/*w w w .ja v a 2 s. c o m*/ BaseFont bf = BaseFont.createFont(); // Do text for (int i = 1; i <= numberOfPages; i++) { addOverlay = pdfStamper.getOverContent(i); addOverlay.saveState(); addOverlay.beginText(); addOverlay.setFontAndSize(bf, MARGIN_TEXT_FONTSIZE); addOverlay.setTextMatrix(0, 1, -1, 0, MARGIN_TEXT_START_X, MARGIN_TEXT_START_Y); addOverlay.showText(String.format("Intygs-ID: %s. %s", id, text)); addOverlay.endText(); addOverlay.restoreState(); } }