How to set the document orientation (for all pages) in the MigraDoc library?

I use MigraDoc to programmatically create a PDF file with text, images and tables.

I need to set the Document Orientation (for all pages) of a document object to Landscape .

So, I tried the following.

 document.DefaultPageSetup.Orientation = Orientation.Landscape; 

But I get the following debug approval error.

 --------------------------- Assertion Failed: Abort=Quit, Retry=Debug, Ignore=Continue --------------------------- DefaultPageSetup must not be modified 

If I click Ignore , it will pass, and Orientation really Landscape .

However, I want to make sure I'm doing it right.

So the question is, how do I set the document orientation for all pages in a Document using the MigraDoc library?

Here is the rest of the code (so that it helps you get the context)

 using System.Runtime.Remoting.Messaging; using MigraDoc.DocumentObjectModel; namespace MyNamespace.PdfReports { class Documents { public static Document CreateDocument() { // Create a new MigraDoc document Document document = new Document(); document.Info.Title = "The Title"; document.Info.Subject = "The Subject"; document.Info.Author = "Shiva"; document.DefaultPageSetup.Orientation = Orientation.Landscape; 

Thank you very much! -Shiva

UPDATE:

SOLUTION: Here's a working code based on Thomas ' below (in the interest of others who may be looking for this solution).

 // Create a new MigraDoc document Document document = new Document(); //... //...... PageSetup pageSetup = document.DefaultPageSetup.Clone(); // set orientation pageSetup.Orientation = Orientation.Landscape; // ... set other page setting you want here... 
+6
source share
1 answer

Assign DefaultPageSetup.Clone() to the PageFormat your section and change this.

Then you change the copy of the default settings and no statement will be executed.

With your approach, all documents will have a landscape by default - not just the document for which you set it.

This answer only applies to MigraDoc , since only MigraDoc uses DefaultPageSetup .

See this post in the PDFsharp forum where Clone() used to create a copy of DefaultPageSetup :

+3
source

All Articles