PdfSharpCore icon indicating copy to clipboard operation
PdfSharpCore copied to clipboard

Make DocumentRenderer.GetRenderInfoFromPage(...) public

Open AndreasBurbach opened this issue 2 years ago • 2 comments

Hey, i would like to know the rendered rectangle of my DocumentObjects, for that it would be helpfull if you could please make the GetRenderInfoFromPage(int) in DocumentRenderer public. Currently i'm using Reflection to get this infos...

Best regards Andreas

AndreasBurbach avatar Mar 08 '22 14:03 AndreasBurbach

Hey, i would like to know the rendered rectangle of my DocumentObjects, for that it would be helpfull i you could please make the GetRenderInfoFromPage(int) in DocumentRenderer public. Currently i'm using Reflection to get this infos...

Best regards Andreas

@AndreasBurbach In the original way, it used this code to recalculate the height of the document (set to a ridiculously large height at the beginning). But now I don't know how to do it

please help me

[Original article:](https://stackoverflow.com/questions/23708750/migradoc-dynamic-page-size)
          
 // Recalculamos la altura (establecida a una altura ridículamente grande al principio), para que la impresora sepa donde cortar el papel.
            DocumentRenderer renderer = new DocumentRenderer(document);
            renderer.PrepareDocument();
            MigraDocCore.Rendering.RenderInfo[] info = renderer.GetRenderInfoFromPage(1);
            int index = info.Length - 1;

            double stop = info[index].LayoutInfo.ContentArea.Y.Millimeter + info[index].LayoutInfo.ContentArea.Height.Millimeter
                + 2 * Tamanios.TopBottomPageMargin; // Add more if you have bottom page margin, borders on the last table etc.
            currentSection.PageSetup.PageHeight = Unit.FromMillimeter(stop);

DarioSalcedo avatar Apr 23 '22 17:04 DarioSalcedo

Because everything i use here is non-public, is solved it with reflection

  	var bindingFlags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance;

	var methodInfo = docRenderer.FormattedDocument.GetType().GetMethod("GetRenderInfos", bindingFlags);
	var renderInfos = (object[])methodInfo.Invoke(docRenderer.FormattedDocument, new object[] { 1 });

	var renderedRectangle = new XRect();

	foreach (var renderInfo in renderInfos)
	{
		var docObj = renderInfo.GetType().GetProperty("DocumentObject", bindingFlags).GetValue(renderInfo) as DocumentObject;
		if (docObj?.Tag as string == element.Id)
		{
			var layoutInfo = renderInfo.GetType().GetProperty("LayoutInfo", bindingFlags).GetValue(renderInfo);
			var area = layoutInfo.GetType().GetProperty("ContentArea", bindingFlags).GetValue(layoutInfo);
			var x = area.GetType().GetProperty("X", bindingFlags).GetValue(area) as XUnit?;
			var y = area.GetType().GetProperty("Y", bindingFlags).GetValue(area) as XUnit?;
			var width = area.GetType().GetProperty("Width", bindingFlags).GetValue(area) as XUnit?;
			var height = area.GetType().GetProperty("Height", bindingFlags).GetValue(area) as XUnit?;

			renderedRectangle.X = x?.Point ?? 0;
			renderedRectangle.Y = y?.Point ?? 0;
			renderedRectangle.Width = width?.Point ?? 0;
			renderedRectangle.Height = height?.Point ?? 0;
			break;
		}
	}

AndreasBurbach avatar Apr 29 '22 07:04 AndreasBurbach

Thank you ❤️‍🔥

AndreasBurbach avatar Apr 06 '23 11:04 AndreasBurbach