Java Code Examples for com.lowagie.text.pdf.PdfContentByte#showTextAligned()

The following examples show how to use com.lowagie.text.pdf.PdfContentByte#showTextAligned() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PdfEventHandler.java    From unitime with Apache License 2.0 6 votes vote down vote up
/**
   * Print footer string on each page
   * @param writer
   * @param document
   */
  public void onEndPage(PdfWriter writer, Document document) {
   
  	if(getDateTime() == null) {
  		setDateTime(new Date());
  	}
  	
PdfContentByte cb = writer.getDirectContent();
cb.beginText();
cb.setFontAndSize(getBaseFont(), getFontSize());
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, getDateFormat().format(getDateTime()), 
	    document.left(), 20, 0);
cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, String.valueOf(document.getPageNumber()), 
	    document.right(), 20, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, MESSAGES.pdfCopyright(Constants.getVersion()),
		(document.left() + document.right()) / 2, 20, 0);
cb.endText();
	
      return;
  }
 
Example 2
Source File: PageNumbersWatermarkTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onStartPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
 */
public void onStartPage(PdfWriter writer, Document document) {
    if (writer.getPageNumber() < 3) {
        PdfContentByte cb = writer.getDirectContentUnder();
        cb.saveState();
        cb.setColorFill(Color.pink);
        cb.beginText();
        cb.setFontAndSize(helv, 48);
        cb.showTextAligned(Element.ALIGN_CENTER, "My Watermark Under " + writer.getPageNumber(), document.getPageSize().getWidth() / 2, document.getPageSize().getHeight() / 2, 45);
        cb.endText();
        cb.restoreState();
    }
}
 
Example 3
Source File: PdfWatermarkUtils.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
public static void addWatermark(PdfStamper pdfStamper, BaseFont baseFont, Color fontColor, String waterMarkString) throws Exception {
	if (null == pdfStamper || null == baseFont) {
		throw new java.lang.Exception("PdfStamper or BaseFont is null.");
	}
	if (StringUtils.isBlank(waterMarkString)) {
		return;
	}
	PdfContentByte pdfContentByte = null;
	Rectangle pageRect = null;
	PdfGState pdfGState = new PdfGState();
	// 设置透明度为0.4
	pdfGState.setFillOpacity(0.4f);
	pdfGState.setStrokeOpacity(0.4f);
       int pageNum = pdfStamper.getReader().getNumberOfPages();
       try {
           for (int i = 1; i <= pageNum; i++) {
           	pageRect = pdfStamper.getReader().getPageSizeWithRotation(i);
           	// 计算水印X,Y坐标
           	float x = pageRect.getWidth() / 2;
           	float y = pageRect.getHeight() / 2;
           	//获得PDF最顶层
           	pdfContentByte = pdfStamper.getOverContent(i);
           	pdfContentByte.saveState();
           	// set Transparency
           	pdfContentByte.setGState(pdfGState);
           	pdfContentByte.beginText();
           	pdfContentByte.setColorFill(fontColor);
           	pdfContentByte.setFontAndSize(baseFont, 60);
           	// 水印文字成45度角倾斜
           	pdfContentByte.showTextAligned(Element.ALIGN_CENTER, waterMarkString, x, y, 45);
           	pdfContentByte.endText();             	
           }
       } catch (Exception e) {
       	e.printStackTrace();
       } finally {
       	pdfContentByte = null;
           pageRect = null;        	
       }
}
 
Example 4
Source File: AddWatermarkPageNumbersTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
    * Reads the pages of an existing PDF file, adds pagenumbers and a watermark.

    */
