We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Create a landscape letter size document

I am trying to create a landscape PDF document 11 X 8.5 using the following code. The resulting file is attached and size is 8.26 x 11.69



Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document();

pdfDocument.PageInfo.IsLandscape = true;
pdfDocument.PageInfo.Height = PageSize.PageLetter.Height;
pdfDocument.PageInfo.Width = PageSize.PageLetter.Width;

pdfDocument.Pages.Add();
string sLine = @“123456789012345678901234567890123456789012345678901234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz”;
sLine = @“REPORT: KL0142BC ------ GE CAPITAL ------ PAGE: 11”;

TextStamp textStamp = new TextStamp(sLine);

textStamp.YIndent = pdfDocument.PageInfo.Height - 200;
textStamp.XIndent = 0;
textStamp.TextState.FontSize = 10;
textStamp.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Regular;
textStamp.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont(@“Arial”);
pdfDocument.Pages[1].AddStamp(textStamp);
pdfDocument.Save(@“C:\landscape.pdf”);

Hi Thaddeus,


Thanks for contacting support.

In order to create PDF file with Landscape orientation, please try using following code snippet. For your reference, I have also attached the output generated over my end.

[C#]

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document();<o:p></o:p>

pdfDocument.Pages.Add();<o:p></o:p>

// pdfDocument.PageInfo.IsLandscape = true;<o:p></o:p>

Aspose.Pdf.Rectangle r = pdfDocument.Pages[1].MediaBox;<o:p></o:p>

double newHeight = r.Width;<o:p></o:p>

double newWidth = r.Height;<o:p></o:p>

double newLLX = r.LLX;<o:p></o:p>

//we must move page upper in order to compensate changing page size

//(lower edge of the page is 0,0 and information is usually placed from the top of the page.

//That’s why we move lower edge upper based on difference between old and new height.<o:p></o:p>

double newLLY = r.LLY + (r.Height - newHeight);<o:p></o:p>

pdfDocument.Pages[1].MediaBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);<o:p></o:p>

//sometimes we also need to set CropBox (if it was set in original file)<o:p></o:p>

pdfDocument.Pages[1].CropBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);<o:p></o:p>

pdfDocument.PageInfo.Height = Aspose.Pdf.PageSize.PageLetter.Height;<o:p></o:p>

pdfDocument.PageInfo.Width = Aspose.Pdf.PageSize.PageLetter.Width;<o:p></o:p>

string sLine =@“123456789012345678901234567890123456789012345678901234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz”;<o:p></o:p>

sLine = @“REPORT: KL0142BC ------ GE CAPITAL ------ PAGE: 11”;<o:p></o:p>

TextStamp textStamp = new TextStamp(sLine);<o:p></o:p>

textStamp.YIndent = 50;// pdfDocument.PageInfo.Height - 200;<o:p></o:p>

textStamp.XIndent = 10;<o:p></o:p>

textStamp.TextState.FontSize = 10;<o:p></o:p>

textStamp.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Regular;<o:p></o:p>

textStamp.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont(@“Arial”);<o:p></o:p>

pdfDocument.Pages[1].AddStamp(textStamp);<o:p></o:p>

pdfDocument.Save(@“C:\pdftest\landscape.pdf”);

Given the sample code above I still don’t understand how to determine the textStamp.YIndent so I can print the first line on the top of the page.

oddity

in the sample code the following line does not correctly assign the lly value to MediaBox?

pdfDocument.Pages[1].MediaBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);

I even tried changing the line of code to the following
pdfDocument.Pages[1].MediaBox = new Aspose.Pdf.Rectangle(10, 10, 200, 200);

after this assignment statement did not change the value of pdfDocument.Pages[1].MediaBox?

I stepped in the debugger to examine this?


Thaddeus:
Given the sample code above I still don’t understand how to determine the textStamp.YIndent so I can print the first line on the top of the page.
Hi Thaddeus,

Thanks for sharing the details.

In order to render the Stamp object on top of page, please try setting YIndent value equal to page height. Furthermore when converting Portrait document to Landscape, we may come across a scenario where we may cut some contents of the document (because we decrease height), so in order to avoid such problems, you can consider increasing width proportionally and leave height intact. See following highlighted code lines.

[C#]

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document();<o:p></o:p>

pdfDocument.Pages.Add();

// pdfDocument.PageInfo.IsLandscape = true;

Aspose.Pdf.Rectangle r = pdfDocument.Pages[1].MediaBox;

//new height the same

double newHeight = r.Height;

//new width is expanded proportionally to make orientation landscape

//(we assume that previous orientation is portrait)

double newWidth = r.Height * r.Height / r.Width;

// double newHeight = r.Width;

// double newWidth = r.Height;

double newLLX = r.LLX;

//we must to move page upper in order to compensate changing page size

//(lower edge of the page is 0,0 and information is usually placed from the top of the page.

//That's why we move lover edge upper on difference between old and new height.

double newLLY = r.LLY + (r.Height - newHeight);

pdfDocument.Pages[1].MediaBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);

