Android Question Make a pdf file with large text

The Printig library is a great library for making pdf ,but my text is more than a page how can I make my pdf.
If i make it with viewtobitmap it cant be selectable and searchable after i make it
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
I tried a lot but I couldn't.

I have a vague recollection from a few years ago that creating text-only PDFs wasn't actually that hard.

Can't find what I remember reading at the time, closest I've gotten (so far...) is this:

https://brendanzagaeski.appspot.com/0004.html :

Minimal PDF

PDF is a binary format, but it contains mostly plain text

Most PDF files do not look readable in a text editor. Compression, encryption, and embedded images are largely to blame. After removing these three components, one can more easily see that PDF is a human-readable document description language.

With patience, one can write a PDF file by hand

 
Upvote 0

emexes

Expert
Licensed User
A start, based on sample from:

https://stackoverflow.com/a/70748286

Is this headed in the right direction? Do you definitely need only text on your pdf? If you're going to be adding images etc later, then you'd be better off with an existing library rather than needlessly reimplementing akin to reinventing the wheel.
 

Attachments

  • textonly.pdf
    3.6 KB · Views: 64
Last edited:
Upvote 0

emexes

Expert
Licensed User
How can I make a pdf file with a large text file.

with a large text file

This isn't a one-off task, is it?

What's wrong with just leaving it as a text file? = an even-more universal format than PDF.

Or have you considered simpler formats like HTML, Markdown, even EPUB (good enough for the Library of Congress) ?
 
Upvote 0
I have this code. I want to make pages for the rest of my long string.how can i do that to make a pdf file.

B4X:
PDF_WrapText(EditText1.Text,30,50, 185,a, 30,-16777216,"LEFT")

Private Sub PDF_WrapText(Text As String, x As Float, y As Float, Length As Float, Font As Typeface,TextSize As Float, TextColor As Int, Alignment As String)
    Dim PDF As PdfDocument
    PDF.Initialize
    PDF.StartPage(595, 842) 'A4 size
    Dim y1 As Float = y
    Dim b As Int = 0
    For c = 1 To Text.Length
        If Text.SubString2(b, c).Length > Length / 4.5 Then
            PDF.Canvas.DrawText(Text.SubString2(b,Text.LastIndexOf2(" ", c)), x,y1,Font,TextSize/GetDeviceLayoutValues.Scale,TextColor, Alignment)
            b = Text.LastIndexOf2(" ", c) + 1
            y1 = y1 + TextSize
        End If
        If c = Text.Length And Text.SubString2(b,c).Length > 0 Then
            PDF.Canvas.DrawText(Text.SubString2(b,c), x,y1,Font,TextSize,TextColor, Alignment)
        End If
    Next
    PDF.FinishPage
    Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "1.pdf", False)
    PDF.WriteToStream(out)
    out.Close
    PDF.Close
    Dim i As Intent
    i.Initialize(i.ACTION_VIEW, "file://" & File.DirRootExternal & "/1.pdf")
    i.SetType("application/pdf")
    StartActivity(i)
End Sub
 
Upvote 0

emexes

Expert
Licensed User
I think you have to turn your page/line logic inside out, so...

Instead of this:
Start PDF file
Start PDF page
For each text line
    Add line to page
Next
Finish PDF page
Finish PDF file

Should look more like this:
Start PDF file
For each text line
    If not already in a PDF page then
        Start PDF page
    Else if won't fit on current PDF page then
        Finish PDF page
        Start next PDF page
    End if
 
    Add line to pdf
Next

If in a PDF page then
    Finish PDF page
end if
Finish PDF file

or if you you don't mind a blank empty PDF page being produced if you have no text lines, or if you *always* have text lines eg a report header or summary that says there ain't nuthin' to report, then:

Simpler, but always emits at least one page:
Start PDF file
Start PDF page

For each text line
    If won't fit on current PDF page then
        Finish PDF page
        Start next PDF page
    End if
 
    Add line to pdf
Next

Finish PDF page
Finish PDF file

What could possibly go wrong? šŸ™ƒ
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
my long string

What does this long string look like?

Is it also split into lines by ASCII CR or LF or CR+LFs aka Chr(13) or Chr(10) ?

Is it split into paragraphs, eg by blank lines or by ASCII FF aka Chr(12) ?

Do we need to worry about very long words that won't fit on a single line?

Do you want multiple spaces reduced to a single space? ie like this forum software does eg each of these numbers 1 2 3 4 5 is followed by that number of spaces
 
Upvote 0
thank you for replying. this is my text file for (long string), and how could I make a sub from this for use.
and what is the best (Length as Flout) length automatically so my word warp work fine.
 

Attachments

  • United State.txt
    8.7 KB · Views: 53
Last edited:
Upvote 0
Top