@Test
   public  void main() throws Exception {
           // we create a reader for a certain document
           PdfReader reader = new PdfReader(PdfTestBase.RESOURCES_DIR +"ChapterSection.pdf");
           int n = reader.getNumberOfPages();
           // we create a stamper that will copy the document to a new file
           PdfStamper stamp = new PdfStamper(reader,PdfTestBase.getOutputStream("watermark_pagenumbers.pdf"));
           // adding some metadata
           HashMap<String, String> moreInfo = new HashMap<String, String>();
           moreInfo.put("Author", "Bruno Lowagie");
           stamp.setMoreInfo(moreInfo);
           // adding content to each page
           int i = 0;
           PdfContentByte under;
           PdfContentByte over;
           Image img = Image.getInstance(PdfTestBase.RESOURCES_DIR +"watermark.jpg");
           BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
           img.setAbsolutePosition(200, 400);
           while (i < n) {
           	i++;
           	// watermark under the existing page
           	under = stamp.getUnderContent(i);
           	under.addImage(img);
           	// text over the existing page
           	over = stamp.getOverContent(i);
           	over.beginText();
           	over.setFontAndSize(bf, 18);
           	over.setTextMatrix(30, 30);
           	over.showText("page " + i);
           	over.setFontAndSize(bf, 32);
           	over.showTextAligned(Element.ALIGN_LEFT, "DUPLICATE", 230, 430, 45);
           	over.endText();
           }
           // adding an extra page
           stamp.insertPage(1, PageSize.A4);
           over = stamp.getOverContent(1);
       	over.beginText();
       	over.setFontAndSize(bf, 18);
           over.showTextAligned(Element.ALIGN_LEFT, "DUPLICATE OF AN EXISTING PDF DOCUMENT", 30, 600, 0);
           over.endText();
           // adding a page from another document
           PdfReader reader2 = new PdfReader(PdfTestBase.RESOURCES_DIR +"SimpleAnnotations1.pdf");
           under = stamp.getUnderContent(1);
           under.addTemplate(stamp.getImportedPage(reader2, 3), 1, 0, 0, 1, 0, 0);
           // closing PdfStamper will generate the new PDF file
           stamp.close();
   }
 
Example 5
Source File: TwoOnOneTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Reads the pages of an existing PDF file and puts 2 pages from the
 * existing doc into one of the new doc.
 */
@Test
public void main() throws Exception {
	// we create a reader for a certain document
	PdfReader reader = new PdfReader(PdfTestBase.RESOURCES_DIR + "ChapterSection.pdf");
	// we retrieve the total number of pages
	int n = reader.getNumberOfPages();
	// we retrieve the size of the first page
	Rectangle psize = reader.getPageSize(1);
	float width = psize.getHeight();
	float height = psize.getWidth();

	// step 1: creation of a document-object
	Document document = new Document(new Rectangle(width, height));
	// step 2: we create a writer that listens to the document
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("2on1.pdf"));
	// step 3: we open the document
	document.open();
	// step 4: we add content
	PdfContentByte cb = writer.getDirectContent();
	int i = 0;
	int p = 0;
	while (i < n) {
		document.newPage();
		p++;
		i++;
		PdfImportedPage page1 = writer.getImportedPage(reader, i);
		cb.addTemplate(page1, .5f, 0, 0, .5f, 60, 120);
		if (i < n) {
			i++;
			PdfImportedPage page2 = writer.getImportedPage(reader, i);
			cb.addTemplate(page2, .5f, 0, 0, .5f, width / 2 + 60, 120);
		}
		BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
		cb.beginText();
		cb.setFontAndSize(bf, 14);
		cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p + " of " + ((n / 2) + (n % 2 > 0 ? 1 : 0)),
				width / 2, 40, 0);
		cb.endText();
	}
	// step 5: we close the document
	document.close();

}
 
Example 6
Source File: XandYcoordinatesTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
    * Creates a PDF document with shapes, lines and text at specific X and Y coordinates.
    */
