Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Set Document Orientation (for All Pages) in MigraDoc Library?

I'm using MigraDoc to programatically generate a PDF file with text, images and tables.

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

So I tried the following.

document.DefaultPageSetup.Orientation = Orientation.Landscape;

But I get the following debug assertion error.

---------------------------
Assertion Failed: Abort=Quit, Retry=Debug, Ignore=Continue
---------------------------

DefaultPageSetup must not be modified

If I click Ignore, it goes through and the Orientation is indeed Landscape.

However, I want to make sure I am doing this the right way.

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

Here's the rest of the code (so 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;

Many thanks!
-Shiva

UPDATE:

SOLUTION: Here's the working code, based on Thomas' answer below (for the benefit of others who maybe 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...
like image 389
Shiva Avatar asked Mar 26 '14 22:03

Shiva


People also ask

How to get the DDL of a document in migradoc?

The DocumentPreview object receives the document as an MigraDoc DDL string. You can obtain this DDL string from your Document object using the DdlWriter class. view source print? 02. { 03. InitializeComponent (); 04. 05. // Create a new MigraDoc document 06. Document document = SampleDocuments.CreateSample1 (); 07.

How to change the orientation of a document in word?

How to Change the Orientation of a Document 1 To change the orientation of the whole document, select Layout > Orientation. 2 Choose Portrait or Landscape. See More....

Does changing the page orientation change the size of the report?

Note that in JR, changing the Page Orientation does not change the page size of your report design. The page orientation is just a hint used by some printers, to know the need to change the direction of printing.


1 Answers

Assign DefaultPageSetup.Clone() to the PageFormat of your section and modify that.

Then you modify a copy of the default settings and no assertion will fail.

With your approach, all documents would default to landscape - not just the document you set it for.

This answer applies to MigraDoc only as only MigraDoc uses DefaultPageSetup.

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

like image 102
I liked the old Stack Overflow Avatar answered Sep 21 '22 14:09

I liked the old Stack Overflow