JBIG compression .NET  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
Lambchop
User
Posts: 34
Joined: Mon May 02, 2022 5:58 pm

JBIG compression .NET

Post by Lambchop »

I have been trying to implement JBIG from https://sdkhelp.pdf-xchange.com/view/PXV:IPXC_Document_SetEncodersForStreamType and https://sdkhelp.pdf-xchange.com/view/PXV:IPXS_EncodeFiltersArray_AddFilterInfo but I am not having much success. Specifically, I am looking for an example in .NET that follows this order:
1) loads a PDF from a file into memory
2) compresses the in memory PDF file using JBIG (90% is black/white)
3) allows me to save to a binary file stream (I am storing inside of SQL Server)

Now, if I can directly pull and compress the file directly into the binary file stream ... that would be even better ;)
I have searched the forums, but I have not found enough to get me over the humps ... any help is greatly appreciated! Thank you guys!
-Eric
User avatar
Vasyl-Tracker Dev Team
Site Admin
Posts: 2352
Joined: Thu Jun 30, 2005 4:11 pm
Location: Canada

Re: JBIG compression .NET  SOLVED

Post by Vasyl-Tracker Dev Team »

Hi Eric.

In context of our FullDemo(C#) I tried the following code and it works well:

Code: Select all

FileStream srcStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
if (srcStream == null)
	return;

IStreamWrapper srcIStream = new IStreamWrapper(srcStream);
PDFXEdit.IPXC_Document doc = pxcInst.OpenDocumentFrom(srcIStream, null);
if (doc == null)
	return;

PDFXEdit.IPXS_Document cosDoc = doc.CosDocument;

PDFXEdit.IPXS_EncodeFiltersArray jbig2Encoder = pxsInst.Create_EncodeFiltersArray();
jbig2Encoder.AddFilterInfo(pxsInst.StrToAtom("JBIG2Decode"), null);

cosDoc.LockDocumentExclusive();

uint atm_Subtype = pxsInst.StrToAtom("Subtype");
uint atm_BitsPerComponent = pxsInst.StrToAtom("BitsPerComponent");
uint atm_Width = pxsInst.StrToAtom("Width");
uint atm_Height = pxsInst.StrToAtom("Height");
uint atm_Image = pxsInst.StrToAtom("Image");

PDFXEdit.IMemBlock mb = null;
PDFXEdit.PXS_StreamInfo si = new PDFXEdit.PXS_StreamInfo();
uint pos = 0;
bool docChanged = false;
bool docErr = false;
do
{
	PDFXEdit.IPXS_PDFVariant var = cosDoc.GetNextIndirect(ref pos);
	if (var == null)
		break;
	if (var.Type != PDFXEdit.PXS_PDFVariantType.PVT_Stream)
		continue;

	// check for valid monochrome image
	if (var.Dict_GetNameA(atm_Subtype, 0) != atm_Image)
		continue;
	if (var.Dict_GetIntA(atm_BitsPerComponent, 0) != 1)
		continue;
	int width = var.Dict_GetIntA(atm_Width, 0);
	if (width <= 0)
		continue;
	int height = var.Dict_GetIntA(atm_Height, 0);
	if (height <= 0)
		continue;
	if (mb == null)
		mb = auxInst.CreateMemBlock(0);

	try
	{
		var.Stream_GetMemData(mb);

		si.nType = PDFXEdit.PXS_StreamType.Stream_Generic;
		si.nWidth = (uint)width;
		si.nHeight = (uint)height;
		si.nComponents = 1;

		var.Stream_SetMemData(mb, jbig2Encoder, ref si);

		docChanged = true;
	}
	catch
	{
		docErr = true;
		break;
	}
}
while (true);

cosDoc.UnlockDocumentExclusive();
cosDoc = null;

if (!docErr && docChanged)
	doc.WriteTo(null); // null to write to srcIStream/srcStream back
Please note: the current IStreamWrapper sample's implementation has one issue - its SetSize method isn't implemented. So doc.WriteTo may work incorrectly in some cases. If you have plans to use the IStreamWrapper you need to add this:

Code: Select all

public class IStreamWrapper : IStream
{
....
	void IStream.SetSize(long libNewSize)
	{
		m_stream.SetLength(libNewSize); // +++
	}
....	
}
HTH
Vasyl Yaremyn
Tracker Software Products
Project Developer

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
Lambchop
User
Posts: 34
Joined: Mon May 02, 2022 5:58 pm

Re: JBIG compression .NET

Post by Lambchop »

Vasyl - I have searched through the FullDemo code and there is no such JBIG code like you posted. I honestly have no idea where your code snippet came from because I also searched through all C# samples and never found your code. However, I am converting and testing your code approach. It looks like there are several methods that I was not aware of within the library and your code is very helpful in putting it all together. Thank You! -Eric
User avatar
Vasyl-Tracker Dev Team
Site Admin
Posts: 2352
Joined: Thu Jun 30, 2005 4:11 pm
Location: Canada

Re: JBIG compression .NET

Post by Vasyl-Tracker Dev Team »

...I have searched through the FullDemo code and there is no such JBIG code like you posted.
Because the original FullDemo doesn't contain that code. I added and tested it in my local copy of FullDemo-project.
Seems is not bad idea to add such code-snippet into the public version of FullDemo..
Vasyl Yaremyn
Tracker Software Products
Project Developer

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
Post Reply