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,571
  • PDFWriter1.1.zip
    25.6 KB · Views: 2,694
Last edited:

rbsoft

Active Member
Licensed User
Longtime User
Works perfect now as long I leave calls to "addRawcontent" out.

What is "addRawcontent" really good for? What do it's parameters mean?

Rolf
 
Last edited:

NJDude

Expert
Licensed User
Longtime User
Ok, this is my report:

I followed what other users said about "AddRawcontent", so, I excluded those lines in the sample, recompiled and this is what happened:

1- EVO 4G (Froyo) Adobe Reader version 10.1.0 = Corrupted file error

2- GalaxyTab (Android 3.1) Adobe Reader 10.1.0, QuickOffice 4.6

a) Opening the file with Adobe gives the corrupted file error
b) Opening the file with QuickOffice gives no error.

However, running the app more than once gives me a NULL POINTER error, this happens on all the devices I've tested.
 

vb1992

Well-Known Member
Licensed User
Longtime User
Here is a PDF project to test with
Curious to know if it works on different devices
 

Attachments

  • Test4PagePDF.zip
    10 KB · Views: 588
Last edited:

vb1992

Well-Known Member
Licensed User
Longtime User
Here is the PDF file that was generated,
please test this in any/all Pdf Viewers
thanks

( 4th page is random generated characters )
 

Attachments

  • Report01-111130112432.pdf
    11.4 KB · Views: 482

rbsoft

Active Member
Licensed User
Longtime User
Program works on Galaxy Tab 7" und HTC Desire.

PdfReader on Desire cannot read file.
Galaxy Tab / ThinkFree Office works.

On Desktop:
PdfExchange works.
Nitro Pdf Reader works.
Google Chrome works.
MS Word 2000 opens file and shows contents.

Same result as with my little test project.

Rolf
 

vb1992

Well-Known Member
Licensed User
Longtime User
Interesting because the pdf file run through a pdf analyzer shows no errors.
This library generates PDF 1.4 compliant docs

What kind of error happens with HTC Desire
 

rbsoft

Active Member
Licensed User
Longtime User
No error when writing file, just Pdf reader cannot open it.

I have to go to an appointment right now. Will check how the file works on desktop when I am back.

Rolf
 

vb1992

Well-Known Member
Licensed User
Longtime User
Using zingbat fonts, I can get checkmarks and xmarks and place a rectangle box around them.


give this a whirl


B4X:
      PdfWriter1.newPage
      
            PDFWriter1.setFont(Fonts.SYMBOL, Fonts.HELVETICA)
         PDFWriter1.setFont(Fonts.SUBTYPE, Fonts.ZAPDINGBATS)
  
              
            
          PDFWriter1.addText(10, 600, 90, Chr(111))
         PDFWriter1.addText(20, 620, 100, Chr(51))




B4X:
      PdfWriter1.newPage
      
            PDFWriter1.setFont(Fonts.SYMBOL, Fonts.HELVETICA)
         PDFWriter1.setFont(Fonts.SUBTYPE, Fonts.ZAPDINGBATS)
  
              
            
          PDFWriter1.addText(10, 600, 90, Chr(111))
         PDFWriter1.addText(11, 602, 80, Chr(55))
 
Last edited:

rbsoft

Active Member
Licensed User
Longtime User
What kind of error happens with HTC Desire

As mentioned the HTC Desire has no problem creating the file. File is readable on desktop apps, it is just the PdfViewer app that has problems with it. I installed the AdobeReader on it, it also cannot open the file. Says that the file is corrupted.

Dingbats fonts work nice.

Rolf
 
Last edited:

rbsoft

Active Member
Licensed User
Longtime User
Yes - absolutely no problem.
Adobe and PdfViewer (HTC app) read it.

Rolf
 

vb1992

Well-Known Member
Licensed User
Longtime User
Maybe start off with a test, with one simple word "HELLO"
and don't choose any fonts, to see if that will work.

The bare minimum to see if that works?
 

Harris

Expert
Licensed User
Longtime User
Above pdf opens fine in Adobe 10.1.1 on my desktop
 

rbsoft

Active Member
Licensed User
Longtime User
Does not open on my Desire.
(I'm having wrong desires I guess...)

I had tried already myself with a minimum text in my test app. Something seems to be incompatible.

Rolf
 

vb1992

Well-Known Member
Licensed User
Longtime User
Thanks Rolf for testing, -- maybe you
can download some PDF creation apps on the Desire
from the APP store, and see how the Desire handles
those opening those PDF's created.
 

Fox

Active Member
Licensed User
Longtime User
thank you

thank you for this wonderfull and useful extension :)

really really helpful...

but i have a question is it possible that you can create an example how can the user make an pdf from an List... ?

this would be very great and help much i think...
 

Harris

Expert
Licensed User
Longtime User
I wrote the author JSD. Here is his response to my question below.

Hey Harris,

Nice to see the folks over B4A use APW, hopefully all of you can build
interesting apps with it.

The addRawContent method is intended to directly inject any valid PDF
content to the resulting PDF stream. To use it properly you must follow
the PDF specs, or you may corrupt the resulting PDF. In the demo I'm
using it to set the colours of the texts and to set the style of the
rectangle I'm painting. There's no trick, if you open the resulting PDF
file with a text editor, you will see that lines just like they where
passed to addRawContent().

About the image support, APW does not support images of any kind.
The correct way to add images to APW is to add XObjects support, but it
is a complex task to achieve since it involves a lot of time of research
and development. Unluckily I don't have that time.
In case you want to implement XObjects for you, you should follow the
Adobe PDF 1.4 Reference (APW produce valid PDF 1.4 documents), specially
the chapter 4.

Regards,
Javier Santo Domingo

On 07/12/2011 09:59 p.m., Harris wrote:
> Hi Javier,
> I asked the folks at B4A if they could write a B4A lib from your PDF writer
> and behold they did! Excellent. Of course we credit you. It works well
> for the most part as long as I don\'t use addRawContent. (dont quite
> understand what this method does...).
>
> Have you found a way to incorporate images (png, jpg) into the pdf? I
> truly need to add a signature to the document to make it (legal). Thanks
> so much for your effort - nothing else exists out there for writing PDFs
> on Android (that I can find).
>
 

vb1992

Well-Known Member
Licensed User
Longtime User
Harris, What would you like to achieve with PDF (besides the images).
I invested lots of time into testing this, so maybe I can help you if you
have something in mind.
 
Top