@Test
public void main() throws Exception {
       
       // step 1: creation of a document-object
       Document document = new Document();
       
       try {      
           // step 2: creation of the writer
           PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "XandY.pdf"));
           
           // step 3: we open the document
           document.open();
           
           // step 4:
           PdfContentByte cb = writer.getDirectContent();
           
           // we create a PdfTemplate
           PdfTemplate template = cb.createTemplate(25, 25);
           
           // we add some crosses to visualize the coordinates
           template.moveTo(13, 0);
           template.lineTo(13, 25);
           template.moveTo(0, 13);
           template.lineTo(50, 13);
           template.stroke();
           template.sanityCheck();
           
           // we add the template on different positions
           cb.addTemplate(template, 216 - 13, 720 - 13);
           cb.addTemplate(template, 360 - 13, 360 - 13);
           cb.addTemplate(template, 360 - 13, 504 - 13);
           cb.addTemplate(template, 72 - 13, 144 - 13);
           cb.addTemplate(template, 144 - 13, 288 - 13);

           cb.moveTo(216, 720);
           cb.lineTo(360, 360);
           cb.lineTo(360, 504);
           cb.lineTo(72, 144);
           cb.lineTo(144, 288);
           cb.stroke();
           
           BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
           cb.beginText();
           cb.setFontAndSize(bf, 12);
           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(3\", 10\")", 216 + 25, 720 + 5, 0);
           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(5\", 5\")", 360 + 25, 360 + 5, 0);
           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(5\", 7\")", 360 + 25, 504 + 5, 0);
           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(1\", 2\")", 72 + 25, 144 + 5, 0);
           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(2\", 4\")", 144 + 25, 288 + 5, 0);
           cb.endText(); 
           
           cb.sanityCheck();
       }
       catch(DocumentException de) {
           System.err.println(de.getMessage());
       }
       catch(IOException ioe) {
           System.err.println(ioe.getMessage());
       }
       
       // step 5: we close the document
       document.close();
   }
 
Example 7
Source File: UpsideDownTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
    * Changes the default coordinate system so that the origin is in the upper left corner
    * instead of the lower left corner.
    */
@Test
public void main() throws Exception {
       
       // step 1: creation of a document-object
       Document document = new Document(PageSize.A4);
       
       try {
           // step 2: creation of the writer
           PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "upsidedown.pdf"));
           
           // step 3: we open the document
           document.open();
           
           // step 4:
           PdfContentByte cb = writer.getDirectContent();
           cb.concatCTM(1f, 0f, 0f, -1f, 0f, PageSize.A4.getHeight());
           
           // we create a PdfTemplate
           PdfTemplate template = cb.createTemplate(25, 25);
           
           // we add some crosses to visualize the coordinates
           template.moveTo(13, 0);
           template.lineTo(13, 25);
           template.moveTo(0, 13);
           template.lineTo(50, 13);
           template.stroke();
           template.sanityCheck();
           
           // we add the template on different positions
           cb.addTemplate(template, 216 - 13, 720 - 13);
           cb.addTemplate(template, 360 - 13, 360 - 13);
           cb.addTemplate(template, 360 - 13, 504 - 13);
           cb.addTemplate(template, 72 - 13, 144 - 13);
           cb.addTemplate(template, 144 - 13, 288 - 13);

           cb.moveTo(216, 720);
           cb.lineTo(360, 360);
           cb.lineTo(360, 504);
           cb.lineTo(72, 144);
           cb.lineTo(144, 288);
           cb.stroke();
           
           BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
           cb.beginText();
           cb.setFontAndSize(bf, 12);
           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(3\", 10\")", 216 + 25, 720 + 5, 0);
           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(5\", 5\")", 360 + 25, 360 + 5, 0);
           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(5\", 7\")", 360 + 25, 504 + 5, 0);
           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(1\", 2\")", 72 + 25, 144 + 5, 0);
           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(2\", 4\")", 144 + 25, 288 + 5, 0);
           cb.endText();
           
           cb.sanityCheck();
       }
       catch(DocumentException de) {
           System.err.println(de.getMessage());
       }
       catch(IOException ioe) {
           System.err.println(ioe.getMessage());
       }
       
       // step 5: we close the document
       document.close();
   }
 
