As in title mentioned, how to draw border with "RED" color , width-stroke 5 for all generated pdf pages using iText library. I've tried some codes but got no result.
(1)
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(99);
table.setLockedWidth(true);
PdfPCell cell = new PdfPCell();
cell.setFixedHeight(PageSize.A4.getHeight());
document.add(table);
(2)
PdfContentByte content = PdfWriter.getInstance(document, fout).getDirectContent();
Rectangle pageRect = document.getPageSize();
pageRect.setLeft(pageRect.getLeft() + 10);
pageRect.setRight(pageRect.getRight() - 10);
pageRect.setTop(pageRect.getTop() - 10);
pageRect.setBottom(pageRect.getBottom() +10);
content.setColorStroke( BaseColor.BLUE);
content.rectangle(pageRect.getLeft(), pageRect.getBottom(), pageRect.getWidth(), pageRect.getHeight());
content.setLineWidth(10);
content.stroke();
content.fillStroke();
Those methods give me no result, Thanks!
Edit
I've changed my methods also thanks to Bruno Lowagie for his respond. the example worked like a charm but i couldn't fit it into my code.
Here's my code: By pressing a button, PDF file will generate at specif address. I'll add more content later but now let's stick to generating pdf file(s).
SaveToSD = (Button)findViewById(R.id.SaveToMemoryCard_xml);
SaveToSD.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View sssdd)
{
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ConcreteProject";
File dir = new File(path);
if (!dir.exists())
dir.mkdirs();
Log.d("PDFCreator", "PDF Path: " + path);
// Incremental Process of Creating File(s).
String pdfName = "SDG_Created_pdf.pdf";
int num = 0;
File file = new File(dir, pdfName);
while (file.exists()) {
num++;
pdfName = "SDG_Created_pdf" + num + ".pdf";
file = new File(dir, pdfName);
}
try {
new ConAccept_Result().createPdf(pdfName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//End Of onClick(View sssdd).
});
And Here is method/class definitions:
public class RedBorder extends PdfPageEventHelper {
@Override
public void onEndPage(PdfWriter writer, Document document) {
PdfContentByte canvas = writer.getDirectContent();
Rectangle rect = document.getPageSize();
rect.setBorder(Rectangle.BOX); // left, right, top, bottom border
rect.setBorderWidth(5); // a width of 5 user units
rect.setBorderColor(BaseColor.RED); // a red border
rect.setUseVariableBorders(true); // the full width will be visible
canvas.rectangle(rect);
}
}
public void createPdf(String stringfile) throws IOException, DocumentException {
// step 1
com.itextpdf.text.Document document = new com.itextpdf.text.Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(stringfile));
RedBorder event = new RedBorder();
writer.setPageEvent(event);
// step 3
document.open();
// step 4
Chunk chunk = new Chunk("Lovin' iText - Lovin' iText");
chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 0.3f, BaseColor.CYAN);
document.add(chunk);
// step 5
document.close();
}
After running da app a folder as i named created but there is no PDF file !
Thanks a lot..
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…