//sometimes we also need to set CropBox (if it was set in original file)

pdfDocument.Pages[1].CropBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);

pdfDocument.PageInfo.Height = Aspose.Pdf.PageSize.PageLetter.Height;

pdfDocument.PageInfo.Width = Aspose.Pdf.PageSize.PageLetter.Width;

string sLine = @"123456789012345678901234567890123456789012345678901234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz";

// sLine = @"REPORT: KL0142BC ------ GE CAPITAL ------ PAGE: 11";

TextStamp textStamp = new TextStamp(sLine);

textStamp.YIndent = pdfDocument.PageInfo.Height - 10;

textStamp.XIndent = 10;

textStamp.TextState.FontSize = 10;

textStamp.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Regular;

textStamp.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont(@"Arial");

pdfDocument.Pages[1].AddStamp(textStamp);

pdfDocument.Save(@"C:\pdftest\landscape.pdf");

Thaddeus:
oddity

in the sample code the following line does not correctly assign the lly value to MediaBox?

pdfDocument.Pages[1].MediaBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);

I even tried changing the line of code to the following
pdfDocument.Pages[1].MediaBox = new Aspose.Pdf.Rectangle(10, 10, 200, 200);

after this assignment statement did not change the value of pdfDocument.Pages[1].MediaBox?

I stepped in the debugger to examine this?


Hi Thaddeus,

I have tested the scenario and have observed that MediaBox dimensions are changed when passing different values to Rectangle object. However another option can be to use PdfPageEditor class of Aspose.Pdf.Facades namespace.

