Is it possible to convert PDF File or XML file into binary

Is it possible to convert PDF File or XML file into String

I mean if is this possible like convert Pictures to string using EncodeBase64.
but instead pictures convert XML and PDF files to string, and then convert these strings to XML or PDF File.

Regards.

Yes.

What are you attempting to accomplish?

A string is a series of byte characters… a file is a series of byte characters, a pdf is a file, therefore a pdf is a string of byte characters

[quote=261712:@Dave S]What are you attempting to accomplish?

A string is a series of byte characters… a file is a series of byte characters, a pdf is a file, therefore a pdf is a string of byte characters[/quote]
I wanna Read a PDF file and store this as a String Value and then Reassembly or save this String value to PDF.

I stilll don’t understand.
if you open the file and read it using TextInputStream or BinaryStream, you can put it into a string
What “reassembly”… how are you “disassembling” it?

Or are you wanting to extract the TEXT from the PDF? this will depend on if the PDF is encrypted or not, contains images or not etc. etc. And if by “reassemble” you mean take TEXT and turn back into a PDF? that to takes some doing… I spent a year writing a class to do this, and there is DynaPDF (your’e welcome Christian), and other tools… but its NOT a simple thing.

dim f as folderitem
dim t as textinputstream
dim s as string
f=specialfolder.desktop.child("test.pdf")
t=textinputstream.open(f)
s=t.readall
t.close

this reads the contents of a PDF into a String… but its the CONTENTS, not “just the text”

[quote=261716:@Dave S]I stilll don’t understand.
if you open the file and read it using TextInputStream or BinaryStream, you can put it into a string
What “reassembly”… how are you “disassembling” it?

Or are you wanting to extract the TEXT from the PDF? this will depend on if the PDF is encrypted or not, contains images or not etc. etc. And if by “reassemble” you mean take TEXT and turn back into a PDF? that to takes some doing… I spent a year writing a class to do this, and there is DynaPDF (your’e welcome Christian), and other tools… but its NOT a simple thing.

dim f as folderitem
dim t as textinputstream
dim s as string
f=specialfolder.desktop.child("test.pdf")
t=textinputstream.open(f)
s=t.readall
t.close

this reads the contents of a PDF into a String or binaryStream… but its the CONTENTS, not “just the text”[/quote]
Hi Dave!, I want to save the PDF file like a string in a variable, for this Reason.

I don’t want to have it locally. And when I install my program do this:

Look if base.pdf exists, if is not, then take the String where are stored the PDF and then create it again and save it in the desired place.

I’ve done something similar but Converting Pictures to String and then Convert that String to Picture again using EncodeBase64

then use the code I posted above, and add an Encode64 to it. PDF files are NOT binary images, they are either clear text, encrypted (ie. Zip format) text, or a combination of both…

Do what Dave says. As he says, a file is just a pile of bytes. Personally I like binarystreams so just open the file as a binarystream, read it one-time all into a string, create a new binarystream somewhere else and write the string out. I see no need for Base64 unless you want to put the pile of bytes into a database as a blob or suchlike.

[quote=261721:@Gerardo García]Hi Dave!, I want to save the PDF file like a string in a variable, for this Reason.

I don’t want to have it locally. And when I install my program do this:

Look if base.pdf exists, if is not, then take the String where are stored the PDF and then create it again and save it in the desired place.

I’ve done something similar but Converting Pictures to String and then Convert that String to Picture again using EncodeBase64[/quote]

Would it not be easier to drag the pdf into the project, and when you need it, simply CopyFileTo it or move it to the place where you want it ?

dim thePath as Xojo.io.FolderItem = Xojo.io.SpecialFolder.GetResource("xmetro.pdf") dim f as FolderItem = GetFolderItem(thePath.Path, FolderItem.PathTypeNative) dim destination as FolderItem = SpecialFolder.UserHome.child("Downloads").child("xmetro.pdf") f.CopyFileTo(destination)

[quote=261775:@Michel Bujardet]Would it not be easier to drag the pdf into the project, and when you need it, simply CopyFileTo it or move it to the place where you want it ?

dim thePath as Xojo.io.FolderItem = Xojo.io.SpecialFolder.GetResource("xmetro.pdf") dim f as FolderItem = GetFolderItem(thePath.Path, FolderItem.PathTypeNative) dim destination as FolderItem = SpecialFolder.UserHome.child("Downloads").child("xmetro.pdf") f.CopyFileTo(destination) [/quote]
Another way to do it, thanks

Also consider using the CopyFile build step to copy your PDF file into the Resources folder that goes along with your app anyway. This way it doesn’t take up memory space in your runtime environment. You can then either use it directly or copy it to another place on your user’s disk.

@Dave S
This works great. How do you take the Encoded Base 64 string data and restore it to a folderitem?

Use decodebase64 before writing it back to the file.

Thanks. I know how to decode it but I don’t know the syntax to take the decoded string and write it back out to a file. :confused:

Nevermind I got it. BinaryStream to the rescue. I was looking for a method in FolderItem all this time. :slight_smile:

dim s as string = DecodeBase64(data) dim bs as BinaryStream dim f as Folderitem = GetFolderItem("05.mp3") bs = BinaryStream.Create(f,true) bs.Write(s) bs.Close