1. Dennis Little
  2. PowerBuilder
  3. Wednesday, 30 August 2023 16:11 PM UTC

Hello all,

We are running PB2022 V1878. I need a way to convert word docs, excel files, etc to pdf programmatically. We are currently using Win2PDF as a printer, but I can't seem to find a way to convert.

We've just started using pdfdocument to pdf datawindows, datastores and append other pdfs, works pretty well.

Anyone have any ideas?

 

Thanks,

Dennis L

Tobias Roth Accepted Answer Pending Moderation
  1. Wednesday, 30 August 2023 20:34 PM UTC
  2. PowerBuilder
  3. # 1

Hi Dennis,

if Office is installed the conversion can be done as follows.

Basically, the script opens the file with the corresponding Office product and saves it as a PDF.

Works very well for us in practice, the good thing is, the resulting PDF is always created correctly, because, for example, Word interprets the formatting and saves the content as a PDF.

We have explicitly not hidden word and co in the automation, because otherwise the process would hang and the user could not react to queries from Word, e.g. specify intials.

I hope i could help you.

String	ls_filetype
long	ll_ret

choose case lower(ls_filetype)
	case "doc","docx","dot","dotx","rtf","odt"
		try
			oleobject	ole_doc
			ole_doc = create oleobject
			ll_ret = ole_doc.ConnectToNewObject("Word.Application")
			if ll_ret  <> 0 then
				MessageBox("Error", "Could not create Word object (" + string(ll_Ret) + ")!~nIs Microsoft Word installed?", Exclamation!)
				destroy ole_doc
				return false
			end if
					
			// PDF speichern
			ole_doc.Visible = true;
			ole_doc.Documents.Open(as_sourcefile) 
			ole_doc.ActiveDocument.saveas(as_filename,17)
			ole_doc.ActiveDocument.Close(false)
			ole_doc.Options.SaveNormalPrompt = false
			ole_doc.Options.SavePropertiesPrompt = false
			ole_doc.NormalTemplate.Saved = true
			ole_doc.Quit()
			ole_doc.DisconnectObject()
			
			return true
		catch ( NullObjectError noe1 )
			Messagebox("Error","An error occurred during communication with Word!~n NOE: "+noe1.GetMessage())
			return false
		catch ( PBXRuntimeError pbxre1 )
			Messagebox("Error","An error occurred while communicating with Word!~n PBXRE: "+pbxre1.GetMessage())
			return false
		catch ( OleRunTimeError ort1)
			Messagebox("Error","An error occurred during communication with Word!~n PBXRE: "+ort1.GetMessage())
			return false
		catch ( RunTimeError rtn1)   
			Messagebox("Error","An error occurred during communication with Word!~n PBXRE: "+rtn1.GetMessage())
			return false
		catch ( CorbaUserException cue1)
			Messagebox("Error","An error occurred during communication with Word!~n PBXRE: "+cue1.GetMessage())
			return false
		catch ( Throwable oe1 )
			Messagebox("Error","An error occurred while communicating with Word!~n OE: "+oe1.GetMessage())
			return false
		end try
		
	case "xls","xlsx"
		try
			oleobject	ole_excel
			ole_excel = create oleobject
			ll_ret = ole_excel.ConnectToNewObject("Excel.Application")
			if ll_ret  <> 0 then
				MessageBox("Error", "Could not create Excel object (" + string(ll_Ret) + ")!~nIs Microsoft Excel installed?", Exclamation!)
				destroy ole_excel
				return false
			end if
			ole_excel.Visible = true;
			
			// PDF speichern
			ole_excel.WorkBooks.Open(as_sourcefile)
			oleobject lole_worksheet,lole_workbook
			lole_workbook = ole_excel.application.workbooks(1)
			lole_worksheet = lole_workbook.worksheets(1)
			lole_worksheet.SaveAs(as_filename, 57);
			ole_excel.Quit()
			ole_excel.DisconnectObject()
			
			return true
			
		catch ( NullObjectError noe2 )
			Messagebox("Error","An error occurred while communicating with Excel!~n NOE: "+noe2.GetMessage())
			return false
		catch ( PBXRuntimeError pbxre2 )
			Messagebox("Error","An error occurred while communicating with Excel!~n PBXRE: "+pbxre2.GetMessage())
			return false
		catch ( OleRunTimeError ort2)
			Messagebox("Error","An error occurred while communicating with Excel!~n PBXRE: "+ort2.GetMessage())
			return false
		catch ( RunTimeError rtn2)   
			Messagebox("Error","An error occurred while communicating with Excel!~n PBXRE: "+rtn2.GetMessage())
			return false
		catch ( CorbaUserException cue2)
			Messagebox("Error","An error occurred while communicating with Excel!~n PBXRE: "+cue2.GetMessage())
			return false
		catch ( Throwable oe2 )
			Messagebox("Error","An error occurred while communicating with Excel!~n OE: "+oe2.GetMessage())
			return false
		end try		
		
	case "ppt","pptx"
		try
			oleobject	ole_powerpoint
			ole_powerpoint = create oleobject
			ll_ret = ole_powerpoint.ConnectToNewObject("powerpoint.Application")
			if ll_ret  <> 0 then
				MessageBox("Error", "Could not create PowerPoint object (" + string(ll_Ret) + ")!~nIs Microsoft PowerPoint installed?", Exclamation!)
				destroy ole_powerpoint
				return false
			end if
			ole_powerpoint.Visible = true;
			
			// PDF speichern
			ole_powerpoint.Presentations.Open(as_sourcefile)	
			ole_powerpoint.ActivePresentation.saveas(as_filename,32)
			ole_powerpoint.Quit()
			ole_powerpoint.DisconnectObject()
			return true
		catch ( NullObjectError noe3 )
			Messagebox("Error","An error occurred while communicating with PowerPoint!~n NOE: "+noe3.GetMessage())
			return false
		catch ( PBXRuntimeError pbxre3 )
			Messagebox("Error","An error occurred while communicating with Powerpoint!~n PBXRE: "+pbxre3.GetMessage())
			return false
		catch ( OleRunTimeError ort3)
			Messagebox("Error","An error occurred while communicating with PowerPoint!~n PBXRE: "+ort3.GetMessage())
			return false
		catch ( RunTimeError rtn3)   
			Messagebox("Error","An error occurred while communicating with PowerPoint!~n PBXRE: "+rtn3.GetMessage())
			return false
		catch ( CorbaUserException cue3)
			Messagebox("Error","An error occurred while communicating with Powerpoint!~n PBXRE: "+cue3.GetMessage())
			return false
		catch ( Throwable oe3 )
			Messagebox("Error","An error occurred while communicating with PowerPoint!~n OE: "+oe3.GetMessage())
			return false
		end try		
	case else
		Messagebox("Error","File type "+ls_filetype+" not supported".)	
		Return False			
end choose

Return true

Regards Tobi

 

 
Comment
  1. Chris Pollach @Appeon
  2. Thursday, 31 August 2023 00:04 AM UTC
Thanks for that OLE code Tobias! :-)
  1. Helpful
  1. Dennis Little
  2. Thursday, 31 August 2023 13:32 PM UTC
Tobias, thanks for the nice example. I will be giving it a try. I'd like to try it without opening Word, so that may be the wrinkle I'll need to solve.
  1. Helpful
  1. Oliver Nieper
  2. Friday, 10 November 2023 19:16 PM UTC
Thanks for the example, i was missing the 17 in ole_doc.ActiveDocument.saveas to get it running
  1. Helpful
There are no comments made yet.
Peter Pang @Appeon Accepted Answer Pending Moderation
  1. Thursday, 31 August 2023 03:46 AM UTC
  2. PowerBuilder
  3. # 2

Hi Dennis,

 You can use the RichTextEdit control to implement this. The code is as follows(PB2022 R2 2819):

rte_1.InsertDocument("test.docx",true,FileTypeDoc!)
rte_1.SaveDocumentAsPDF("testa.pdf")

rte_1.clearall( )
rte_1.InsertDocument("test.xlsx",true,FileTypeXLSX!)
rte_1.SaveDocumentAsPDF("testb.pdf")

 

Best Regards,

Peter

Comment
  1. Dennis Little
  2. Thursday, 31 August 2023 13:37 PM UTC
Peter, thanks for the input. The RTE control looks like a pretty straight forward solution, but we're doing this in a non-visual. We may try it in a window or userobject in the future.
  1. Helpful
  1. Miguel Leeuwe
  2. Thursday, 31 August 2023 15:54 PM UTC
From you non visual, you could open up a window that's not visible and has the RTE control.
  1. Helpful
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Wednesday, 30 August 2023 17:05 PM UTC
  2. PowerBuilder
  3. # 3

Hi Dennis;

   Have you thought about using OLE over to MS-Office to ask Word / Excel to perform a SaveAs (PDF!) for your App?

Regards ... Chris

Comment
  1. Dennis Little
  2. Thursday, 31 August 2023 13:27 PM UTC
I hadn't thought about that, I see Tobias added a nice example.
  1. Helpful
There are no comments made yet.
Armeen Mazda @Appeon Accepted Answer Pending Moderation
  1. Wednesday, 30 August 2023 17:24 PM UTC
  2. PowerBuilder
  3. # 4

A PDF printer is not going to do a true conversion... it just takes the output that would go on printed paper and direct to PDF format.  For a true conversion of Office files, you would need to integrate third-party PDF software that supports such conversion.  Some are available as a library you can package with your app and others are cloud-based service you can call from your app as REST APIs.  Here is an example of a library: https://developers.foxit.com/full-feature-list/ Here is an example of REST API service: https://developer.ilovepdf.com/

 