Example 8
Source File: AffineTransformationTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Changes the transformation matrix with AffineTransform.
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document(PageSize.A4);

	// step 2: creation of the writer
	PdfWriter writer = PdfWriter.getInstance(document,
			PdfTestBase.getOutputStream("affinetransformation.pdf"));

	// step 3: we open the document
	document.open();

	// step 4:
	PdfContentByte cb = writer.getDirectContent();
	cb.transform(AffineTransform.getScaleInstance(1.2, 0.75));

	// we create a PdfTemplate
	PdfTemplate template = cb.createTemplate(25, 25);

	// we add some crosses to visualize the coordinates
	template.moveTo(13, 0);
	template.lineTo(13, 25);
	template.moveTo(0, 13);
	template.lineTo(50, 13);
	template.stroke();
	template.sanityCheck();

	// we add the template on different positions
	cb.addTemplate(template, 216 - 13, 720 - 13);
	cb.addTemplate(template, 360 - 13, 360 - 13);
	cb.addTemplate(template, 360 - 13, 504 - 13);
	cb.addTemplate(template, 72 - 13, 144 - 13);
	cb.addTemplate(template, 144 - 13, 288 - 13);

	cb.moveTo(216, 720);
	cb.lineTo(360, 360);
	cb.lineTo(360, 504);
	cb.lineTo(72, 144);
	cb.lineTo(144, 288);
	cb.stroke();

	BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,
			BaseFont.NOT_EMBEDDED);
	cb.beginText();
	cb.setFontAndSize(bf, 12);
	cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
			"(3\" * 1.2, 10\" * .75)", 216 + 25, 720 + 5, 0);
	cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
			"(5\" * 1.2, 5\" * .75)", 360 + 25, 360 + 5, 0);
	cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
			"(5\" * 1.2, 7\" * .75)", 360 + 25, 504 + 5, 0);
	cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
			"(1\" * 1.2, 2\" * .75)", 72 + 25, 144 + 5, 0);
	cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
			"(2\" * 1.2, 4\" * .75)", 144 + 25, 288 + 5, 0);
	cb.endText();

	cb.sanityCheck();

	// step 5: we close the document
	document.close();
}
 
Example 9
Source File: TextTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Adding text at absolute positions.
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document();

	// step 2: creation of the writer
	PdfWriter writer = PdfWriter.getInstance(document,
			PdfTestBase.getOutputStream("text.pdf"));

	// step 3: we open the document
	document.open();

	// step 4: we grab the ContentByte and do some stuff with it
	PdfContentByte cb = writer.getDirectContent();

	// first we draw some lines to be able to visualize the text alignment
	// functions
	cb.setLineWidth(0f);
	cb.moveTo(250, 500);
	cb.lineTo(250, 800);
	cb.moveTo(50, 700);
	cb.lineTo(400, 700);
	cb.moveTo(50, 650);
	cb.lineTo(400, 650);
	cb.moveTo(50, 600);
	cb.lineTo(400, 600);
	cb.stroke();

	// we tell the ContentByte we're ready to draw text
	cb.beginText();

	BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,
			BaseFont.NOT_EMBEDDED);
	cb.setFontAndSize(bf, 12);
	String text = "Sample text for alignment";
	// we show some text starting on some absolute position with a given
	// alignment
	cb.showTextAligned(PdfContentByte.ALIGN_CENTER, text + " Center", 250,
			700, 0);
	cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, text + " Right", 250,
			650, 0);
	cb.showTextAligned(PdfContentByte.ALIGN_LEFT, text + " Left", 250, 600,
			0);

	// we draw some text on a certain position
	cb.setTextMatrix(100, 400);
	cb.showText("Text at position 100,400.");

	// we draw some rotated text on a certain position
	cb.setTextMatrix(0, 1, -1, 0, 100, 300);
	cb.showText("Text at position 100,300, rotated 90 degrees.");

	// we draw some mirrored, rotated text on a certain position
	cb.setTextMatrix(0, 1, 1, 0, 200, 200);
	cb.showText("Text at position 200,200, mirrored and rotated 90 degrees.");

	// we tell the contentByte, we've finished drawing text
	cb.endText();

	cb.sanityCheck();

	// step 5: we close the document
	document.close();
}
 
Example 10
Source File: PageNumbersWatermarkTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
 */
