PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Oct 31, 2024 11:53 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Thu Nov 16, 2023 10:18 am 
Offline

Joined: Wed Nov 15, 2023 7:55 am
Posts: 5
I have the following code
Code:
var document = new Document();
var section = document.AddSection();
section.PageSetup = document.DefaultPageSetup.Clone();
section.PageSetup.PageFormat = PageFormat.A4;
section.PageSetup.TopMargin = Unit.FromPoint(18);
section.PageSetup.LeftMargin = section.PageSetup.RightMargin = Unit.FromPoint(18);
section.PageSetup.BottomMargin = Unit.FromPoint(18);
Unit page_width = section.PageSetup.PageWidth - section.PageSetup.LeftMargin - section.PageSetup.RightMargin;
Unit page_height = section.PageSetup.PageHeight - section.PageSetup.TopMargin - section.PageSetup.BottomMargin;
var table = section.AddTable();
table.Borders.Top.Style = BorderStyle.Single;
table.Borders.Top.Width = Unit.FromPoint(1);
table.Borders.Top.Color = Colors.Black;
table.Borders.Bottom = table.Borders.Top.Clone();
table.Borders.Left = table.Borders.Top.Clone();
table.Borders.Right = table.Borders.Top.Clone();

table.AddColumn(page_width / 8);
table.AddColumn(page_width / 8);
table.AddColumn(page_width / 8);
table.AddColumn(page_width / 8);
table.AddColumn(page_width / 8);
table.AddColumn(page_width / 8);
table.AddColumn(page_width / 8);
table.AddColumn(page_width / 8);

var row = table.AddRow();
row.Cells[0].MergeRight = 3;
row.Cells[0].AddParagraph("[0,0]");
row.Cells[4].AddParagraph("[0,4]");
row.Cells[4].MergeDown = 1;
row.Cells[4].VerticalAlignment = VerticalAlignment.Center;
row.Cells[5].AddParagraph("[0,5]");
row.Cells[5].MergeDown = 1;
row.Cells[5].VerticalAlignment = VerticalAlignment.Center;
row.Cells[6].AddParagraph("[0,6]");
row.Cells[6].MergeDown = 1;
row.Cells[6].VerticalAlignment = VerticalAlignment.Center;
row.Cells[7].AddParagraph("[0,7]");
//row.Cells[7].MergeDown = 1; // <-- Cant set to Merge Down = 1
row.Cells[7].VerticalAlignment = VerticalAlignment.Center;

row = table.AddRow();
row.Cells[0].MergeRight = 3;
row.Cells[0].MergeDown = 2;
row.Cells[0].AddParagraph("[1,0]");

row = table.AddRow();
row.Cells[4].AddParagraph("[2,4]");
row.Cells[4].MergeDown = 1;
row.Cells[4].VerticalAlignment = VerticalAlignment.Center;
row.Cells[5].AddParagraph("[2,5]");
row.Cells[5].MergeDown = 1;
row.Cells[5].MergeRight = 1; // <-- Cant set to MergeRight = 2
row.Cells[5].VerticalAlignment = VerticalAlignment.Center;

row = table.AddRow();

var pdf = new PdfDocument();
var renderer = new DocumentRenderer(document);
renderer.PrepareDocument();
for (int i = 1; i <= renderer.FormattedDocument!.PageCount; i++)
{
    var page = pdf.AddPage();
    var gfx = XGraphics.FromPdfPage(page);
    renderer.RenderPage(gfx, i);
}
return pdf;


This produces the following:
Image
But I need the [0,7] cell to merge down (just like the other [0,4], [0,5] & [0,6] cells)
And I need the [2,5] cell to merge right 2, currently I can only get it to merge right 1.

If i try and apply the MergeDown = 1 to the [0,7] cell or the MergeRight = 2 to the [2,5] cell I get the following exception:
Code:
InvalidOperationException: GetMinMergedCell: Unexpected problem #1

    MigraDoc.Rendering.TableRenderer.GetMinMergedCell(int row)
    MigraDoc.Rendering.TableRenderer.CreateNextBottomBorderPosition(ref int skipIndex, ref int lastBorderRow)
    MigraDoc.Rendering.TableRenderer.CreateBottomBorderMap()
    MigraDoc.Rendering.TableRenderer.InitFormat(Area area, FormatInfo previousFormatInfo)
    MigraDoc.Rendering.TableRenderer.Format(Area area, FormatInfo previousFormatInfo)
    MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, bool topLevel)
    MigraDoc.Rendering.FormattedDocument.Format(XGraphics gfx)
    MigraDoc.Rendering.DocumentRenderer.PrepareDocument()
    PdfApp.DoSBuilderService.Render(Document document) in DoSBuilderService.cs

            renderer.PrepareDocument();

PdfApp.DoSBuilderService.BuildAsync(CancellationToken cancellationToken) in DoSBuilderService.cs

            using (var pdf = Render(document))

PdfApp.Controllers.HomeController.Index() in HomeController.cs

                return await builder.BuildAsync();

Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor+TaskOfIActionResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
System.Threading.Tasks.ValueTask<TResult>.get_Result()
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask<IActionResult> actionResultValueTask)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)



I dont understand what I'm doing wrong, Can you please help.


Top
 Profile  
Reply with quote  
PostPosted: Thu Nov 16, 2023 10:33 am 
Offline

Joined: Wed Nov 15, 2023 7:55 am
Posts: 5
I believe this is a bug, because if I add an additional column to the table with a width of zero. Then the [0,7] cell can MergeDown = 1, and the [2,5] cell can MergeRight = 2.


Top
 Profile  
Reply with quote  
PostPosted: Thu Nov 16, 2023 10:52 am 
Offline

Joined: Wed Nov 15, 2023 7:55 am
Posts: 5
Could you then explain how i can achieve what I need without the extra 0 width column?


Top
 Profile  
Reply with quote  
PostPosted: Thu Nov 16, 2023 11:48 am 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1000
Location: CCAA
OK, it is a bug.
You no longer need the fourth row when setting MergeRight = 2 for cell [2,5]. This should work, even though all cells of row 4 (index 3) are now hidden.

The bug is complicated. A zero-width column is a good workaround until the bug is fixed.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 28 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group