Reputation: 65
I am working with iText on android and this is basically my scenario:
Run an HTTP call to GET XML data Parse the XML to get a string within it Use the string to create the PDF
The string I am pulling from the XML is encoded pdf data physically coming from another PDF on the web.
I have gotten through the decode process so that its a decoded string... now I just need to get it to write into some form of API in order to allow it to save properly.
I am currently working with iText and when I try to add a Paragraph - it does exactly what I expected and writes the string (which includes tags like its title and author etc)
Is there a way to simply write this string to a PDF document and save it properly?
(I deleted a question from earlier about this to rephrase the issue as best I could).
Upvotes: 1
Views: 2251
Reputation: 1
try{
File obj=new File(Environment.getExternalStorageDirectory(),"pdf");
if(!obj.exists())
{
obj.mkdirs();
}
String name=obj.getAbsolutePath()+"/mypdf.pdf";
FileOutputStream fop=new FileOutputStream(name);
Document doc=new Document();
PdfWriter.getInstance(doc, fop);
doc.open();
Font font=new Font(FontFamily.TIMES_ROMAN, 20, Font.STRIKETHRU, BaseColor.GREEN);
Paragraph p=new Paragraph("sample pdf",font);
p.setAlignment(Element.ALIGN_LEFT);
doc.add(p);
doc.close();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Upvotes: 0
Reputation: 107
public void createPDF()
{
Document doc = new Document();
try {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "//pdf";
File dir = new File(path);
if(!dir.exists())
dir.mkdirs();
Log.d("PDFCreator", "PDF Path: " + path);
File file = new File(dir, "sample1.pdf");
FileOutputStream fOut = new FileOutputStream(file);
PdfWriter.getInstance(doc, fOut);
//open the document
doc.open();
Paragraph p1 = new Paragraph("Hi! I am generating my first PDF using iText library");
Font paraFont= new Font(Font.COURIER);
p1.setAlignment(Paragraph.ALIGN_CENTER);
p1.setFont(paraFont);
//add paragraph to document
doc.add(p1);
Paragraph p2 = new Paragraph("This is an example of a simple paragraph");
Font paraFont2= new Font(Font.COURIER,14.0f,Color.GREEN);
p2.setAlignment(Paragraph.ALIGN_CENTER);
p2.setFont(paraFont2);
doc.add(p2);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.android);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
Image myImg = Image.getInstance(stream.toByteArray());
myImg.setAlignment(Image.MIDDLE);
//add image to document
doc.add(myImg);
//set footer
Phrase footerText = new Phrase("This is an example of a footer");
HeaderFooter pdfFooter = new HeaderFooter(footerText, false);
doc.setFooter(pdfFooter);
} catch (DocumentException de) {
Log.e("PDFCreator", "DocumentException:" + de);
} catch (IOException e) {
Log.e("PDFCreator", "ioException:" + e);
}
finally
{
doc.close();
}
this is simple program by usinf droidText libary. Try this its working.
Upvotes: 0
Reputation: 654
Try this way...
try {
Document document = new Document();
File root = new File(Environment.getExternalStorageDirectory(),
"foldername");
String sdcardhtmlpath = root.getPath().toString() + "/filename.pdf";
PdfWriter.getInstance(document,
new FileOutputStream(sdcardhtmlpath));
document.open();
Paragraph paragraph = new Paragraph("your string");//add ur string here
paragraph.setAlignment(Element.ALIGN_LEFT);
document.add(paragraph);
document.close();
} catch (Exception e) {
Log.v("PDFcreation", e.toString());
}
Upvotes: 2