PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 9:03 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Tue Jan 12, 2016 1:51 pm 
Offline

Joined: Tue Jan 12, 2016 12:17 pm
Posts: 9
Hello,

I'm a new user of PDFSharp & MigraDoc (GDI v1.50.3915) and I have some questions to finalise my project.

Here a scheme of what I try to do:

1st page of my doc
Image

other pages of my doc
Image

The body of my document is a table which can be on many pages (it came from a database). On the last page, I added a paragraph.

code sample of my table:
Code:
Document migraDoc = new Document();
Section migraSection = migraDoc.AddSection();
migraSection.PageSetup.DifferentFirstPageHeaderFooter = true;
migraSection.PageSetup.TopMargin = Unit.FromMillimeter(HeightValue); // HeightValue = the height I need for my 1st header
// same stuff to all margin...

Table migraTable = migraDoc.LastSection.AddTable();
migraTable.AddColumn(Unit.FromMillimeter(ColumnWidth)); // I do as many column as I need

Row row = migraTable.AddRow(); // I do as many row as I need
Cell cell = row.Cells[index]; // I do as many cell in a row as column
Paragraph paragraph = cell.AddParagraph();
paragraph = HtmlString.ToMigradocParagraph(); // I convert a html string into a MigraDoc Paragraph



So the size of the 1st and primary header are different.
On MigraDoc, can we have a different Section.PageSetup.TopMargin to set y and y' and have the right amount of page during render?

What I need to print on the header and footer came from a SVG element like this:
Code:
<rect id="Cadre" x="5.1725mm" y="89.4099mm" width="198.624mm" height="7.1939mm" stroke-width="1" fill="none" stroke="black" rx="5" ry="5"></rect>

It's really easy to convert them into Xgraphics elements with PDFSharp.

A sample of what I do:
Code:
PdfDocument pdfDoc = new PdfDocument();
PdfPage pdfPage = pdfDoc.AddPage();

pdfPage.Size = PdfSharp.PageSize.A4;

XGraphics gfx = XGraphics.FromPdfPage(pdfPage, XGraphicsUnit.Millimeter);

DrawSvgElement(gfx, SvgElement); // I draw a Svg Element with XGraphics method.


At this point, I have a Document with my data table (with automatic page break) and a PdfDocument with my header and footer.

So I would like to use the pdfPage as a background of all Document's pages. How can I do this?
I tried stuff like this:
Code:
DocumentRenderer migraRenderer = new DocumentRenderer(migraDoc);
migraRenderer.PrepareDocument();

PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer();

int nbPage = migraRenderer.FormattedDocument.PageCount; // the count is not great because I don't know how to adapt my table area
for (int i = 0; i < nbPage; i++)
{
    migraRenderer.RenderPage(gfx, i + 1); 
}

pdfRenderer.Document = migraDoc;
pdfRenderer.RenderDocument();


All that I have is a 1st page with the PdfSharp and my table on the others.

If I have a row bigger than my section, how can I split it into 2 rows?

Thanks


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 13, 2016 8:53 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Hi!

rbe42 wrote:
other pages of my doc
The image does not show.
I assume the first page has a larger header. It is up to you to reserve space for it (e.g. using SpaceBefore with the first paragraph on this page).

rbe42 wrote:
If I have a row bigger than my section, how can I split it into 2 rows?
You know the contents of that row, it is up to you to split it. Maybe make two rows without border between them.


With respect to background pages: check this sample code:
viewtopic.php?f=8&t=3172

Before the line "pdfRenderer.DocumentRenderer.RenderPage(gfx, i);" you can add code that draws your background image. This can be a page from a PDF file, drawn using DrawImage.
Also check this sample:
http://www.pdfsharp.net/wiki/XForms-sample.ashx

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 13, 2016 3:27 pm 
Offline

Joined: Tue Jan 12, 2016 12:17 pm
Posts: 9
Thanks for your answer, I'm gonna try it.

Thomas Hoevel wrote:
rbe42 wrote:
other pages of my doc
The image does not show.
I assume the first page has a larger header. It is up to you to reserve space for it (e.g. using SpaceBefore with the first paragraph on this page).


