DrawPageToDIBSection equivalent in Core API  SOLVED

A forum for questions or concerns related to the PDF-XChange Core API SDK

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, 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
Max SAG
User
Posts: 27
Joined: Thu Oct 30, 2014 8:54 am

DrawPageToDIBSection equivalent in Core API

Post by Max SAG »

Hi,

I want to update my code using the Viewer Simple DLL SDK to the new Core API SDK to solve our problem described here. We currently have a Core API SDK license.

I only need the PXCV_GetPagesCount, PXCV_GetPageDimensions and PXCV_DrawPageToDIBSection methods. Which solution is the best:
  • Use the Core API SDK (IPXC_Document.Pages.Count, IPXC_Page.GetDimension and IPXC_Page.DrawToMemory or IPXC_Page.DrawToDevice?) ;
  • Or use the Editor Simple SDK with the same method names (as mentioned here), but I don't think that the Core API SDK license we have work with it, right?
Regards,

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

Re: DrawPageToDIBSection equivalent in Core API  SOLVED

Post by Sasha - Tracker Dev Team »

Hello Max,

Well I think that you can go with the Core API - the IPXC_Document.Pages.Count, IPXC_Page.GetDimension and IPXC_Page.DrawToDevice should work for you.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Max SAG
User
Posts: 27
Joined: Thu Oct 30, 2014 8:54 am

Re: DrawPageToDIBSection equivalent in Core API

Post by Max SAG »

Hi,

Thank you for the quick answer. I started to update my code but I encounter an error using the Core API with multiple threads :

Code: Select all

System.Runtime.InteropServices.COMException occurred
  ErrorCode=-2147352568
  HResult=-2147352568
  Message=Bad variable type. (Exception de HRESULT : 0x80020008 (DISP_E_BADVARTYPE))
  InnerException: 
It seems to be the same error here. I run my code in a WPF application on Windows 10. You can reproduce it easily:

Code: Select all

var coreAPI = new PDFXCoreAPI.PXC_Inst();
coreAPI.Init(key);
var document1 = coreAPI.NewDocument();
document1.Close();
var thread = new Thread(() =>
{
    var document2 = coreAPI.NewDocument();
    document2.Close();
});
thread.Start();
How can I use the API with multiple threads?

Best regards,

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

Re: DrawPageToDIBSection equivalent in Core API

Post by Sasha - Tracker Dev Team »

Hello Max,

Have you tried setting the appartment state to MTA?
th.SetApartmentState(ApartmentState.MTA);

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Max SAG
User
Posts: 27
Joined: Thu Oct 30, 2014 8:54 am

Re: DrawPageToDIBSection equivalent in Core API

Post by Max SAG »

I tried both, ApartmentState.STA and ApartmentState.MTA but I always get the same error. With the old Viewer Simple DLL SDK, I called the PXCV_DrawPageToDIBSection in a STA apartment thread.

Regards,

Max
Max SAG
User
Posts: 27
Joined: Thu Oct 30, 2014 8:54 am

Re: DrawPageToDIBSection equivalent in Core API

Post by Max SAG »

Hi,

Do you think that it's bug in the Core API SDK or a mistake I made using the API in the other thread? I tried to look for this exception using COM objects in a multi-thread context but I can't find a solution to this problem (apart from the MTA apartment state which does not solve the problem for me).

Best regards,

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

Re: DrawPageToDIBSection equivalent in Core API

Post by Sasha - Tracker Dev Team »

Hello Max,

Well I have this sample here that works for the Editor SDK Full Demo sample:

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;
	}
}
The program itself is running at the [STAThread]:

Code: Select all

namespace FullDemo
{
	static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false); 

			Application.Run(new MainFrm());
		}
	}
}
Maybe this will help you somehow.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Max SAG
User
Posts: 27
Joined: Thu Oct 30, 2014 8:54 am

Re: DrawPageToDIBSection equivalent in Core API

Post by Max SAG »

Hi,

OK thank you for the sample I understand now. My mistake was to use API from both a STA thread (the main thread) and a MTA thread. If I understand correctly, if I want to do multi-threading, I have to always use the API from a MTA thread (even the synchronous calls):

Wrong way of using the API with multiple threads:

Code: Select all

// Initialize the API in the main STA thread
var coreAPI = new PDFXCoreAPI.PXC_Inst();
coreAPI.Init(key);
var document1 = coreAPI.NewDocument();
document1.Close();

// Use the API in a MTA thread
var thread = new Thread(() =>
{
    var document2 = coreAPI.NewDocument(); // => COMException (Bad variable type)
    document2.Close();
});
thread1.SetApartmentState(ApartmentState.MTA);
thread.Start();
Right way of using the API with multiple threads:

Code: Select all

// Initialize the API in a MTA thread
PDFXCoreAPI.PXC_Inst coreAPI = null;
var thread = new Thread(() =>
{
    coreAPI = new PDFXCoreAPI.PXC_Inst();
    coreAPI.Init(key);
});
thread.SetApartmentState(ApartmentState.MTA);
thread.Start();
// Wait for the thread to end
thread.Join();

// Use the API in a MTA thread
var thread1 = new Thread(() =>
{
    var document1 = coreAPI.NewDocument();
    document1.Close();
});
thread1.SetApartmentState(ApartmentState.MTA);
thread1.Start();

// Use the API in another MTA thread
var thread2 = new Thread((data) =>
{
    var document2 = coreAPI.NewDocument();
    document2.Close();
});
thread2.SetApartmentState(ApartmentState.MTA);
thread2.Start();
So I guess I'll have use a MTA thread for all my API calls (synchronous and asynchronous), because the main WPF thread is STA (stackoverflow example here).
It's not ideal but it seems to work.

Best regards,

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

Re: DrawPageToDIBSection equivalent in Core API

Post by Sasha - Tracker Dev Team »

Hello Max,

Glad you got it working.

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