Error when used on threading

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.
chavas
User
Posts: 141
Joined: Tue Mar 15, 2016 12:21 pm

Error when used on threading

Post by chavas »

Hi,

I am working on using PDFXedit for optimizing PDFs. Here is the code that I have written-
private sub button_click(sender As Object, e As EventArgs) Handles Button1.Click
Dim doc As PDFXEdit.IPXC_Document
doc = m_pxcInst.OpenDocumentFromFile(FileName, Nothing)
OptimizeDocument_1(doc)
doc.Close()
end sub

Private Sub OptimizeDocument_1(ByVal doc As PDFXEdit.IPXC_Document)
Try
Dim nID As Integer = m_Inst.Str2ID("op.document.optimize", False)
dim outputpath as string= filepath & "_Optimized,pdf"

Dim op As PDFXEdit.IOperation = m_Inst.CreateOp(nID)

Dim input As PDFXEdit.ICabNode = op.Params.Root("Input")
Dim fsInst As PDFXEdit.IAFS_Inst = CType(m_Inst.GetExtension("AFS"), PDFXEdit.IAFS_Inst)
Dim impPath As PDFXEdit.IAFS_Name = fsInst.DefaultFileSys.StringToName(outputpath)
doc.WriteTo(impPath)
Dim pxcInst As PDFXEdit.IPXC_Inst = CType(m_Inst.GetExtension("PXC"), PDFXEdit.IPXC_Inst)
Dim resDoc As PDFXEdit.IPXC_Document = pxcInst.OpenDocumentFrom(impPath, Nothing)
input.Add().v = resDoc
Dim options As PDFXEdit.ICabNode = op.Params.Root("Options")
Dim images As PDFXEdit.ICabNode = options("Images")
images("Enabled").v = True
images("ReducedOnly").v = True
Dim comp As PDFXEdit.ICabNode = images("Comp")
comp("Color.JPEGQuality").v = 3
comp("Grayscale.JPEGQuality").v = 0
op.Do()

resDoc.WriteTo(impPath)
resDoc.Close()

MsgBox("Done..File saved as:- '" & outputpath & "'")

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

This works fine on click of a button. But throws exception when run in a thread(STA thread). Here is the exception that I get when I call
doc = m_pxcInst.OpenDocumentFromFile(data.sFolder, Nothing)
The error is-
Bad variable type. (Exception from HRESULT: 0x80020008 (DISP_E_BADVARTYPE))
I have called InitPDFControl() in the 'New()' function of the form...Working without keeping an activex control on the form...using interface calls...

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

Re: Error when used on threading

Post by Sasha - Tracker Dev Team »

Hello Charu,

Here's a code snippet that I have that illustrates working with multiple threads:

Code: Select all

public struct stData
{
	public string sFolder;
	public PDFXEdit.IPXC_Document Doc;
}

private void multiThreadedExportToImagesToolStripMenuItem_Click(object sender, EventArgs e)
{
	Thread th = new Thread(ExtractDocToTiff);
	th.SetApartmentState(ApartmentState.MTA);
	stData data = new stData();
	data.sFolder = "C:\\Users\\polar_000\\Documents\\TiffExtract";
	data.Doc = pdfCtl.Doc.CoreDoc;
	th.Start(data);

}

public void ExtractDocToTiff(object obj)
{
	try
	{
		stData data = (stData)obj;
		int nID = pdfCtl.Inst.Str2ID("op.document.exportToImages", false);
		PDFXEdit.IOperation Op = pdfCtl.Inst.CreateOp(nID);
		PDFXEdit.ICabNode input = Op.Params.Root["Input"];
		input.Add().v = data.Doc;
		PDFXEdit.ICabNode options = Op.Params.Root["Options"];
		options["PagesRange.Type"].v = "All";
		options["DestFolder"].v = data.sFolder; //Output folder
		options["ExportMode"].v = "AllToMutliPage";
		options["Zoom"].v = 150;
		//Saving as tiff
		PDFXEdit.ICabNode fmtParams = options["FormatParams"];
		//Compression type
		fmtParams["COMP"].v = 5; //LZW compression
									//X DPI
		fmtParams["DPIX"].v = 150;
		//Y DPI
		fmtParams["DPIY"].v = 150;
		//Image format
		fmtParams["FMT"].v = 1414088262; //TIFF
											//Image type
		fmtParams["ITYP"].v = 16; //24 TrueColor
									//Use Predictor
		fmtParams["PRED"].v = 1; //Yes
									//Thumbnail
		fmtParams["THUM"].v = 0; //No
		pdfCtl.Inst.AsyncDoAndWaitForFinish(Op);
		data.Doc.Close();
	}
	catch (System.Exception ex)
	{
		int a = ex.HResult;
	}
}
Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
chavas
User
Posts: 141
Joined: Tue Mar 15, 2016 12:21 pm

Re: Error when used on threading

Post by chavas »

Hi,

We will not use pdfctrl but use your api calls of PXV_Inst and IPXC_Inst. This code is designed to run in background/service without any user interface. Hence it will not have any pdfctrl. This function throws exception when called in a thread..
doc = m_pxcInst.OpenDocumentFromFile(data.sFolder, Nothing)

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

Re: Error when used on threading

Post by Sasha - Tracker Dev Team »

Hello Charu,

Well, you are using the STA, not MTA - thus the exception.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
chavas
User
Posts: 141
Joined: Tue Mar 15, 2016 12:21 pm

Re: Error when used on threading

Post by chavas »

thanks..that worked.... :D
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: Error when used on threading

Post by Sasha - Tracker Dev Team »

:)
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: Error when used on threading

Post by Sasha - Tracker Dev Team »

Hello TimothyPer,

I don't think that this is a SDK related question. Please mail your problems to the support@pdf-xchange.com or post at the correct forum.

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