On the 2nd page, the header is different, it can be larger or smaller (in 90% of cases it's smaller).
However I don't have any new paragraph on the 2nd page (and the next pages), it's the next row of my table. Moreover, I don't know when the break page happens, migradoc add it automatically.


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 15, 2016 10:22 am 
Offline

Joined: Tue Jan 12, 2016 12:17 pm
Posts: 9
Hi, I'm still stuck when I try to render my document.

What I do:
Code:
            MemoryStream stream = new MemoryStream();

            PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer();
            pdfRenderer.Document = migraDoc; // Add my Document (1 section with a multi page table)

            pdfRenderer.PrepareRenderPages();

            int pages = pdfRenderer.DocumentRenderer.FormattedDocument.PageCount;
            for (int i = 1; i <= pages; ++i)
            {
                PdfPage page = pdfRenderer.PdfDocument.AddPage();

                PageInfo pageInfo = pdfRenderer.DocumentRenderer.FormattedDocument.GetPageInfo(i); // A4
                page.Width = pageInfo.Width;
                page.Height = pageInfo.Height;
                page.Orientation = pageInfo.Orientation;

                using (XGraphics gfx = XGraphics.FromPdfPage(page))
                {
                    gfx.MUH = pdfRenderer.Unicode ? PdfFontEncoding.Unicode : PdfFontEncoding.WinAnsi;
                    DrawBackground(gfx); // Draw background on the XGraphics
                    pdfRenderer.DocumentRenderer.RenderPage(gfx, i);
                }
            }

            pdfRenderer.PdfDocument.Save(stream, false);
           
            return stream;


What I have:http://img15.hostingpics.net/pics/523250Capture1.jpg
Image

What I want:
Extend my background on all the page. If I print my background alone, it extends on all the page.

Thanks


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 15, 2016 12:46 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
rbe42 wrote:
What I want:
Extend my background on all the page. If I print my background alone, it extends on all the page.
I'm afraid I need some explanation what I see on the picture and what is wrong with it.

When people show just code snippets here, then in most cases the error is in the code they do not show.

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


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 15, 2016 2:51 pm 
Offline

Joined: Tue Jan 12, 2016 12:17 pm
Posts: 9
I expect something like that
http://img15.hostingpics.net/pics/616766Capture2.jpg
Image

instead of:
http://img15.hostingpics.net/pics/315168Capture3.jpg
Image

I found, why my background was tiny. I forgot to set XGraphicsUnit.Millimeter on XGraphics constructor.
Code:
using (XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsUnit.Millimeter ))


but now my PDF look like this:
http://img15.hostingpics.net/pics/321815Capture4.jpg
Image

I don't undestand why my table disappeared instead of being "above" the background. Maybe the background "push", but how to fix it?


Top
 Profile  
Reply with quote  
PostPosted: Sat Jan 16, 2016 5:42 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
rbe42 wrote:
I found, why my background was tiny. I forgot to set XGraphicsUnit.Millimeter on XGraphics constructor.
I'm not sure MigraDoc will work correctly with that gfx setting.
Maybe first create a "Millimeter" gfx to draw the background, then create a default gfx and draw the table.

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


Top
 Profile  
Reply with quote  
PostPosted: Mon Jan 18, 2016 12:22 pm 
Offline

Joined: Tue Jan 12, 2016 12:17 pm
Posts: 9
Thanks for the hint =)

I drew on the XGraphics with point instead of millimeters, and it worked well.

Here the working result:
Code:
            MemoryStream stream = new MemoryStream();

            PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer();
            pdfRenderer.Document = migraDoc; // Add my Document (1 section with a multi page table)

            pdfRenderer.PrepareRenderPages();

            int pages = pdfRenderer.DocumentRenderer.FormattedDocument.PageCount;
            for (int i = 1; i <= pages; ++i)
            {
                PdfPage page = pdfRenderer.PdfDocument.AddPage();

                PageInfo pageInfo = pdfRenderer.DocumentRenderer.FormattedDocument.GetPageInfo(i);
                page.Width = pageInfo.Width;
                page.Height = pageInfo.Height;
                page.Orientation = pageInfo.Orientation;


                using (XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsUnit.Point))
                {
                    gfx.MUH = pdfRenderer.Unicode ? PdfFontEncoding.Unicode : PdfFontEncoding.WinAnsi;
                    DrawBackground(gfx); // Drawing background method with stuff like gfx.DrawString(xElement.Value, font, color, x.Point, y.Point, alignment);
                    pdfRenderer.DocumentRenderer.RenderPage(gfx, i);
                }
            }

            pdfRenderer.PdfDocument.Save(stream, false);
            return stream;


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 02, 2016 11:03 am 
Offline

Joined: Wed Nov 02, 2016 10:55 am
Posts: 1
Hi rbe42,

In your original post you mentioned a piece of code like below:
Code:
PdfDocument pdfDoc = new PdfDocument();
PdfPage pdfPage = pdfDoc.AddPage();

pdfPage.Size = PdfSharp.PageSize.A4;

XGraphics gfx = XGraphics.FromPdfPage(pdfPage, XGraphicsUnit.Millimeter);

DrawSvgElement(gfx, SvgElement); // I draw a Svg Element with XGraphics method.


I tried using the DrawSvgElement method but it appears that this is not a valid method, is it one you created yourself? If so, could you share your code as this would be very useful for me. If this isn't custom code, do you know if the method name has changed in a more recent release and what I should use instead to achieve the same thing?

Thanks :)


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 03, 2023 3:50 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Thomas Hoevel wrote:
I assume the first page has a larger header. It is up to you to reserve space for it (e.g. using SpaceBefore with the first paragraph on this page).
As of MigraDoc 6.0.0, SpaceBefore is now ignored for the first paragraph on a page.
Version 6.0.0-preview-1 was released today.

To use that trick with version 6.0, insert a new empty dummy paragraph and then set SpaceBefore on the second paragraph, the one following the dummy paragraph.

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 135 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:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group