Skip to content

Instantly share code, notes, and snippets.

@aembleton
Created May 8, 2013 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aembleton/5539861 to your computer and use it in GitHub Desktop.
Save aembleton/5539861 to your computer and use it in GitHub Desktop.
Example of iText that repeats an image all over a page and draws a semi-transparent yellow line on top.
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfShading;
import com.itextpdf.text.pdf.PdfShadingPattern;
import com.itextpdf.text.pdf.PdfSpotColor;
import com.itextpdf.text.pdf.PdfWriter;
public class Main {
/**
* @param args
* @throws DocumentException
* @throws IOException
* @throws MalformedURLException
*/
public static void main(String[] args) throws DocumentException, MalformedURLException, IOException {
// TODO Auto-generated method stub
// Create new PDF document
Document document = new Document(PageSize.A4);
// Create an output stream of PDF file.
OutputStream out = new FileOutputStream("PDF_Absolute_Position_Image_Example.pdf");
// Get a PdfWriter instance to write in PDF document.
PdfWriter writer = PdfWriter.getInstance(document, out);
// Open the PDF document
document.open();
for (int i=0; i<14; i++){
for (int j=0;j<19;j++){
Image image = Image.getInstance("logo.jpg");
// Set the position of image
image.setAbsolutePosition((i*42), j*44);
// Add paragraph to PDF document.
document.add(image);
}
}
PdfContentByte cb = writer.getDirectContent();
cb.setLineWidth(10.0f);
PdfGState gState = new PdfGState();
gState.setStrokeOpacity(0.5f);
cb.setColorStroke(BaseColor.YELLOW);
cb.setGState(gState);
//cb.setGrayStroke(0.5f);
cb.moveTo(100, 100);
cb.lineTo(500, 800);
cb.stroke();
// Close the PDf document after use.
document.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment