[C#] Converting Files to .PDF  SOLVED

PDF-XChange Editor SDK for Developers

Moderators: TrackerSupp-Daniel, Tracker Support, Paul - Tracker Supp, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, Tracker Supp-Stefan

Forum rules
DO NOT post your license/serial key, or your activation code - these forums, and all posts within, are public and we will be forced to immediately deactivate your license.

When experiencing some errors, use the IAUX_Inst::FormatHRESULT method to see their description and include it in your post along with the error code.
Post Reply
DeKo
User
Posts: 56
Joined: Thu Sep 28, 2017 9:45 am

[C#] Converting Files to .PDF

Post by DeKo »

Hello,

i'm trying to convert files to .pdf. My current approach is to open them with the PDF XChange Editor and then save them at the same location. So far so good. But when i try to do this in a thread or a task, i get a System.Runtime.InteropServices.COMException.

My Code:

Code: Select all

	    string[] files = Directory.GetFiles(, "C:\\Folder", SearchOption.AllDirectories);
            IPXV_Inst ipxv = pdfCtl.Inst;
            IAFS_Inst fsInst = (PDFXEdit.IAFS_Inst)pdfCtl.Inst.GetExtension("AFS"); //Getting IAFS_Inst from IPXV_Inst
            //PDFXEdit.IAFS_Name dest;
            int l = files.Length;
            IPXV_Document[] docs = new IPXV_Document[l];


            Thread task = new Thread(() => {
                pdfCtl.OpenDocFromPath(files[0]);
                IPXV_Document doc = pdfCtl.Doc;
                doc.Save(fsInst.DefaultFileSys.StringToName(GivePDFName(files[0])), (int)PXV_DocSaveFlags.PXV_DocSave_AllowUI);
            });
The GivePDFName-method returns the given path with .pdf. The pdfCtl is my AxPXV_Control.

Am i doing something wrong? Is my approach the wrong one? And also, it seems that i cant open all file-types in the pdf-xchange editor (xlsx, msg, eml aren't working)

Appreciate every help,
thank you
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: [C#] Converting Files to .PDF

Post by Sasha - Tracker Dev Team »

Hello DeKo,

Have you checked the FullDemo solution? There are plenty of samples there.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
DeKo
User
Posts: 56
Joined: Thu Sep 28, 2017 9:45 am

Re: [C#] Converting Files to .PDF

Post by DeKo »

Hello Alex,

thank you for replying so quick. Yes i checked the FullDemo and i've found something that can help me. So, the problem changed a bit: I still can't save in a Thread or a Task, seems like its impossible for PDFXEdit to Save 2 different files simultaneously. Correct me if i'm wrong.

Greetings
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: [C#] Converting Files to .PDF

Post by Sasha - Tracker Dev Team »

Hello DeKo,

You can't use the Control like that - one thread only.
You can use the IPXV_ImportConverter to convert from the different formats to the pdf (IPXC_Document). This can be done from multiple threads as this does not update the Control itself - only needs IPXV_Inst by the way. Here's the useful topic:
https://www.pdf-xchange.com/forum3 ... 66&t=26367
Also, check out this wiki page:
https://sdkhelp.pdf-xchange.com/view/PXV:CoClasses

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
DeKo
User
Posts: 56
Joined: Thu Sep 28, 2017 9:45 am

Re: [C#] Converting Files to .PDF

Post by DeKo »

Hello Alex,

thank you for the links. I didn't knew that there was a converter like this. I've checked the links and tried some things, but i dont quiet understand how exactly i can convert a given file. I've searched for an example, but with no success. I didnt see anything in the Full Demo either.

An example would help me. Thanks in advance.

Greetings,
Dennis
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: [C#] Converting Files to .PDF  SOLVED

Post by Sasha - Tracker Dev Team »

Hello Dennis,

Here's a sample with a tiff file:

Code: Select all

PDFXEdit.IPXV_Inst Inst = pdfCtl.Inst;
//IPXV_Inst Inst; //we have the IPXV_Inst
for (uint i = 0; i < Inst.ImportConvertersCount; i++)
{
	PDFXEdit.IPXV_ImportConverter iConv = Inst.ImportConverter[i];
	if (iConv.ID.Equals("conv.imp.image.tiff"))
	{
		PDFXEdit.IAFS_Inst fsInst = (PDFXEdit.IAFS_Inst)Inst.GetExtension("AFS");
		PDFXEdit.IAFS_Name name = fsInst.DefaultFileSys.StringToName(System.IO.Directory.GetParent(System.Environment.CurrentDirectory).Parent.FullName + "\\Resources\\multipage_tif_example.tif");
		int openFileFlags = (int)(PDFXEdit.AFS_OpenFileFlags.AFS_OpenFile_Read | PDFXEdit.AFS_OpenFileFlags.AFS_OpenFile_ShareRead);
		PDFXEdit.IAFS_File destFile = fsInst.DefaultFileSys.OpenFile(name, openFileFlags);
		PDFXEdit.IPXC_Document doc = iConv.Convert((PDFXEdit.PXV_Inst)Inst, destFile);
		pdfCtl.OpenDocFrom(doc);
		break;
	}
}
Here by having the IPXC_Document you can write it to file - simply as that.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
DeKo
User
Posts: 56
Joined: Thu Sep 28, 2017 9:45 am

Re: [C#] Converting Files to .PDF

Post by DeKo »

Hello Alex,

thank you for the example! It really helped me a lot. I've worked out an solution for my problem today thanks to your example. I even got it working without a control element (as i dont really need it) and in Tasks.
If anyone is interested or you, Alex, want me to show my example, i gladly share it.

PS: Sorry for not answering earlier, but i dont work on weekends :oops:

Thanks again for the help, Alex.

Greetings,
Dennis
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: [C#] Converting Files to .PDF

Post by Sasha - Tracker Dev Team »

Hello Dennis,

Glad you got it working :)

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
DeKo
User
Posts: 56
Joined: Thu Sep 28, 2017 9:45 am

Re: [C#] Converting Files to .PDF

Post by DeKo »

Hello,

its me again. I didn't want to create a new topic, so i just ask here. Ofc anyone is free to anwser, not only sasha :D

I wanted to know if you can convert .eml and .msg to .pdf using the Editor SDK. I've searched the forums for a while and i didnt find a way (programatically, that is).

Thanks in advance!

Greetings,
Dennis
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: [C#] Converting Files to .PDF

Post by Sasha - Tracker Dev Team »

Hello Dennis,

As far as I know, the End-User Editor does not support those types which basically means that the SDK won't support them too. You can write your own converter though - not necessarily to PDF but to the format that we can convert from.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
lidds
User
Posts: 510
Joined: Sat May 16, 2009 1:55 pm

Re: [C#] Converting Files to .PDF

Post by lidds »

Alex,

Sorry to jump on the back of someone else's post, however I am wanting to convert files to PDF as well. Your code was a great start however I noticed on this line of code you are checking to see if the file is a tiff file and using the conv.imp.image.tiff converter.

Code: Select all

Dim iConv As PDFXEdit.IPXV_ImportConverter = Inst.ImportConverter(i)
            If iConv.ID.Equals("conv.imp.image.tiff") Then
            	.....
What I wanted to do is write a common conversion class that will convert any of your approved file types to PDF without having to rely on the control to convert them when loaded. Therefore how would I modify your above code to find the correct conversion type to use?

Thanks

Simon
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: [C#] Converting Files to .PDF

Post by Sasha - Tracker Dev Team »

Hello Simon,

Loop through all of the available converters and make a list of them. Then make a converter-extension references - then you will have a table that would tell which converter is needed for each extension.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
DeKo
User
Posts: 56
Joined: Thu Sep 28, 2017 9:45 am

Re: [C#] Converting Files to .PDF

Post by DeKo »

Hello,

i'm back to the party! :D Jokes aside: I also needed a list of all avaiable converters. I looped through each converter, but then i hardcoded the indecies of the converters as an enum. When i saw your response, Alex, i kinda get the feeling that the avaible converters depend on something?

Could you clarify if it depends on something and if yes, depends on what?

Greetings,
Dennis
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: [C#] Converting Files to .PDF

Post by Sasha - Tracker Dev Team »

Hello Dennis,

They depend on the plugins that you have.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
DeKo
User
Posts: 56
Joined: Thu Sep 28, 2017 9:45 am

Re: [C#] Converting Files to .PDF

Post by DeKo »

Hello Alex,

thanks for the fast reply. So i can assume that the customers of our company have the same options avaiable as i have on my machine i develop on?

Greetings,
Dennis
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: [C#] Converting Files to .PDF

Post by Sasha - Tracker Dev Team »

Hello Dennis,

The plugins are the pvp files that come with the End-User Editor - of course your clients will be able to use those.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Post Reply