Small application to color 2 pdfs and overlay them

PDF-XChange Editor SDK for Developers

Moderators: Daniel - PDF-XChange, PDF-XChange Support, Vasyl - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Paul - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

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
Markus123456789
User
Posts: 1
Joined: Thu May 02, 2024 1:20 pm

Small application to color 2 pdfs and overlay them

Post by Markus123456789 »

Hey :)
I would like to use PDF-XChange Editor SDK and Winforms to make 1 Button for the process to color 1 pdf red,1 blue using stroke color then overlay them and use blend mode to Multiply.
I know that there is the Compare tool but that sadly does not fulfill our needs....

Is that possible?
User avatar
Vasyl - PDF-XChange
Site Admin
Posts: 2444
Joined: Thu Jun 30, 2005 4:11 pm

Re: Small application to color 2 pdfs and overlay them

Post by Vasyl - PDF-XChange »

Hi Markus.

Yes, for sure its possible with our Editor/CoreAPI SDK. Here is the working code snippet to do this magic job:

Code: Select all

private bool ReplColorsInContent(PDFXEdit.IPXC_Content con, PDFXEdit.IPXC_Color newClr)
{
	if (con == null || con.Items.Count == 0)
		return false;

	bool bRes = false;

	IPXC_Document doc = con.Document;

	uint statesCount = con.CStatesCount;
	for (uint j = 0; j < statesCount; j++)
	{
		PDFXEdit.IPXC_CState st = con.GetCStateByIndex(j);
		st.FillColor = newClr;
		st.StrokeColor = newClr;
		st.Flags |= 3;
		con.SetCStateByIndex(j, st);
		bRes = true;
	}

	// look deep into sub-contents recursively
	uint itemsCount = con.Items.Count;
	for (uint i = 0; i < itemsCount; i++)
	{
		IPXC_ContentItem ci = con.Items[i];
		if (ci.Type != PXC_CIType.CIT_XForm)
			continue;

		IPXC_XForm xf = doc.GetXFormByHandle(ci.XForm_Handle);
		if (xf != null)
		{
			IPXC_Content xf_con = xf.GetContent(PXC_ContentAccessMode.CAccessMode_FullClone);
			if (xf_con != null)
			{
				if (ReplColorsInContent(xf_con, newClr))
				{
					xf.SetContent(xf_con);
					bRes = true;
				}
			}
		}
	}

	return bRes;
}

private void ReplColorsOnPages(PDFXEdit.IPXC_Document doc, Color newClr, uint startPage, uint pagesCount)
{
	if (doc == null)
		return;

	PDFXEdit.IPXC_Color clr = doc.CreateStdColor(PDFXEdit.PXC_ColorSpaceType.CS_DeviceRGB);

	double[] rgb = new double[3];
	rgb[0] = (double)newClr.R / 255.0;
	rgb[1] = (double)newClr.G / 255.0;
	rgb[2] = (double)newClr.B / 255.0;

	clr.SetComponents2(rgb);

	for (uint i = 0; i < pagesCount; i++)
	{
		PDFXEdit.IPXC_Page pg = doc.Pages[i + startPage];
		PDFXEdit.IPXC_Content con = pg.GetContent(PXC_ContentAccessMode.CAccessMode_FullClone);
		if (con == null || con.Items.Count == 0)
			continue;

		if (ReplColorsInContent(con, clr))
			pg.PlaceContent(con, 2);
	}
}

private PDFXEdit.IPXC_Document CreateDocCmpReport(PDFXEdit.IPXC_Inst pxcInst, PDFXEdit.IAUX_Inst auxInst, PDFXEdit.IPXC_Document doc1, PDFXEdit.IPXC_Document doc2, Color clr1, Color clr2)
{
	if (doc1 == null || doc2 == null)
		return null;

	PDFXEdit.IPXC_Document doc = pxcInst.NewDocument();

	uint pagesCount1 = doc1.Pages.Count;
	uint pagesCount2 = doc2.Pages.Count;

	uint pagesInsertFlags = (uint)PDFXEdit.PXC_InsertPagesFlags.IPF_Annots_Flatten | (uint)PDFXEdit.PXC_InsertPagesFlags.IPF_Widgets_Flatten | (uint)PDFXEdit.PXC_InsertPagesFlags.IPF_Bookmarks_DoNotCopy;

	doc.Pages.InsertPagesFromDoc(doc1, 0,	 0, pagesCount1, pagesInsertFlags);
	doc.Pages.InsertPagesFromDoc(doc2, pagesCount1, 0, pagesCount2, pagesInsertFlags);

	ReplColorsOnPages(doc, clr1, 0, pagesCount1);
	ReplColorsOnPages(doc, clr2, pagesCount1, pagesCount2);

	// doc.WriteToFile("f:\\__tmp\\CmpClrTest\\docCmdPep_pre.pdf");

	uint cnt = Math.Min(pagesCount1, pagesCount2);
	
	for (uint i = 0; i < cnt; i++)
	{
		PDFXEdit.IPXC_Page pgDst = doc.Pages[i];
		PDFXEdit.IPXC_Page pgSrc = doc.Pages[i + pagesCount1];

		PDFXEdit.IPXC_Content dstCon = pgDst.GetContent(PXC_ContentAccessMode.CAccessMode_FullClone);

		uint insertPos = dstCon.Items.Count;

		PDFXEdit.IPXC_XGroup xgr = doc.CreateXGroup(PDFXEdit.PXC_XGroupType.XGT_Transparency);
		
		PDFXEdit.IPXC_XForm xf = doc.CreateXFormFromPage(pgSrc);

		xgr.Isolated = true;
		xf.SetXGroup(xgr);

		PDFXEdit.PXC_Rect bbox = xf.get_BBox();

		PDFXEdit.IPXC_ContentCreator cc = doc.CreateContentCreator();
		cc.Attach(dstCon);
		cc.ResetAllStatesToDefault();

		cc.SetBlendMode(PXC_BlendMode.BlendMode_Multiply);
		cc.PlaceXFormEx(xf, bbox, "Overlay");

		dstCon = cc.Detach();

		pgDst.PlaceContent(dstCon, 2);
	}

	PDFXEdit.IBitSet pagesToDel = auxInst.CreateBitSet(doc.Pages.Count);
	for (uint i = 0; i < cnt; i++)
		pagesToDel.Set(i + pagesCount1);

	PDFXEdit.IPXC_UndoRedoData ud = null;
	doc.Pages.DeletePages(pagesToDel, null, out ud);
	ud = null;

	return doc;
}

private void btnCreateCmpDocsReport(object sender, EventArgs e)
{
	PDFXEdit.IPXC_Document doc1 = pxcInst.OpenDocumentFromFile("f:\\__tmp\\CmpClrTest\\doc1.pdf", null);
	PDFXEdit.IPXC_Document doc2 = pxcInst.OpenDocumentFromFile("f:\\__tmp\\CmpClrTest\\doc2.pdf", null);

	PDFXEdit.IPXC_Document docCmdPep = CreateDocCmpReport(pxcInst, auxInst, doc1, doc2, Color.Red, Color.Blue);

	doc1.Close();
	doc2.Close();

	if (docCmdPep != null)
	{
		docCmdPep.WriteToFile("f:\\__tmp\\CmpClrTest\\docCmdPep.pdf");
		docCmdPep.Close();
	}
}
Result:

image.png

HTH.
PDF-XChange Co. LTD (Project Developer)

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