public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();
    // write the headertable
    table.setTotalWidth(document.right() - document.left());
    table.writeSelectedRows(0, -1, document.left(), document.getPageSize().getHeight() - 50, cb);
    // compose the footer
    String text = "Page " + writer.getPageNumber() + " of ";
    float textSize = helv.getWidthPoint(text, 12);
    float textBase = document.bottom() - 20;
    cb.beginText();
    cb.setFontAndSize(helv, 12);
    // for odd pagenumbers, show the footer at the left
    if ((writer.getPageNumber() & 1) == 1) {
        cb.setTextMatrix(document.left(), textBase);
        cb.showText(text);
        cb.endText();
        cb.addTemplate(tpl, document.left() + textSize, textBase);
    }
    // for even numbers, show the footer at the right
    else {
        float adjust = helv.getWidthPoint("0", 12);
        cb.setTextMatrix(document.right() - textSize - adjust, textBase);
        cb.showText(text);
        cb.endText();
        cb.addTemplate(tpl, document.right() - adjust, textBase);
    }

    // draw a Rectangle around the page
    cb.setColorStroke(Color.orange);
    cb.setLineWidth(2);
    cb.rectangle(20, 20, document.getPageSize().getWidth() - 40, document.getPageSize().getHeight() - 40);
    cb.stroke();

    // starting on page 3, a watermark with an Image that is made transparent
    if (writer.getPageNumber() >= 3) {

        cb.setGState(gstate);
        cb.setColorFill(Color.red);
        cb.beginText();
        cb.setFontAndSize(helv, 48);
        cb.showTextAligned(Element.ALIGN_CENTER, "Watermark Opacity " + writer.getPageNumber(), document.getPageSize().getWidth() / 2, document.getPageSize().getHeight() / 2, 45);
        cb.endText();
        try {
            cb.addImage(headerImage, headerImage.getWidth(), 0, 0, headerImage.getHeight(), 440, 80);
        }
        catch(Exception e) {
            throw new ExceptionConverter(e);
        }
    }
    cb.restoreState();
    cb.sanityCheck();
}
 
Example 11
Source File: DvdCoverTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Method that generates the actual PDF file.
 */
public void generatePdf() throws Exception {

	// step 1: creation of a document-object
	Rectangle pageSize = new Rectangle(780, 525);
	if (backgroundcolor != null) {
		pageSize.setBackgroundColor(backgroundcolor);
	}
	Document document = new Document(pageSize);

	// step 2:
	// we create a writer that listens to the document
	// and directs a PDF-stream to a file
	if (filename == null) {
		filename = PdfTestBase.OUTPUT_DIR + "dvdcover.pdf";
	}
	PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));

	// step 3: we open the document
	document.open();

	// step 4:
	PdfContentByte cb = writer.getDirectContent();
	if (title != null) {
		cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false), 24);
		cb.beginText();
		if (front == null) {
			cb.showTextAligned(Element.ALIGN_CENTER, title, 595f, 262f, 0f);
		}
		if (side == null) {
			cb.showTextAligned(Element.ALIGN_CENTER, title, 385f, 262f, 270f);
		}
		cb.endText();
	}
	cb.moveTo(370, 0);
	cb.lineTo(370, 525);
	cb.moveTo(410, 525);
	cb.lineTo(410, 0);
	cb.stroke();
	if (front != null) {
		front.scaleToFit(370, 525);
		front.setAbsolutePosition(410f + (370f - front.getScaledWidth()) / 2f,
				(525f - front.getScaledHeight()) / 2f);
		document.add(front);
	}
	if (back != null) {
		back.scaleToFit(370, 525);
		back.setAbsolutePosition((370f - back.getScaledWidth()) / 2f, (525f - back.getScaledHeight()) / 2f);
		document.add(back);
	}
	if (side != null) {
		side.scaleToFit(40, 525);
		side.setAbsolutePosition(370 + (40f - side.getScaledWidth()) / 2f, (525f - side.getScaledHeight()) / 2f);
		document.add(side);
	}

	// step 5: we close the document
	document.close();
}