B4A Library PDFWriter

Discussion in 'Additional libraries, classes and official updates' started by XverhelstX, Nov 26, 2011.

  1. XverhelstX

    XverhelstX Well-Known Member Licensed User

    Hello everyone,

    Here is yet another library: PDFWriter.
    Linked by user mwhetmore at this topic, you can now create your own PDF files and save them!
    The library has 3 declarations:

    I. PDFWriter
    Dim PDFWriter1 As PDFWriter
    Declare this in globals so you can benifit from all main methods for writing your PDF File.

    1.1 Initialize
    PDFWriter1.Initialize("PDFWriter1",PaperSize.A4_WIDTH, PaperSize.A4_HEIGHT)
    This initializes your PDF writer and gives in an eventname for when the pdf has been created, and it creates a new document with the papersizes than you can choose. (A1, A2, A3, and a lot more!).

    1.2 addLine
    PDFWriter1.addLine(int fromLeft, int fromBottom, int toLeft, int toBottom)
    Adds a line to your PDF file. The parameters are self-explanatory.

    1.3 addRawContent
    PDFWriter1.addRawContent(String rawContent)
    Adds raw content to your pdf file.
    For example: PDFWriter1.addRawContent("0 1 0 rg\n")

    1.4 addRectangle
    PDFWriter1.addRectangle(int fromLeft, int fromBottom, int toLeft, int toBottom)
    Adds a rectangle to your pdf file.

    1.5 addText
    PDFWriter1.addText(int leftPosition, int topPositionFromBottom, int fontSize, String text)
    PDFWriter1.addText2(int leftPosition, int topPositionFromBottom, int fontSize, 
    String text, String transformation)
    This will add text to your PDF file.
    The difference between addText and addText2 is that you can add a transformation to your text, see chapter III. PDFStandardFonts.

    1.6 ConverseDocument
    This wll create you a PDF file. Sub ConversionDone will be raised when the conversion has been done.

    Sub PDFWriter1_ConversionDone (Content As String)
       PDFContent = Content
    ToastMessageShow("Conversion has been done.",False)
    End Sub
    1.7 CreateNewDocument
    PDFWriter1.CreateNewDocument(int Width, int Height)
    This wll create a new PDF document with the given Width and Height in Papersizes, like in the initialize method.

    1.8 newPage
    This wll create a new PDF page in the same document.

    1.9 outputToFile
    PDFWriter1.outputToFile(String Directory, String FileName, String pdfContent, String encoding)
    This will save your pdf content in the given Directory with the given filename with the given encoding.

    2.0 outputToScreen
    This will show your pdfcontent in a label. (note that it won't show the pdf itself but the content!)

    Label1.Text = PDFWriter1.outputToScreen
    2.1 setFont
    PDFWriter1.setFont(String subType, String baseFont)
    String subType, String baseFont, String encoding)
    This will set the font in your PDF file.
    The difference between setFont and setFont2 is that you can add a encoding to your text in setFont2, see chapter III. PDFStandardFonts.

    II. PDFPaperSizes
    Dim PaperSize As PDFPaperSizes
    With PaperSize you can select, when you create a new document (or with initialize), what paper you would like to use to work with.
    Here are all the options:

    III. PDFStandardFonts
    Dim Fonts As PDFStandardFonts
    And to end, you can choose between a lot of available fonts.
    I won't put them here, but it varies from Times New Roman, to Courier, and some more.

    IV. License

    The library is licensed under BSD!
    So you just have to mention the author. (not me!)
    Author: Javier Santho Domingo (j-a-s-d@coderesearchlabs.com)

    In the attachments, you can find the library files and 2 text files:
    A readme.txt and Sample.txt
    The project was just to big to upload here at the forum.
    So just copy the content of Sample.txt in a new Basic4Android Project and
    check the PDFWriter at the right hand pane in the library tab of the IDE, and run the project.

    Have fun.


    Attached Files:

    Last edited: Apr 6, 2012
    GudEvil, soonya and jsanchezc like this.
  2. rbsoft

    rbsoft Active Member Licensed User

    Great job, Tomas.

    Thank you for the quick response. I will play around with it.

  3. vb1992

    vb1992 Well-Known Member Licensed User

    Great job - this is very useful.:sign0087:
  4. NJDude

    NJDude Expert Licensed User

    If I try to open the generated PDF I get "File corrupted", I did this on the device and copied it to my PC.

    My Adobe Reader version: 10.1.1

    If I run the app more than once, I get the error on the attached screenshot.

    Great library by the way, thanks.

    Attached Files:

    Last edited: Nov 26, 2011
  5. XverhelstX

    XverhelstX Well-Known Member Licensed User

    If i try to open it on my device, I also get the error that the file is corrupted.
    However when I open it on my pc, I don't receive any error.
    I don't know what causes it though.

    Last edited: Nov 26, 2011
  6. NJDude

    NJDude Expert Licensed User

    What's your PC's reader version?
  7. XverhelstX

    XverhelstX Well-Known Member Licensed User

    I use nitro PDF Professional.
  8. NJDude

    NJDude Expert Licensed User

    It's a known issue that certain PDF's are version dependant, I might be wrong, but this library might work with Adobe reader (and compatible) version 5 and 6 only, unless there's something else I don't know about.
  9. vb1992

    vb1992 Well-Known Member Licensed User

    You would think the latest Adobe Reader is backwards compatible...
  10. Fox

    Fox Active Member Licensed User

    ok i use the sample and try it with adobe reader on 3 android devices (not work) on copy to pc and adobe acrobat reader 10 not work on adobe acrobat (suite pro) not work...

    i think the lib need some fixes....
  11. Smee

    Smee Well-Known Member Licensed User

    Whoa!! this looks good.

    Gotta try it out.

  12. rbsoft

    rbsoft Active Member Licensed User

    I had to change the path of the file to DirRootExternal. Otherwise I never could find the generated pdf.

    ThinkFree Office showed some elements of the Pdf on the device.
    PdfExchange and NitroReader just showed on page 3 then line "Ported by: RootSoft LLC."

  13. Smee

    Smee Well-Known Member Licensed User

    Tried it on Viewsonic VS10 works good. Just got to work out the formatting properly
  14. boten

    boten Active Member Licensed User

    sample code worked fine on my SGS using APV PDF Viewer
  15. boten

    boten Active Member Licensed User

    suggestion for features:

    1) Method to return page width & height (depending on paper size) in "fontsize" units. This will allow to write a sequence of text lines onto the PDF.
    By trial & error I discovered that A4 is about 800-900 "units" high, so using fontsize 20 I cud fit 41 lines on page, 83 lines with fontsize 10.

    2) measure text position from TOP of page

    3) allow "automatic" page skip when new line of text wud be beyond the page size, so one does not have to count positions "manually"

    not asking much, right? :cool:
  16. vb1992

    vb1992 Well-Known Member Licensed User

    I was able to create a pdf, and it's view-able
    with THINK-FREE office app - ThinkFree Mobile

    I attached the sample pdf
    Last edited: Jan 18, 2012
  17. peacemaker

    peacemaker Well-Known Member Licensed User

    The best would be if to attach the screenshot of the right planned content of the PDF :)
    That is not well readable in Adobe Reader, at least.
  18. vb1992

    vb1992 Well-Known Member Licensed User

    THINK-FREE office app reads it fine.
    Adobe Acrobat doesn't like it.

    I sent the PDF into a analyzer
    and I am attaching the screen shot results

    I also sent the PDF into a repair program, and I will
    attached the repaired PDF to show what it looks like

    Hopefully this can help

    desktop.zip has the 3 screen shots

    Attached Files:

    • two.pdf
      File size:
      8 KB
    Last edited: Nov 29, 2011
  19. vb1992

    vb1992 Well-Known Member Licensed User

    This seems to be the common cause for the problem:

    "You could have unbalanced BT and ET sequences"

    Not sure how that relates to the library though...?
  20. Harris

    Harris Well-Known Member Licensed User

    WOW!!!! This is great!!!
    I downloaded it on Friday and haven't been near an internet connection since.

    Thanks ever so much to Tomas for this great lib (and of course to JSD - the originator).

    I played all weekend with it and am able to produce valid PDFs (error free) on both device (Acer A100) and desktop using Adobe 9.

    First, I found that any reference to "addRawcontent" would cause an error in Adobe (but not on Acer). I don't quite know what addRawcontent does - seems to change font color (don't understand the param list). Removing it, producing just black text, works like a charm! I also thought that random placement of objects was causing an issue - but this is not the case. I can place text, lines and rects any where on the page at any time and the result comes out just fine.

    Second, I found that at times, the setfont was not respected. I had to place a setfont(symbol) before setfont(Courier) before the latter would take.

    Using zingbat fonts, I can get checkmarks and xmarks and place a rectangle box around them.

    Now, if I could only get a graphic image (like a signature) into the PDF. That's for another day I guess. Paragraph wrap would also be helpful.

    Again, thank you very much for this. It will (and is) very useful for me and others. Great Job!
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice