logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
ju1989  
#1 Posted : Tuesday, August 2, 2016 8:12:21 AM(UTC)
ju1989

Rank: Member

Groups: Registered
Joined: 6/1/2016(UTC)
Posts: 28
Germany
Location: Hessen

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
With one of the latest versions of Pdfium, you set the method PdfPageObject.Copy(PdfPageObject) to obsolete.
This was the only method that could restore the FS_MATRIX of an PdfPageObject.

In my case I have to remove all PdfFormObjects, render the page and restore the PdfFormObjects. Until the latest update I copied all PdfFormObjects to local variables, transformed all PdfFormObjects with a FS_MATRIX(0,0,0,0,0,0), rendered the page and than copied the PdfFormObject to the the old objects. So after this process the content of the document was still the same like before the process.

Do you have any alternative methods for PdfPageObject.Copy(PdfPageObject)

This was my code until the latest version:


Code:
public static void MyMethod(PdfPage page)
{
	var elements = FadeOut(page);
	//Render page here
	FadeIn(page, elements);
}




internal static List<PdfElement> FadeOut(PdfPage page)
{
	List<PdfElement> result = new List<PdfElement>();
	IEnumerable<PdfPageObject> formObjects = page.PageObjects.Where(x => x is PdfFormObject);
	foreach (PdfPageObject pdfPageObject in formObjects)
	{
		IntPtr handle = pdfPageObject.Handle;
		PdfFormObject obj = PdfFormObject.Create();
		result.Add(new PdfElement(handle, obj));
		obj.Copy(pdfPageObject);
		pdfPageObject.Transform(0, 0, 0, 0, 0, 0);
	}
	return result;
}

internal static void FadeIn(PdfPage page, List<PdfElement> fadeInElements)
{
	foreach (PdfElement pdfElement in fadeInElements)
	{
		var pageElement = page.PageObjects.FirstOrDefault(x => x.Handle == pdfElement.Handle);
		if (pageElement == null)
		{
			throw new InvalidOperationException("No element found.");
		}
		pageElement.Copy(pdfElement.PdfPageObject);
	}
}

internal class PdfElement
{
	public IntPtr Handle { get; private set; }

	public PdfPageObject PdfPageObject { get; private set; }

	public PdfElement(IntPtr handle, PdfPageObject pdfPageObject)
	{
		Handle = handle;
		PdfPageObject = pdfPageObject;
	}
}


Paul Rayman  
#2 Posted : Tuesday, August 2, 2016 4:20:55 PM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
Unfortunately the Copy method has been removed from the original PDFium engine.
It is supposed to use the Clone method instead.
But in you case, you need to use the PdfFormsObject.Matrix property.
With it you can get and set the matrices to PdfForms objects.
ju1989  
#3 Posted : Wednesday, August 3, 2016 12:15:50 AM(UTC)
ju1989

Rank: Member

Groups: Registered
Joined: 6/1/2016(UTC)
Posts: 28
Germany
Location: Hessen

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
I know I can set the matrix, but when I once set the matrix 0,0,0,0,0,0 I can't change it any more, either with PdfFormObject.Matrix = mymatrix or PdfFormObject.Transform(mymatrix), it will always be 0,0,0,0,0,0.


Code:
public static void MyMethod(PdfPage page)
{
	var elements = FadeOut(page);
	//Render page here
	FadeIn(page, elements);
}




internal static List<PdfElement> FadeOut(PdfPage page)
{
	List<PdfElement> result = new List<PdfElement>();
	IEnumerable<PdfFormObject> formObjects = page.PageObjects.OfType<PdfFormObject>();
	foreach (PdfFormObject pdfFormObject in formObjects)
	{
		IntPtr handle = pdfFormObject.Handle;
		result.Add(new PdfElement(handle, pdfFormObject.Matrix.Clone()));
		pdfPageObject.Transform(0, 0, 0, 0, 0, 0);
	}
	return result;
}

internal static void FadeIn(PdfPage page, List<PdfElement> fadeInElements)
{
	foreach (PdfElement pdfElement in fadeInElements)
	{
		var pdfFormObject = page.PageObjects.FirstOrDefault(x => x.Handle == pdfElement.Handle) as PdfFormObject;
		if (pdfFormObject == null)
		{
			throw new InvalidOperationException("No element found.");
		}
		var matrix = pdfElement.Matrix.Clone();
        pdfFormObject.Matrix = matrix; //Does not work (pdfFormObject.Matrix = 0,0,0,0,0,0)
		pdfFormObject.Transform(matrix); //Does not work (pdfFormObject.Matrix = 0,0,0,0,0,0)
	}
}

internal class PdfElement
{
	public IntPtr Handle { get; private set; }

	public FS_MATRIX Matrix { get; private set; }

	public PdfElement(IntPtr handle, FS_MATRIX matrix)
	{
		Handle = handle;
		Matrix = matrix;
	}
}

internal static class Extensions
    {
        public static FS_MATRIX Clone(this FS_MATRIX matrix)
        {
            return new FS_MATRIX()
            {
                a = matrix.a,
                b = matrix.b,
                c = matrix.c,
                d = matrix.d,
                e = matrix.e,
                f = matrix.f,
            };
        }
    }
Paul Rayman  
#4 Posted : Wednesday, August 3, 2016 1:32:44 AM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
Originally Posted by: ju1989 Go to Quoted Post
I know I can set the matrix, but when I once set the matrix 0,0,0,0,0,0 I can't change it any more, either with PdfFormObject.Matrix = mymatrix or PdfFormObject.Transform(mymatrix), it will always be 0,0,0,0,0,0.


It seems like a bug. I'll check it.

Edited by user Wednesday, August 3, 2016 6:06:55 PM(UTC)  | Reason: Not specified

Paul Rayman  
#5 Posted : Wednesday, August 3, 2016 2:37:10 AM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
Fixed

Please upgrade to the latest version.

Now the following code works fine.
Code:

private Dictionary<IntPtr, FS_MATRIX> listOfMatrices = new Dictionary<IntPtr, FS_MATRIX>();

public void HideFormObjects(PdfDocument doc)
{
	foreach (var page in doc.Pages)
	{
		foreach (var obj in page.PageObjects)
		{
			var fo = obj as PdfFormObject;
			if (fo == null)
				continue;
			if (!listOfMatrices.ContainsKey(fo.Handle))
			{
				listOfMatrices.Add(fo.Handle, fo.Matrix);
				fo.Matrix = new FS_MATRIX() { a = 0, b = 0, c = 0, d = 0, e = 0, f = 0 };
			}
		}
	}
}

public void ShowFormObjects(PdfDocument doc)
{
	foreach (var page in doc.Pages)
	{
		foreach (var obj in page.PageObjects)
		{
			var fo = obj as PdfFormObject;
			if (fo == null)
				continue;
			fo.Matrix = listOfMatrices[fo.Handle];
		}
	}
}

Edited by user Wednesday, August 3, 2016 2:40:03 AM(UTC)  | Reason: Not specified

ju1989  
#6 Posted : Wednesday, August 3, 2016 2:46:07 AM(UTC)
ju1989

Rank: Member

Groups: Registered
Joined: 6/1/2016(UTC)
Posts: 28
Germany
Location: Hessen

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
Works great. Thank you!
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.