[C#]

Document doc = new Document(“PdfWithText.pdf”);<o:p></o:p>

Rectangle r = doc.Pages[1].Rect;

PdfPageEditor ppe = new PdfPageEditor();

ppe.BindPdf("PdfWithText.pdf");

ppe.Zoom = (float) (r.Width / r.Height);

ppe.PageSize = new PageSize((float)r.Height, (float)r.Width);

ppe.Save("36115-1.pdf");

using the sample code the document size ends up being 16.55X11.69?

I am still confused by this comment?
//(we assume that previous orientation is portrait)

is there a constructor for a pdf document where I can define the orientation and size?

[C#]

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document();

pdfDocument.Pages.Add();

// pdfDocument.PageInfo.IsLandscape = true;

Aspose.Pdf.Rectangle r = pdfDocument.Pages[1].MediaBox;

//new height the same

double newHeight = r.Height;

//new width is expanded proportionally to make orientation landscape

//(we assume that previous orientation is portrait)

double newWidth = r.Height * r.Height / r.Width;

Hi Thaddeus,


Thanks for sharing the details.

We do not have any property which can change page orientation but you may consider adjusting page dimensions to change page orientation from Landscape to Portrait and vice versa. Furthermore, during my testing with following code snippet (as shared earlier), I am able to see that document is properly being converted from Portrait to Landscape.

[C#]

Document doc = new Document(@“C:\pdftest\landscape\landscape.pdf”);<o:p></o:p>

Aspose.Pdf.Rectangle r = doc.Pages[1].Rect;

Aspose.Pdf.Facades.PdfPageEditor ppe = new Aspose.Pdf.Facades.PdfPageEditor();

ppe.BindPdf(@"C:\pdftest\landscape\landscape.pdf");

ppe.Zoom = (float)(r.Width / r.Height);

ppe.PageSize = new PageSize((float)r.Height, (float)r.Width);

ppe.Save(@“C:\pdftest\landscape\landscape_36115-1.pdf”);

I think we have a misunderstanding of my problem. I do not have an existing portrait pdf that I want to change to landscape. I am creating a NEW pdf document. I would like this pdf document to be landscape 11x8.5.


The previous example code requires an existing document.




Hi Thaddeus,


Thanks for sharing the details.

In order to create PDF document from scratch, you may consider using blank constructor of Document() object without providing input file name as argument and create PDF file in desired dimensions.

[C#]

Document doc = new Document();<o:p></o:p>

doc.Pages.Add();

Aspose.Pdf.Rectangle r = doc.Pages[1].Rect;<o:p></o:p>

Aspose.Pdf.Facades.PdfPageEditor ppe = new Aspose.Pdf.Facades.PdfPageEditor();<o:p></o:p>

ppe.BindPdf(@“C:\pdftest\landscape\landscape.pdf”);<o:p></o:p>

ppe.Zoom = (float)(r.Width / r.Height);<o:p></o:p>

ppe.PageSize = new PageSize((float)r.Height, (float)r.Width);<o:p></o:p>

ppe.Save(@“C:\pdftest\landscape\landscape_36115-1.pdf”);

ppe.BindPdf(@“C:\pdftest\landscape\landscape.pdf”);


Where do I get this file? I get file not found when I execute this code sample?
I am getting closer, I now can create landscape documents...

but the following code produces the same size PDF file regardless of the sizes I set in the PageInfo object.

static void Main(string[] args)
{
NewPdf(@"C:\z-LetterLandscape.pdf", PageSize.PageLetter.Width, PageSize.PageLetter.Height, true);
NewPdf(@"C:\z-LegalLandscape.pdf", PageSize.PageLegal.Width, PageSize.PageLegal.Height, true);
NewPdf(@"C:\z-11X17Landscape.pdf", PageSize.P11x17.Width, PageSize.P11x17.Height, true);
}

static void NewPdf(string outputFilename, double desiredWidth, double desiredHeight, bool Landscape)
{
PageInfo pi = new PageInfo();
pi.Width = desiredWidth;
pi.Height = desiredHeight;

Document doc = new Document();
doc.PageInfo = pi;

doc.Pages.Add();
doc.Pages[1].PageInfo = pi;
if (Landscape) doc.Pages[1].Rotate = Rotation.on90;

doc.Save(outputFilename);
}

Thaddeus:
ppe.BindPdf(@“C:\pdftest\landscape\landscape.pdf”);

Where do I get this file? I get file not found when I execute this code sample?
Hi Thaddeus,

Thanks for contacting support.

The Landscape.pdf is an existing PDF file which which dimensions need to be changed. You can pass your own PDF file and in case you are creating PDF file from scratch, please try using following code snippet.

[C#]

Document doc = new
Document();<o:p></o:p>

doc.Pages.Add();

Aspose.Pdf.Rectangle r = doc.Pages[1].Rect;

MemoryStream ms = new MemoryStream();

doc.Save(ms);

Aspose.Pdf.Facades.PdfPageEditor ppe = new Aspose.Pdf.Facades.PdfPageEditor();

ppe.BindPdf(ms);

ppe.Zoom = (float)(r.Width / r.Height);

ppe.PageSize = new Aspose.Pdf.PageSize((float)r.Height, (float)r.Width);

ppe.Save(@"C:\pdftest\landscape_36115-1.pdf");

ms.Close();

the document size is not letter. The results of your last sample returns a document

11.69 X 8.26.

My last sample code returns the same page size for letter, legal and 11x17.

When creating a New pdf document how do you initialize the douments page size and orientation.

My company has an account with priority support, should I enter this problem in a priority ticket. I really need to get this resolved for one our customers.

Thaddeus:
the document size is not letter. The results of your last sample returns a document
11.69 X 8.26.

My last sample code returns the same page size for letter, legal and 11x17.
Hi Thaddeus,

Thanks for sharing the details.

When using earlier shared code snippet, the dimensions of page are changed so that it appears as Landscape and as you are aware, for landscape page, the width of page is greater than height. Also please note that in order to set page size as Letter (8.50 × 11.00 inches) and in Aspose.Pdf, 1 inch = 72 points, so Letter dimensions in points will be (612, 792) points.

Thaddeus:
My company has an account with priority support, should I enter this problem in a priority ticket. I really need to get this resolved for one our customers.
You can post queries in Priority Support forum anytime. However usually customers raise the issues/bugs in Priority Support forum in order to expedite the investigation/resolution of their reported problems.

" so Letter dimensions in points will be (612, 792) points."


How is this accomplished in your previous example?

Hi Thaddeus,


Please take a look over following code snippet. When using (612, 792) as page dimensions, the output is Portrait Letter page and in case you need to have output in Landscape, you need to use (792, 612).

[C#]

Document doc = new
Document();<o:p></o:p>

doc.Pages.Add();

Aspose.Pdf.Rectangle r = doc.Pages[1].Rect;

MemoryStream ms = new MemoryStream();

doc.Save(ms);

Aspose.Pdf.Facades.PdfPageEditor ppe = new Aspose.Pdf.Facades.PdfPageEditor();

ppe.BindPdf(ms);

ppe.Zoom = (float)(r.Width / r.Height);

ppe.PageSize = new Aspose.Pdf.PageSize(612, 792);//, (float)r.Width);

ppe.Save(@"C:\pdftest\landscape_36115-1.pdf");

ms.Close();