Word doc to pdf

Greetings,

I am building a program,where one of the features would be, to have the user to be able to generate a certificate/diploma in pdf format.

The user should be able to input name and date and then be able to press a “generate” button.

I already have the certificate ready in Word format, so hopefully I can just build on that, instead of having to set all up with code inside Xojo.

How could I make this? :slight_smile:

Thanks in advance!

Kind regards

Ole

Print to PDF in Word and import in Xojo?

Assuming Windows:
Can we assume Word is installed on the machine???

If so, one technique is to save the document template as RTF.
Replace the name and the date in the template by easily found tokens such as <<>> and <<>>

Then open the template rtf as a text file, replace the tokens, save it back as a new rtf file.
Use automation to open Word, have it open the rtf file, have it save as pdf, kill word.

If Word is not present, you might be better using one of the PDF classes/plugins to generate a PDF from scratch, or amend an existing ‘template’

I already have the certificate ready in Word format

If this is “only text” (or simple image), I would code all the stuff inside Xojo and print it to pdf from Xojo, all by myself. But I understand you prefer a different version.

I use Print to PDF in OS X and Windows (thru PDFCreator).
I’ve made a simple report from data stored in SQLite (one splash page with the company logo and text and many pages with header / footer and db Rows as line in-between).
[No User Input in the process).

If you created the certificate with Xojo in Word, you can either directly save the certificate as a pdf document with Xojo in Word or you can print the certificate with Xojo in Word.

Sincerely,

Eugene

Thanks for all the answers! Much appreciated! :slight_smile:

I do not assume, that the user has Word.

Either I think I would code it all from scratch - or perhaps better, use a PDF plugin.

This is basically what I want the finished program to do:

  • The user opens a program.
  • It opens a window with 1 text field and 1 pushbutton.
  • The user write his/her name in the textfield and press the button.
  • A PDF is generated with the users name on top of an image (eventually the diploma).

Does anyone know, step by step, how that could be done?

If I know how to do this, then I can do it :slight_smile:

Regards

Ole

In theory you could

  • Generate an UNCOMPRESSED PDF with a marker where you want the name to go
    PDF’s are basically text when they’re not compressed - just not text anyone would want to read
  • Open the PDF into a binary blob (like a memory block)
  • Replace the marker with the name
  • Save & Print the modified PDF

However I doubt its ever quite so simple

You could do PDF editing with our MBS DynaPDF Plugin.
It can generate PDF without compressed stream and of course DynaPDF Pro can simply do text search & replace in a PDF page.

Hi,

I take sometimes yesterday evening to make something.

It is just “something” you can get ideas from, a RAW something.

I wanted to draw in half of a circle a title (the certificate of achievement…), but do not read an example on my hard disks, so I used Object2D to draw stuff (as a ‘graphic’ template).

Here’s and idea of what you can get:


The drawing in done in three parts: red, blue and green in a loop, the certificate title in a different one.

