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

koezuma

Member
Licensed User
Longtime User
error when insert picture

dear all

i got error message when i use script bellow :

Dim picture As Bitmap

PDFWriter1.setFont(Fonts.SYMBOL, Fonts.COURIER_BOLD)
PDFWriter1.setFont(Fonts.SUBTYPE, Fonts.COURIER_BOLD)
picture.Initialize(File.DirRootExternal,"1.jpg")
PDFWriter1.addImage(10,10,picture)

best regards
koezuma
 

koezuma

Member
Licensed User
Longtime User
error when insert picture

thx errel,

where i found log file?

sorry for stupid question.

best regards
koezuma
 

TheMightySwe

Active Member
Licensed User
Longtime User
Scandinavian letters does not work

Hi,

When I try the library with Scandinavian fonts (Swedish) the letters åäö and ÅÄÖ disapears and I get blanks instead.

And do you have any good way to align the text to the right in a invoice?

Thanks for a really good library, and I hope that you can help me with this small issues.
 

miga

Member
Licensed User
Longtime User
dear all

i got error message when i use script bellow :

Dim picture As Bitmap

PDFWriter1.setFont(Fonts.SYMBOL, Fonts.COURIER_BOLD)
PDFWriter1.setFont(Fonts.SUBTYPE, Fonts.COURIER_BOLD)
picture.Initialize(File.DirRootExternal,"1.jpg")
PDFWriter1.addImage(10,10,picture)

best regards
koezuma

Same for me. Applicatio unexpectly crash on

PDFWriter1.ConverseDocument

when I adding image with:

PDFWriter1.addImage(10, 100, Bitmap1)

Thanx

Solved. That was just wrong source image size. Don't know why. When I changed siurce image size everything works.
 
Last edited:

miga

Member
Licensed User
Longtime User
Encoding problem

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. Here is part of my code:

B4X:
...
PDFWriter1.setFont2(Fonts.SUBTYPE, Fonts.HELVETICA, "ISO-8859-2")
Dim text As String
text = Main.Cursor1.GetString ("note")
PDFWriter1.addText(400, 730, 10, text)
...
PDFWriter1.outputToFile(File.DirDefaultExternal, Main.proj &".pdf", PDFContent, "ISO-8859-2")
...

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
 

dunski

Member
Licensed User
Longtime User
colours

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.

Hi
Does anyone know what the colour code for light blue is? Or If anyone knows where there is a table or something with more codes for the font colours.
Thanks
 

dunski

Member
Licensed User
Longtime User
Hi
Does anyone know what the colour code for light blue is? Or If anyone knows where there is a table or something with more codes for the font colours.
Thanks

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

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

Paulsche

Well-Known Member
Licensed User
Longtime User
Is there a way to text in a table format right justified?
Similarly Gravity.RIGHT ?
 

enrico

Active Member
Licensed User
Longtime User
PDF size

A PDF of 1 page with 8 charts is about 14 Mb, with 10 charts 18 Mb, more than 10 it goes Out Of Memory (Force Close).
We need a way to make those PDF smaller.
 

Paulsche

Well-Known Member
Licensed User
Longtime User
I have so resolved to embed the chart graphic:

B4X:
      Dim bmd As BitmapDrawable
      bmd               = pnlchart.Background
      bitmap_grafik      = CreateScaledBitmap(bmd.Bitmap, 300, 300, True)
B4X:
      PDFWriter1.addImage2(xpos_txt,350,300,300,bitmap_grafik)                     'Grafik einfügen
B4X:
      Dim Obj1,obj2 As Reflector
      Obj1.Target = bitmap_grafik                                              'Bitmap vom Speicher freigeben
      Obj1.RunMethod("recycle")
 

enrico

Active Member
Licensed User
Longtime User
Table in a PDF

Can you please suggest me how to display a SQLite table in a PDF ?
 

Paulsche

Well-Known Member
Licensed User
Longtime User
Maybe helps you

B4X:
      scv04Tabelle.Panel.AddView(LBL04zaehlernr,xpos,ypos,85dip,34dip)
          If PDF_erstellen = True Then
            PDFWriter1.addText(PDFxpos, PDFypos, PDFfontsize, LBL04zaehlernr.Text)
            pdf_xpos_znr         = PDFxpos
            PDFxpos             = PDFxpos + 60
         End If   
         LBL04zaehlernr.Gravity      = Bit.Or(Gravity.RIGHT, Gravity.CENTER_VERTICAL)
         LBL04Hzaehlernr.Visible      = True
         LBL04Hzaehlernr.Left      = xpos
         xpos                  = LBL04zaehlernr.Left+LBL04zaehlernr.Width+1      ' xpos für nächste Spalte setzen
 

peternmb

Well-Known 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.
 

cnicolapc

Active Member
Licensed User
Longtime User
hi,
Where can I find the string encoding for Italy? , with "ISO-8859-1" currency symbol €. is displayed as "?" in the PDF file and number "200,34" is displayed as "200.34"
Where can I find documentation?
Nicola
 
Last edited:

Harris

Expert
Licensed User
Longtime User
Search for AHLocale library...

Format dates and numbers based on device language settings.
 

TheMightySwe

Active Member
Licensed User
Longtime User
Any news on special characters

Hi,

Is it the fonts that are missing the special characters in some languages?

I'm trying to make scandinavian docs and the letter ÅÄÖ and åäö are missing.

Anyone know any solution?

// TMS
 

wst

New Member
Licensed User
Longtime User
filename PDFWriter

Is it possible to use long filenames with PDFWriter? The max. I get is about 20 characters, I need about 40 in an app.
I got it.
 
Last edited:
Top