C# code to save pdf in pdf/a format without IPXV_Control

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
Nisha
User
Posts: 43
Joined: Tue May 22, 2018 9:31 am

C# code to save pdf in pdf/a format without IPXV_Control

Post by Nisha »

Hi Team,

I am developing a simple c# application to save a pdf file as pdf/a file using
only PDF-XChange Editor SDK and not using IPXV_Control. After refering few forums I got this code

Code: Select all

PXV_Inst pxvInst = new PXV_Inst();
            pxvInst.Init();
            pxvInst.StartLoadingPlugins();
            pxvInst.AddPluginFromFile(@"E:\PDFAPlugin.pvp");
            pxvInst.FinishLoadingPlugins();
            PDFXEdit.IAFS_Inst fsInst = pxvInst.GetExtension("AFS");
            PDFXEdit.IAFS_Name destPath = fsInst.DefaultFileSys.StringToName(@"E:\Destec_RCJ.pdf");
            int openFileFlags = (int)(PDFXEdit.AFS_OpenFileFlags.AFS_OpenFile_CreateAlways | PDFXEdit.AFS_OpenFileFlags.AFS_OpenFile_Read | PDFXEdit.AFS_OpenFileFlags.AFS_OpenFile_Write | PDFXEdit.AFS_OpenFileFlags.AFS_OpenFile_FullCache);
            PDFXEdit.IAFS_File destFile = fsInst.DefaultFileSys.OpenFile(destPath, openFileFlags);
            PDFXEdit.IPXV_ExportConverter cnv = null;
            IPXC_Inst docInst = (PXC_Inst)pxvInst.GetExtension("PXC");
            IPXC_Document srcDoc = docInst.OpenDocumentFromFile(@"E:\Destec_RCJ.pdf", null);

            for (uint i = 0; i < pxvInst.ExportConvertersCount; i++)
            {
                if (pxvInst.ExportConverter[i].ID == "conv.exp.pdfa")
                    cnv = pxvInst.ExportConverter[i];
            }
            if (cnv != null)
            {
                cnv.Convert(pxvInst, srcDoc, destFile);
            }
But I am facing an error while executing the code.
It throws a com exception on following line

Code: Select all

IPXC_Document srcDoc = docInst.OpenDocumentFromFile(@"E:\Destec_RCJ.pdf", null);
The exception states that
'Exception from HRESULT: 0x83FF0020'.
Please correct me where I am doing wrong.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Sasha - Tracker Dev Team »

Hello Nisha,

If you have used the IAUX_Inst::FormatHRESULT method like the Forum Rules header tells, you would have found that that message means:
Error [Operating system]: The process cannot access the file because it is being used by another process.

You opened a file so that you can convert into it and then you tried to open the document from the same file that is already opened (and judging by your flags it is always created as a new file).

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Nisha
User
Posts: 43
Joined: Tue May 22, 2018 9:31 am

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Nisha »

Hi Alex,
Sorry, but can you please explain me in terms of code.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Sasha - Tracker Dev Team »

Hello Nisha,

To simplify and sum up - your source document and resulting document are the same - change that.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Nisha
User
Posts: 43
Joined: Tue May 22, 2018 9:31 am

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Nisha »

Hi Alex,

And what did you meant by this comment

"and judging by your flags it is always created as a new file"

Do I need to do anything about this, apart from changing the source and resultant document.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Sasha - Tracker Dev Team »

Hello Nisha,

Well the steps should be:
1) You find the needed converter.
2) You open/create the file in which you want to convert your file.
3) You specify the source document that differs from the resulting file (unlike your sample).
4) You pass that all to the Convert() method.
Code it like that (not just by copying the sample but understanding what you are doing exactly) and it all should work.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Nisha
User
Posts: 43
Joined: Tue May 22, 2018 9:31 am

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Nisha »

Sorry Alex but can you please explain the first point
1) You find the needed converter.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Sasha - Tracker Dev Team »

Code: Select all

for (uint i = 0; i < pxvInst.ExportConvertersCount; i++)
            {
                if (pxvInst.ExportConverter[i].ID == "conv.exp.pdfa")
                    cnv = pxvInst.ExportConverter[i];
            }
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Nisha
User
Posts: 43
Joined: Tue May 22, 2018 9:31 am

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Nisha »

Hi Alex,

Thankyou for the steps the error got resolved.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Sasha - Tracker Dev Team »

:)
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Nisha
User
Posts: 43
Joined: Tue May 22, 2018 9:31 am

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Nisha »

Hi Alex,

Can you please help me with the code to hide conversion report. Below is my code

PXV_Inst pxvInst = new PXV_Inst();
pxvInst.Init();
pxvInst.StartLoadingPlugins();
pxvInst.AddPluginFromFile(@"E:\PDFAPlugin.pvp");
pxvInst.FinishLoadingPlugins();

PDFXEdit.IPXV_ExportConverter cnv = null;
for (uint i = 0; i < pxvInst.ExportConvertersCount; i++)
{
if (pxvInst.ExportConverter.ID == "conv.exp.pdfa")
cnv = pxvInst.ExportConverter;
}