Comment
  1. Dennis Little
  2. Thursday, 31 August 2023 13:30 PM UTC
Thanks Armeen, we really just need the visual of the doc, the users print them, manually add their data and send the hard copies back to the customer.
  1. Helpful
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Thursday, 31 August 2023 20:23 PM UTC
  2. PowerBuilder
  3. # 5

Hi Peter;

  It would be interesting to get some feedback on how well the RTE control renders the DOC & XLSX file data into a PDF!

Regards ... Chris

Comment
There are no comments made yet.
Dennis Little Accepted Answer Pending Moderation
  1. Thursday, 18 January 2024 18:55 PM UTC
  2. PowerBuilder
  3. # 6

Hello all, thanks again for all the ideas. I'm going to go with the OLE solution using Tobias' example. I ran into a hiccup when using the RTE solution suggested by Peter. It seems like it truncates some of the info in the word document. The original document had a title of some sort, maybe an image, but it did not populate in the RTE saveas pdf.

 

Thanks again,

Dennis

Comment
  1. Miguel Leeuwe
  2. Thursday, 18 January 2024 21:25 PM UTC
Thanks for sharing that info!

If I understood well what Tobias said, the Word app has to be visible to do the save. If you don't want to show that moment where Word is visible, you can set it's Y or X position to a big value, using the Move() command, so it won't be visible on your monitor.



lole_word = CREATE OLEObject

lole_word.ConnectToNewObject("word.application")

// v4, mjl, 18/08/21: make sure windowstate is normal and not maximized if not it blows up on the move():

// to be able to manipulate the windowstate you first have to Activate:

lole_word.Application.Visible = True

lole_word.Application.Activate

// store the windowstate used by a user the last time a word doc was opened in windows:

li_restoreWindowState = lole_word.Application.WindowState

// make sure the window is in a normal state, if not the move() will blow up:

lole_word.Application.WindowState = 0 // 0=wdWindowStateNormal, 1=wdWindowStateMaximized, 2=wdWindowStateMinimize



// v4, mjl, 30/07/21: less flickering and avoid problems of Paste() not working first time

lole_word.Application.Move(1, 10000) // move all the way down so not visible
  1. Helpful
  1. Miguel Leeuwe
  2. Thursday, 18 January 2024 21:27 PM UTC
Not sure if we could do the Move() BEFORE doing the Visible = True, which would probably give a result with no flickering? (have to try that out).
  1. Helpful
  1. Peter Pang @Appeon
  2. Monday, 22 January 2024 09:04 AM UTC
Hi Dennis,

Can you provide a small case or Word file that can reproduce the problem to facilitate our analysis and debugging?

Best Regards,

Peter
  1. Helpful
There are no comments made yet.
Zhang JX Accepted Answer Pending Moderation
  1. Tuesday, 17 October 2023 03:19 AM UTC
  2. PowerBuilder
  3. # 7

Hi All,

Is there any has an example to convert pdf to jpg image file? (or extract image from PDF)

Regards, 

JX

Comment
  1. Andreas Mykonios
  2. Tuesday, 17 October 2023 10:01 AM UTC
Hi.

This should be a new question as it isn't related to the original question.

Andreas.
  1. Helpful 1
  1. Zhang JX
  2. Tuesday, 17 October 2023 10:05 AM UTC
agree... will open a new question.

Thanks, JX
  1. Helpful
There are no comments made yet.
Dennis Little Accepted Answer Pending Moderation
  1. Monday, 22 January 2024 14:33 PM UTC
  2. PowerBuilder
  3. # 8

Peter,
I tried to add the sample doc, but the forum doesn't accept .doc as attachments. I've enclosed a snip of the doc open in Word. It should be pretty easy to duplicate. It looks like there is some header/footer text and some word art pasted in at the top. It is the header/footer and word art that is missing from the RTE conversion.
Thanks
Dennis

Attachments (2)
Comment
  1. Armeen Mazda @Appeon
  2. Monday, 22 January 2024 17:25 PM UTC
Did you try .zip the .doc file?
  1. Helpful
There are no comments made yet.
Dennis Little Accepted Answer Pending Moderation
  1. Monday, 22 January 2024 18:23 PM UTC
  2. PowerBuilder
  3. # 9

Armeen,

thanks for the suggestion.

 

I've attached the zipped doc file.

Dennis

Attachments (1)
Comment
  1. Peter Pang @Appeon
  2. Tuesday, 23 January 2024 01:54 AM UTC
Hi Dennis,



We reproduced it on our end and will do further research to figure it out. We will keep you posted about the progress.



Best Regards,

Peter
  1. Helpful
There are no comments made yet.
Dennis Little Accepted Answer Pending Moderation
  1. Monday, 12 August 2024 18:20 PM UTC
  2. PowerBuilder
  3. # 10

Tobias, I am using your OLE example and have not had any issues. Is there a way to pass in arguments?

 

Thanks,

Dennis

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.