Documentation for creating files in PDF format

Asked

Viewed 332 times

5

I am some time analyzing free libraries for creating PDF for use in commercial applications on Android, and so far I have not found one that meets all my expectations, functionalities and licenses. As can be seen in that and in that ask right here at Sopt.

The best library I’ve found so far was the Android PDF Writer, that has the ability to create basic PDF and has a license (BSD) suitable for my project, as per in this excellent software license post.

Despite doing a lot of PDF-related stuff, it was designed only for simple PDF creation, with text and images.

Then I would like to know/find some logic documentation to create a PDF, so I can add resources to the library?

As an example of cited library code, to add a text to the PDF is implemented in this way:

public void addText(int leftPosition, int topPositionFromBottom, int fontSize, String text, String transformation) {
    addContent(
        "BT\n" +
        transformation + " " + Integer.toString(leftPosition) + " " + Integer.toString(topPositionFromBottom) + " Tm\n" +
        "/F" + Integer.toString(mPageFonts.size()) + " " + Integer.toString(fontSize) + " Tf\n" +
        "(" + text + ") Tj\n" +
        "ET\n"
    );
}

The first implementation I’m looking for is for the creation of tables where the only documentation I’ve found so far was that one, that I could not understand complement, and I would like something more detailed, with for example: table structure, header structure, column structure, row structure, item structure. To understand and implement each part of the table as a module.

1 answer

-1

Good friend, make sure this can help you:
one method is for you to create and another is for you to open.
follows the link too. http://javafordummy.blogspot.com.br/2013/10/how-to-create-and-display-pdf-file-in.html


 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, "demo.pdf");
   FileOutputStream fOut = new FileOutputStream(file);

   PdfWriter.getInstance(doc, fOut);

   //open the document
   doc.open();

   /* Create Paragraph and Set Font */
   Paragraph p1 = new Paragraph("Hi! I am Generating my first PDF using DroidText");

   /* Create Set Font and its Size */
   Font paraFont= new Font(Font.HELVETICA);
   paraFont.setSize(16);
   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");

  /* You can also SET FONT and SIZE like this */
   Font paraFont2= new Font(Font.COURIER,14.0f,Color.GREEN);
   p2.setAlignment(Paragraph.ALIGN_CENTER);
   p2.setFont(paraFont2);

   doc.add(p2);

   /* Inserting Image in PDF */
   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);

   Toast.makeText(getApplicationContext(), "Created...", Toast.LENGTH_LONG).show();

  } catch (DocumentException de) {
   Log.e("PDFCreator", "DocumentException:" + de);
  } catch (IOException e) {
   Log.e("PDFCreator", "ioException:" + e);
  } 
  finally
  {
   doc.close();
  }
 }      

 void openPdf()
 {
  Intent intent = new Intent(Intent.ACTION_VIEW);
  String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDF";

  File file = new File(path, "demo.pdf");

  intent.setDataAndType( Uri.fromFile( file ), "application/pdf" );
     startActivity(intent);
 }

  • Hello, which library is being used to generate this PDF?

  • Fernando, he uses the droidText

  • 2

    But this library uses LGPL v3 license, and as I mentioned in the question I need something more permissive, since the LGPL license v3, requires that the by-product be licensed from the same license and that source code be opened. Which in my case is not possible, since it is a business application of my employer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.