The main (and only) window have a button to create the PDF * and two fields to get the first / family names.
A simple (error) report is done in a label below the two fields (try to push the button with no name(s)…

  • The pdf is generated by OS X itself / PDFCreator on Windows (XP).

Nothing fancy, just to get ideas, so adapt it or better write your own once you read the code. You do not have to use Object2D to print to pdf, you can use the graphics class to do that.

Here’s the Word Doc to PDF file link.

BTW: You may want to place another TextArea for the mid-name (“A.”)… Or use only one TextArea to get the whole (“Emile A. Schwarz”) name.

You may place the field validation (empty or not) in LostFocus (at the moment the focus goes elsewhere)…

Norman, Christian, Emile and others, many thanks for the ideas! Again much appreciated :slight_smile:

I will try out the different ideas and see what I end up with :slight_smile:

Ole

Emile,

If I wanted to use your Word-to-Doc sample project, which you kindly posted above, with a little change: That it would be an image that the text is written on and not generated graphic. An image like this:

http://olegabrielsen.com/temp/emptycert.jpg

How would I be able to adjust your sample project, so it would print the text on top of the image?

Thanks!

Kind regards

Ole

Hi Ole,

I used graphics commands to not waste time to create an image.

To load an image and draw it, check the docs:

Graphics
DrawPicture,
DrawString

and so on…

something like:
drop your nice image into the Xojo project
and use something like:
g.DrawPicture(emptycert,0,0)

to draw it at position X,Y: 0,0.

Thank you, Emile :slight_smile:

Ole

You may put your graphic certificate ‘template’ in a folder beside your application with some other designs for future uses.

So, the application’s user may be able to choose to load one certificate background from the ones he will find in that folder.

Thanks for the suggestion :slight_smile:

[quote=59434:@Eugene Dakin]If you created the certificate with Xojo in Word, you can either directly save the certificate as a pdf document with Xojo in Word or you can print the certificate with Xojo in Word.

Sincerely,

Eugene[/quote]
Eugene, could you could elaborate on how to do this. I am using this for saving as html word.ActiveDocument.saveas(c,office.wdFormatHTML) but there is no office.wdFormatPDF or such and neither can I find how to access the Export functions of Office 2013 (the version of Word I am using.) Your help would be greatly appreciated.

Damien,

there exists virtual printers that print your data into a pdf file (Windows, not needed in OS X because the funtionality is implemented in the OS).

Hello Damien,

The following examples worked on my machine with Windows 8.1, Microsoft Office 2013 Word, and Xojo 2014 r2.1. The file folder “C:\Test” was created before the program was executed.

Here is an example of how to save the Microsoft Word 2013 file as a PDF:

[code] Dim word as new WordApplication
word.Documents.Add
word.Visible = True

word.Selection.TypeText “This is saved as a PDF file from Word 2013. Lorem ipsum dolor sit amet.”
word.ActiveDocument.ExportAsFixedFormat(“C:\Test\TestFile.pdf”,17)

Exception err as NilObjectException
MsgBox Err.Message[/code]

…And here is an example of how to save a Microsoft Word 2013 file as a htm:

[code] Dim word as new WordApplication
word.Documents.Add
word.Visible = True

word.Selection.TypeText “This is saved as a HTM file from Word 2013. Lorem ipsum dolor sit amet.”

word.ActiveDocument.SaveAs2(“C:\test\HTMLDoc.htm”, 8)

Exception err as NilObjectException
MsgBox Err.Message[/code]

Does this work on your machine?

Caution should be observed when altering the content of an UNCOMPRESS PDF file as suggested above. In many cases it will be “ok”, but in some (depending on the exact format, and the reader involved), it may be detected as a corrupted file.

The PDF format has tons of pointers the link all the sections together, and altering the text could create an offset that the reader will be unable to compensate for. This is most especially true of the XREF table

[quote=127214:@Eugene Dakin]Hello Damien,

The following examples worked on my machine with Windows 8.1, Microsoft Office 2013 Word, and Xojo 2014 r2.1. The file folder “C:\Test” was created before the program was executed.

Here is an example of how to save the Microsoft Word 2013 file as a PDF:

[code] Dim word as new WordApplication
word.Documents.Add
word.Visible = True

word.Selection.TypeText “This is saved as a PDF file from Word 2013. Lorem ipsum dolor sit amet.”
word.ActiveDocument.ExportAsFixedFormat(“C:\Test\TestFile.pdf”,17)

Exception err as NilObjectException
MsgBox Err.Message[/code]

…And here is an example of how to save a Microsoft Word 2013 file as a htm:

[code] Dim word as new WordApplication
word.Documents.Add
word.Visible = True

word.Selection.TypeText “This is saved as a HTM file from Word 2013. Lorem ipsum dolor sit amet.”

word.ActiveDocument.SaveAs2(“C:\test\HTMLDoc.htm”, 8)

Exception err as NilObjectException
MsgBox Err.Message[/code]

Does this work on your machine?[/quote]
Hi Eugene

I discovered the trick of using the number 17 instead of trying to use the enumeration for pdf format. Saving as html works using office.wdFormatHTML. or you can use the value 8 as you have it.

What I am expecting is that options such as “SaveAs2” or “ExportasFixedFormat” will appear in the popup that is available as you are typing in the statement. This popup list does not appear to be complete. For example I only get Save or SaveAs offered in the popup. When I type in word.ActiveDocument.ExportAsFixedFormat(c,17) where c is my folderitem this does work but I get no help from Xojo in selecting the ExportAsFixedFormat method.

I’m using Win 7, MS Office 2013 Word and Xojo 2013 r2. Thanks for your help.