PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon May 06, 2024 11:48 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Sun May 24, 2015 3:00 pm 
Offline

Joined: Sun May 24, 2015 2:49 pm
Posts: 7
What I want to do feels like it should be simple, and therefore I feel like an idiot for being unable to find out how. Basically, I want to change the background colour of a page.

I have found that it's possible to change the background colour of a paragraph, but there are gaps between them AND it doesn't change the colour of the margin area.

Whenever I try to search for this, 90% of the results seem to be for formatting tables. I'm not trying to do anything with tables. It's just simple text on pages.

Is there any way I can do this through MigraDoc or PDFsharp? I'm creating my current document with MigraDoc, but I'm very interested in knowing if there's ANY way changing the background colour from its default colour of white.

Thanks in advance.


Top
 Profile  
Reply with quote  
PostPosted: Tue May 26, 2015 5:37 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 918
Location: CCAA
PDF pages do not have a colour AFAIK - they are transparent.

With PDFsharp you can draw a rectangle over the whole page.

With MigraDoc, you should be able to add a TextFrame as the first element of the header, give the TextFrame the size of the page and set its colour. Or run a simple PDFsharp loop that draws a rectangle on each page after creating the PDF file with MigraDoc.

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


Top
 Profile  
Reply with quote  
PostPosted: Tue May 26, 2015 9:42 pm 
Offline

Joined: Sun May 24, 2015 2:49 pm
Posts: 7
TH-Soft wrote:
PDF pages do not have a colour AFAIK - they are transparent.

With PDFsharp you can draw a rectangle over the whole page.

With MigraDoc, you should be able to add a TextFrame as the first element of the header, give the TextFrame the size of the page and set its colour. Or run a simple PDFsharp loop that draws a rectangle on each page after creating the PDF file with MigraDoc.


I've created some code that adds a TextFrame to the primary header of a section. This does appear to do what I want! Except for one curious thing; it appears to affect the header for EVERY section!? Not just the one I set it. Why does this happen? Is the Headers property somehow global? When I looked at the Headers property for subsequent sections, each section's respective Primary property has no TextFrame elements. Only the first one that I added two has it.

This is the code I'm using (which is also here for the convenience of anyone else trying to set the background of their MigraDoc document for every page):

Code:
MigraDoc.DocumentObjectModel.Document document = new MigraDoc.DocumentObjectModel.Document();

MigraDoc.DocumentObjectModel.Section section = document.AddSection();

MigraDoc.DocumentObjectModel.Shapes.TextFrame frame = section.Headers.Primary.AddTextFrame();

frame.FillFormat.Color = MigraDoc.DocumentObjectModel.Colors.Green;

//The setting of RelativeHorizontal and RelativeVertical to 'Page' ensures they are positioned absolutely.
//The absolute position means that the TextFrame will be in the very top-left corner of the page.
frame.RelativeHorizontal = MigraDoc.DocumentObjectModel.Shapes.RelativeHorizontal.Page;
frame.RelativeVertical = MigraDoc.DocumentObjectModel.Shapes.RelativeVertical.Page;
frame.Width = document.DefaultPageSetup.PageWidth;
frame.Height = document.DefaultPageSetup.PageHeight;


Top
 Profile  
Reply with quote  
PostPosted: Wed May 27, 2015 8:11 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3097
Location: Cologne, Germany
Sections inherit properties from the previous sections unless they are overridden.

If you need a header for the first section but do not want a header for the second section, set a new, empty header for section 2. Section 3 will have the same header as section 2 unless you make any changes.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Sat May 30, 2015 10:09 am 
Offline

Joined: Sun May 24, 2015 2:49 pm
Posts: 7
Thomas Hoevel wrote:
Sections inherit properties from the previous sections unless they are overridden.

If you need a header for the first section but do not want a header for the second section, set a new, empty header for section 2. Section 3 will have the same header as section 2 unless you make any changes.


This is useful to know, thanks.

Also I have discovered what appears to be a bug with how LineFormat property for the TextFrame is rendered. I was using it as a border around some text when I noticed that it didn't appear to quite a line up with what it was bordering. Some quick research found that issue was reported here in 2010: viewtopic.php?f=3&t=1346

It has evidently not been fixed (for reference I am working using WPF). I noticed that the content area of the frame that was sticking out on the bottom and right sides appeared to each be half the size of the line's width. I did my best to try and dig into this and find out what was happening and will try and explain things based on what I've observed.

When the LineFormat property is rendered, I believe it is drawn as a rectangle. When rendered, its (0,0) coordinates are treated as being in the centre of it. However, the code used to position the line treats it as though the line's (0,0) coordinates will be its top left. Thus when the line is eventually rendered around the content area, it will be mis-aligned by half the width of the line, as you can see in the image in the thread linked above.

In investigating this, I encountered the RenderLine() method in ShapeRenderer.cs, which when calculating the position of the line, treats it as if it's rendering with the consideration that the line's (0,0) coordinates are at the top-left of it (and thus the full width of the line is subtracted from the height and width of the content area with the presumed intention being that the line will render inside of the content area on the right and bottom sides, but align with the edge of the content area so that the content area doesn't poke out).

To correct this, I modified the RenderLine() method in ShapeRenderer.cs and changed

Code:
XUnit width = contentArea.Width - lineWidth;
XUnit height = contentArea.Height - lineWidth;


To be:

Code:
XUnit width = contentArea.Width - (lineWidth/2);
XUnit height = contentArea.Height - (lineWidth/2);


Thus making the fixed method look like the following:

Code:
protected void RenderLine()
{
    Area contentArea = this.renderInfo.LayoutInfo.ContentArea;
    XUnit lineWidth = this.lineFormatRenderer.GetWidth();
    XUnit width = contentArea.Width - (lineWidth/2);
    XUnit height = contentArea.Height - (lineWidth/2);
    this.lineFormatRenderer.Render(contentArea.X, contentArea.Y, width, height);
}


Now I don't know if this is the correct way to fix what's wrong here; maybe the line is supposed to be rendering with its (0,0) coordinates being considered as its top-left rather than its centre, in which case it's not this area of the code that needs changing, but something else to do with the actual rendering of the line.

However, for now this correction serves my purposes, and I leave the code above for anyone else who happens to encounter this problem, assuming it continues to go without an official fix being released.


Top
 Profile  
Reply with quote  
PostPosted: Mon Jun 01, 2015 7:38 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3097
Location: Cologne, Germany
EnvelopeExcursion wrote:
It has evidently not been fixed (for reference I am working using WPF).
The bug is fixed. I don't recall if the bug fix is included with the current 1.50 beta, but it is fixed.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Jun 01, 2015 10:43 pm 
Offline

Joined: Sun May 24, 2015 2:49 pm
Posts: 7
Thomas Hoevel wrote:
EnvelopeExcursion wrote:
It has evidently not been fixed (for reference I am working using WPF).
The bug is fixed. I don't recall if the bug fix is included with the current 1.50 beta, but it is fixed.


I'm currently using version 1.32, which appears to be the latest stable version available for download from your Sourceforge and CodePlex pages.


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 02, 2015 9:37 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3097
Location: Cologne, Germany
IMHO there is no reason not to use MigraDoc 1.50 beta. It's on NuGet, source code was not published yet.

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 68 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