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

frapel

Active Member
Licensed User
Longtime User
Adobe PDF color format

This is it. light blue...
PDFWriter1.addRawContent("0 184 245 rg ")

Im so stupid Its just the rgb numbers code.:sign0161:

I've tried myself with RGB values but ranges aren't from 0 to 255, they go from 0 to 1 where 0 is totally off and 1 is totally on. To obtain a value in the interval 0-1 from an integer of RGB triplet you should divide it by 255.

For example,
PDFWriter1.addRawContent("0.64 0.79 0.22 rg ")
will render a light green corresponding to RGB(165, 202, 57)


Here you can find all the info (chapter 4.5):
Adobe Systems Incorporated, PDF Reference, Sixth edition
 

mokrokuce

Member
Licensed User
Longtime User
I trying to make pdf file from SQL.
Everything works great, except of text encoding. I'm from Czech and need ISO-8859-2 encoding. But it doesn't work. All texts from SQL are correct in Labels etc, but when I'm trying tu get them in PDF it's wrong.
.
.
.

Part of riginal text is:
..lukáš den domorodého příspěvku...
And result from PDF:
lukÆš den domorodØho płíspìvku

Whole day I'm trying to solve this, with no success.
Pleace help

Hi all, I am from Serbia and I'm having same issues with this:

B4X:
   PDFWriter1.setFont2(Fonts.SUBTYPE, Fonts.COURIER,Fonts.WIN_ANSI_ENCODING)
    PDFWriter1.addText(70, 50, 12, "This is an example of PDFWriter. Šš Đđ Čč Ćć Žž")
    PDFWriter1.addLine(150, 140, 270, 140)

And the output:
B4X:
   PDFWriter1.outputToFile(File.DirDefaultExternal,"myNewPDF-srb.pdf",PDFContent,"ISO-8859-2")

is like in the file attached. Note that in labels it is shown as it should be.
 

Attachments

  • myNewPDF-srb.pdf
    873 bytes · Views: 286
  • Screenshot_2013-03-28-15-23-31.png
    Screenshot_2013-03-28-15-23-31.png
    24.4 KB · Views: 311

dunski

Member
Licensed User
Longtime User
This is not in the Wiki Library

I couldn't find this library in the Wiki Libraries section

It should be its a great library!

As far as I know it is the only way to create a PDF document with B4A
 

Harris

Expert
Licensed User
Longtime User
As far as I know it is the only way to create a PDF document with B4A


Currently. Adobe seems to ignore Android for some reason (with java code)... Pissed off with someone I guess.

The wiki is in this thread. This is what is known about using PDFWriter.

One needs to read the official PDF format doc (which I have until my eyes glazed over). I have posted some examples as well as others.

Sorry, but this is what we have to date - which is alot!
 

Dave O

Well-Known Member
Licensed User
Longtime User
Thanks for this library - I got it mostly working with my sketching app in about half an hour.

I use the addImage method to save sketches to pages of a PDF. It crashed my app when I output the full-size image (~1200x800), but it worked when I used the createScaledBitmap method of the BitmapPlus library to scale it down to 2/3 size.

Question - my sketch app lets the user link sketches together using hotspots. The PDF format allows this too, but this library does not (yet). Any plans to add that to the library?

Thanks again!
 

Dave O

Well-Known Member
Licensed User
Longtime User
Correction - it worked when I create a single-page PDF from a scaled bitmap, but when I created 3 pages from 3 scaled bitmaps, it crashed on the converseDocument call.

It worked when I scaled the bitmaps smaller (33% instead of 50%), so I'm guessing this is a memory issue.

My image files are very small when stored as PNG (because they are B&W line drawings with occasional yellow highlights), but I guess the bitmap (stored in memory) is uncompressed and therefore blowing out the PDF creation process?
 

aggelos

Member
Licensed User
Longtime User
Hello,

i am posting this in order to save someone else some time.

i am just commenting after 2 hours of trying to create a pdf with
unicode characters.it doesn't work.and its all Adobe's fault.

after reading adobe 1.5 reference and surfing the web about pdf+unicode
i wonder how the hell PDF became a worldwide standard!

its like adobe said...lets support english and forget the rest 200 countries on the planet.


anyway.for anyone trying to display unicode characters.don't bother.

create a php script if you want to give your users the option for creating pdf.

Check this tFPDF




Angelos
 
Last edited:

Harris

Expert
Licensed User
Longtime User
I know what you mean....

I have created perfectly fine PDFs with this lib that Windows Abobe reader would condemn as corrupted - yet every other viewer would display just fine.

As of yet, they still don't have an Android version of reader to my knowledge...
Probably would get OOM errors trying to load that bloatware anyways...
 

miquelr

Member
Licensed User
Longtime User
Tankyou for this very useful lib!
How may I to make a landscape document?
It is possible to define all doc orientation or I will have to rotate every element of the document?

Miquel
 

Firpas

Active Member
Licensed User
Longtime User
Hallo,

Is there any way to format the text (Numbers between 1 and 999) on the right side? So like in B4A Gravity.RIGHT ?

Thank you.


Did you find any solution to the problem of text alignment to the right with PDFWriter?
 

Harris

Expert
Licensed User
Longtime User
Did you find any solution to the problem of text alignment to the right with PDFWriter?


Messy, but currently the only way to right justify is to set the leftPosition of addtext and use a mono-space font (to keep column straight).

Something like (when numbers are 1 - 999):
Dim leftpos as int

Select s.length
Case 1
leftpos = 16dip
Case 2
leftpos = 14dip
Case 3
leftpos = 12dip

End Select

PDFWriter1.addText( leftpos, topPositionFromBottom, fontSize, text)
 

GaryOCO

Member
Licensed User
Longtime User
What would be nice would be if the PDFWriter1.NewPage command would allow us to specify Portrait or Landscape.

How can we do that otherwise?
 

GaryOCO

Member
Licensed User
Longtime User
Sorry,
I know how to form a PDF as Portrait or Landscape but how do I change the orientation mid-document and insert, for instance, a Landscape page into the pages of a Portrait document?
 

Locutus

Member
Licensed User
Longtime User
I would also like to know how to add a new page as either portrait or landscape.
Can anyone also provide an example of addText2 ? I cannot seem to get it to work for rotating text.
 

Harris

Expert
Licensed User
Longtime User
Sorry,
I know how to form a PDF as Portrait or Landscape but how do I change the orientation mid-document and insert, for instance, a Landscape page into the pages of a Portrait document?

Probably not supported in the same document.
Try "CreateNewDocument" where you decide you need the landscape page.
 

Harris

Expert
Licensed User
Longtime User
PDFWriter1.addText2(100, 400, 35, "@COPYRIGHT 2012", Fonts.DEGREES_45_ROTATION)

This Doesn't work?
 
Top