Reputation: 161
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!
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..
Upvotes: 0
Views: 8091
Reputation: 618
I did the same this way..
PdfCopy copy = new PdfCopy(document, bout);
document.open();
......
//read the PDF however you want to PDFReader testPdf
for (int page = 0; page < noOfPages; ) {
PdfImportedPage importedPage = copy.getImportedPage(testPdf, ++page);
PageStamp pageStamp = copy.createPageStamp(importedPage);
addHilitePageBlock(pageStamp);
copy.addPage(importedPage);
}
copy.freeReader(testPdf);
testPdf.close();
......
public void addPageBlock(PageStamp stamp) throws IOException {
PdfContentByte canvas = stamp.getOverContent();
drawRectangle(canvas, PageSize.A5.getWidth(), PageSize.A5.getHeight());
stamp.alterContents();
}
public void drawRectangle(PdfContentByte content, float width, float height) {
content.saveState();
PdfGState state = new PdfGState();
state.setFillOpacity(0.0f);
content.setGState(state);
content.setColorStroke(BaseColor.RED);
content.setLineWidth(6);
content.rectangle(17, 17, width + 155, height+ 145);
content.fillStroke();
content.restoreState();
}
Upvotes: 0
Reputation: 161
In onClick part of SaveToSD there are some changes. Change these:
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();
}
With this:
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/ConcreteProject";
File dir = new File(path);
if(!dir.exists())
dir.mkdirs();
//Incremental
String NameWithCounter = "/ConcreteProject/myPDFfile_1.pdf";
String DEST = Environment.getExternalStorageDirectory().getAbsolutePath()+NameWithCounter;
//int FileCounter = 1;
File real_file = new File(DEST);
while(real_file.exists()){
FileCounter++;
NameWithCounter = "/ConcreteProject/myPDFfile_"+FileCounter+".pdf";
DEST = Environment.getExternalStorageDirectory().getAbsolutePath()+NameWithCounter;
real_file = new File(DEST);
}
try {
createPdf(DEST);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
So when user press Save to Memory, PDF files will generated. For example if user pressed 4 times there will be 4 different PDF file with increment file name.
Upvotes: 0
Reputation: 77528
Reading your question, it seems obvious that you need a page event. Your attempts will add a border only once whereas you probably want to add a border to each page.
Please take a look at the PageBorder example. In this example, you'll find an implementation of the PageEvents
interface named RedBorder
:
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);
}
}
The onEndPage()
method is triggered automatically, every time a page ends (do not use theonStartPage()
method to add content).
In the implementation of this method, we ask the document
object for its current page size. Note that the document
instance passed to the event is of type PdfDocument
. It is not the same document as used in the createPdf()
method.
We adapt the rectangle to our needs. We set the border to BOX
meaning we want to add a border to the left, right, top and bottom. We define the width of the border (in this case 5 user units) and we define the color.
If you would stop there, a rectangle with a border of 5 user units would be drawn, but you would only see lines of 2.5 user units because the other half of the 5 user units would be outside the visible area of the page.
You can avoid this by using a width of 10 user units, or by setting the variable borders flag to true
.
Now all we have to do is to pass the rect
object to the rectangle()
method. This method is different from the method with the same name you have used in the sense that it also strokes the rectangle.
Upvotes: 4