PDFXEdit.ICab cab = pxvInst.GetFormatConverterParams(false, "conv.exp.pdfa");
PDFXEdit.ICabNode convParamsRoot = cab.Root;
convParamsRoot.SetInt("ShowReport", 0);

PDFXEdit.IAFS_Inst fsInst = pxvInst.GetExtension("AFS");
PDFXEdit.IAFS_Name destPath = fsInst.DefaultFileSys.StringToName(@"E:\3_a.pdf");
int openFileFlags = (int)(PDFXEdit.AFS_OpenFileFlags.AFS_OpenFile_CreateAlways | PDFXEdit.AFS_OpenFileFlags.AFS_OpenFile_Read | PDFXEdit.AFS_OpenFileFlags.AFS_OpenFile_Write | PDFXEdit.AFS_OpenFileFlags.AFS_OpenFile_FullCache);
PDFXEdit.IAFS_File destFile = fsInst.DefaultFileSys.OpenFile(destPath, openFileFlags);


IPXC_Inst docInst = (PXC_Inst)pxvInst.GetExtension("PXC");
IPXC_Document srcDoc = docInst.OpenDocumentFromFile(@"E:\3.pdf", null);


if (cnv != null)
{
cnv.Convert(pxvInst, srcDoc, destFile);
}
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Sasha - Tracker Dev Team »

Hello Nisha,

Well you have created the conversion parameters and did not use them in the Convert method - thus the result.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Nisha
User
Posts: 43
Joined: Tue May 22, 2018 9:31 am

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Nisha »

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

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Sasha - Tracker Dev Team »

:)
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Nisha
User
Posts: 43
Joined: Tue May 22, 2018 9:31 am

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Nisha »

Hi Alex,
Is there any code snippet in C# to detect if a pdf file is PDFA.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Sasha - Tracker Dev Team »

Hello Nisha,

There are two methods CheckFormat and CheckFromat2 that can check whether the file is of the needed type (in your case PDF\A):
https://sdkhelp.pdf-xchange.com/vi ... tConverter

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Nisha
User
Posts: 43
Joined: Tue May 22, 2018 9:31 am

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Nisha »

Hi Alex,

I need your help in the following code:

PXV_Inst pxvInst = new PXV_Inst();
pxvInst.Init();
pxvInst.StartLoadingPlugins();
pxvInst.AddPluginFromFile(@"E:\PDFAPlugin.pvp");
pxvInst.FinishLoadingPlugins();

IPXC_Inst docInst = (PXC_Inst)pxvInst.GetExtension("PXC");
IPXC_Document srcDoc = docInst.OpenDocumentFromFile(@"E:\Serrala\Task\DCC185\PDFATest\3.pdf", null);

PDFXEdit.IPXV_ImportConverter importConverter = null;
PXV_FmtCheckResult ckhResult = importConverter.CheckFormat(pxvInst, srcDoc);


I am receiving following error:
"Object reference not set to instance of an object"

in the following line:
PXV_FmtCheckResult ckhResult = importConverter.CheckFormat(pxvInst, srcDoc);

I know the error has occured because of the following line:
PDFXEdit.IPXV_ImportConverter importConverter = null;

But I am not able to code the required thing. Hence can you please help.
Please note that I have recently started working with this library and I am yet to get familiar with all the functionalities. Hence I am seeking much of your help.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Sasha - Tracker Dev Team »

Hello Nisha,

You should do like you have done here, only go through the import converters and search for the needed one:
viewtopic.php?p=127383#p127202

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Nisha
User
Posts: 43
Joined: Tue May 22, 2018 9:31 am

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Nisha »

Hi Alex,
Now I am receiving following error :

'Value does not fall within the expected range.' in the following line:
PXV_FmtCheckResult ckhResult = importConverter.CheckFormat(pxvInst, srcDoc);

I am trying to detect a pdfa file. But in import convertor ID list I could not find any ID related to pdfa, hence I selected conv.imp.pdf. Please correct me.

Below is my code:

PDFXEdit.IPXV_ImportConverter importConverter = null;
for (uint i = 0; i < pxvInst.ImportConvertersCount; i++)
{
string h = pxvInst.ImportConverter.ID;
if (pxvInst.ImportConverter.ID == "conv.imp.pdf")
{
importConverter = pxvInst.ImportConverter;
}
}


IPXC_Inst docInst = (PXC_Inst)pxvInst.GetExtension("PXC");
IPXC_Document srcDoc = docInst.OpenDocumentFromFile(@"E:\Serrala\Task\DCC185\PDFATest\3.pdf", null);

if (importConverter != null)
{
PXV_FmtCheckResult ckhResult = importConverter.CheckFormat(pxvInst, srcDoc);
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Sasha - Tracker Dev Team »

Hello Nisha,

I've mislead you a little. What you need to do is obtain the PXC_PDFStandard from the IPXC_DocumentProps that can be obtained from here:
https://sdkhelp.pdf-xchange.com/vi ... ment_Props
You won't need any converter for this - only the IPXC_Document.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Nisha
User
Posts: 43
Joined: Tue May 22, 2018 9:31 am

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Nisha »

Hi Alex,

Sorry for delayed reply. Thankyou :)
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: C# code to save pdf in pdf/a format without IPXV_Control

Post by Sasha - Tracker Dev Team »

:)
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Post Reply