B4A Library PDFWriter

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
B4X:
Dim PDFWriter1 As PDFWriter
Declare this in globals so you can benifit from all main methods for writing your PDF File.


1.1 Initialize
B4X:
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
B4X:
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
B4X:
PDFWriter1.addRawContent(String rawContent)

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

1.4 addRectangle
B4X:
PDFWriter1.addRectangle(int fromLeft, int fromBottom, int toLeft, int toBottom)

Adds a rectangle to your pdf file.

1.5 addText
B4X:
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
B4X:
PDFWriter1.ConverseDocument
This wll create you a PDF file. Sub ConversionDone will be raised when the conversion has been done.

B4X:
Sub PDFWriter1_ConversionDone (Content As String)
   PDFContent = Content
   ProgressDialogHide
   ToastMessageShow("Conversion has been done.",False)
End Sub

1.7 CreateNewDocument
B4X:
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
B4X:
PDFWriter1.newPage
This wll create a new PDF page in the same document.

1.9 outputToFile
B4X:
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.

B4X:
PDFWriter1.outputToFile(File.DirDefaultExternal,"myNewPDF.pdf",PDFContent,"ISO-8859-1")

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

B4X:
Label1.Text = PDFWriter1.outputToScreen

2.1 setFont
B4X:
PDFWriter1.setFont(String subType, String baseFont)
PDFWriter1.setFont2(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
B4X:
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:

A0_WIDTH
A0_HEIGHT
A1_WIDTH
A1_HEIGHT
A2_WIDTH
A2_HEIGHT
A3_WIDTH
A3_HEIGHT
A4_WIDTH
A4_HEIGHT
A5_WIDTH
A5_HEIGHT
A6_WIDTH
A6_HEIGHT
A7_WIDTH
A7_HEIGHT
A8_WIDTH
A8_HEIGHT
A9_WIDTH
A9_HEIGHT
A10_WIDTH
A10_HEIGHT
B0_WIDTH
B0_HEIGHT
B1_WIDTH
B1_HEIGHT
B2_WIDTH
B2_HEIGHT
B3_WIDTH
B3_HEIGHT
B4_WIDTH
B4_HEIGHT
B5_WIDTH
B5_HEIGHT
B6_WIDTH
B6_HEIGHT
B7_WIDTH
B7_HEIGHT
B8_WIDTH;
B8_HEIGHT
B9_WIDTH
B9_HEIGHT
B10_WIDTH
B10_HEIGHT
LETTER_WIDTH
LETTER_HEIGHT
TABLOID_WIDTH
TABLOID_HEIGHT
LEDGER_WIDTH
LEDGER_HEIGHT
LEGAL_WIDTH
LEGAL_HEIGHT
STATEMENT_WIDTH
STATEMENT_HEIGHT
EXECUTIVE_WIDTH
EXECUTIVE_HEIGHT
FOLIO_WIDTH
FOLIO_HEIGHT
QUARTO_WIDTH
QUARTO_HEIGHT

III. PDFStandardFonts
B4X:
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 ([email protected])


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.

Tomas
 

Attachments

  • PDFWriter1.0.zip
    18.7 KB · Views: 1,572
  • PDFWriter1.1.zip
    25.6 KB · Views: 2,695
Last edited:

Harris

Expert
Licensed User
Longtime User
Thanks vb1992.

I have created several complex pdfs already with lines, rects, fonts and columns. They read well on devices with PDF to go and Adobe Reader X on my desktop. It would have been nice to save an image into the file as well (signature capture).

Need a CutePDF for Android if we want to get fancier.
 

Harris

Expert
Licensed User
Longtime User
I will send you an example next week (by PM) when I finish my outline this weekend - kids permitting. Not too daunting... Counting how many rows fit in the horizontal space and placing them correctly (in reverse order - top down) in the three columns was fun.

Since text does not "wrap" on a line, I will need to figure out how to place a paragraph using addtext using a loop (or something)...? Likely, based on the number of characters per line, create a list with the correct number of available words in the paragraph.

Other than that, a pdf in an android app (that I can email) is absolutely great! A report writer would be nice, but likely huge overhead. Even with that - what is better than output to pdf?

Cheers!
 

vb1992

Well-Known Member
Licensed User
Longtime User
This might work


'
'
' Make font black
PDFWriter1.addText2(100, 400, 35, "@COPYRIGHT 2012", Fonts.DEGREES_45_ROTATION)


'
' Change the Font Color here to light grey? White?
'
PDFWriter1.addText2(102, 400, 34, "@COPYRIGHT 2012", Fonts.DEGREES_45_ROTATION)
 

Harris

Expert
Licensed User
Longtime User
Yes, it was the pdf-tool in the two.pdf you posted. It was rotated on each page.

I will try what you suggested above.

Thanks
 

Fox

Active Member
Licensed User
Longtime User
How you can create an pdf from an database?! Any example here...
 

Harris

Expert
Licensed User
Longtime User
If you mean, create pdf from content in database records - then yes.

Using the pdf lib is kind of like creating a report writer. You need to know where to place elements, how to increment your (next) line, how many lines are able to print on a page. I will post a clean example soon. It depends on what you are trying to accomplish. Each report is up to the developer.

It would be SO nice to have a report writer (for Android) that would output to PDF... (someday)
 

Harris

Expert
Licensed User
Longtime User
Example attached - Pdf and table records
 

Attachments

  • Pdf example .txt
    6.1 KB · Views: 602

Harris

Expert
Licensed User
Longtime User
Another example for Fox.
 

Attachments

  • Pdf example1.txt
    6.9 KB · Views: 453

Harris

Expert
Licensed User
Longtime User
Excellent. Now you see how limited it is as a report writer - yet very useful it can be (all it takes is code - as with everything else we do).

Cheers
 

Smee

Well-Known Member
Licensed User
Longtime User
I have run into a strange phenomenon.

I have written some code that generates the required PDF perfectly. I then tried to move it to a separate Service Module to make it more Generic. The resulting PDF filesize was 0. I checked the code and everything was exactly the same down to the declarations etc in the correct places.

When this did not work I tried moving it to a Code Module. Same result. The code ran without errors. I got all the correct messages to say the file had been saved including the message displayed in the
sub PDFWriter1_ConversionDone (Content As String)

I then tried to run the code that was working in the working module from another module. Again everything ran ok but a zero size file resulted.

Any ideas? I dont want to have to duplicate the code in each Activity module that needs to create a PDF

Thanks


UPDATE:

B4X:
PDFWriter1.ConverseDocument
   PDFWriter1.outputToFile(File.DirDefaultExternal,"Test.pdf",PDFContent,"ISO-8859-1")
   ToastMessageShow("PDF Saved.",False)
PDFWriter1.outputToScreen
End Sub


Sub PDFWriter1_ConversionDone (Content As String)
   PDFContent = Content
   ProgressDialogHide
   ToastMessageShow("Conversion has been done.",False)
End Sub

Ok i copied and pasted all the code to a new code module and stepped through it. It seems that the last sub IS NOT BEING EXECUTED

Any ideas? This is very frustrating
 
Last edited:

Smee

Well-Known Member
Licensed User
Longtime User
Strange indeed.

The whole thing runs fine if the code is pasted into a service module.

Anyone know why?
:BangHead::BangHead:
 

Smee

Well-Known Member
Licensed User
Longtime User
Does anyone know how to change the font colours
 

Harris

Expert
Licensed User
Longtime User
PDFWriter1.addRawContent("1 0 0 rg ") ' changes the next line to red

PDFWriter1.addText(221, 643, 14, " - DEFECT")

PDFWriter1.addRawContent("0 0 0 rg ") ' changes the font to black

vb1992 knows more about addRawContent. What I do know is if this method is not used "correctly", the pdf will corrupt.
 

Harris

Expert
Licensed User
Longtime User
Strange indeed.

The whole thing runs fine if the code is pasted into a service module.

Anyone know why?
:BangHead::BangHead:

I am afraid of a whole bunch of public vars if I move the pdf to a service or code module. How did you avoid this?

Thanks
 

vb1992

Well-Known Member
Licensed User
Longtime User
This will give you a blue star, and a red line of text
It seems to work with all the readers except for "Adobe Reader for Android" (Go figure?)

B4X:
 PDFWriter1.setFont2(Fonts.SUBTYPE, Fonts.SYMBOL, Fonts.WIN_ANSI_ENCODING)
          PDFWriter1.setFont2(Fonts.SUBTYPE, Fonts.COURIER, Fonts.WIN_ANSI_ENCODING)
         
   PDFWriter1.addRawContent("1 0 0 rg ")
    PDFWriter1.addText(70, 50, 12, "This is Red down here in small print")
   
   PDFWriter1.addRawContent("0 0 1 rg ")
   PDFWriter1.addRawContent("4 0 0 4 315 204 cm ") 
   PDFWriter1.addRawContent("0 5.5 m ")
   PDFWriter1.addRawContent("-4 -5.5 l ")
   PDFWriter1.addRawContent("6 1 l ")
   PDFWriter1.addRawContent("-6 1 l ") 
   PDFWriter1.addRawContent("4 -5.5 l ")
   PDFWriter1.addRawContent("f ")
 

Smee

Well-Known Member
Licensed User
Longtime User
Thanks for the replies guys, That colour change works fine. Is there a list of colours anywhere i could use. I am trying to pick up on the watermark idea by making the colours light gray.


I am afraid of a whole bunch of public vars if I move the pdf to a service or code module. How did you avoid this?

All i did was got it working in an activity module and then pasted the code to a service module as follows

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim PDFWriter1 As PDFWriter
   Dim PDFContent As String
   Dim PaperSize As PDFPaperSizes
   Dim Fonts As PDFStandardFonts
   Dim LN,PageNum,Pages,SentNo As Int      
   Dim CustTotal As Double
End Sub

ub Service_Start (StartingIntent As Intent)
MakePDF
End Sub


In the calling Activity i simply write

B4X:
      DoEvents
      ProgressDialogShow("Generating Document.")
      DoEvents
      StartService(spdf)
      StopService(spdf)
      ProgressDialogHide
      ToastMessageShow("File Converted and Saved.",False)
      OpenPDF("/test.pdf")

It seems to work fine. It just will not run if called as an activity module or code module.

At least not for me.

Thanks again
 
Top