Rotate text and images inside PDF using Aspose.PDF for .NET

Is it possible to rotate text and images while they are being added to the pdf dynamically?

Something like:

image.rotation = 90;

or text.roration = 180;

Thanks,

Hi,

Thank you for considering Aspose.

  1. Yes, it is possible, you can set the rotation angle property of the text. Please refer to:

Rotation in TextFragment class.

  1. Rotation of image is not supported. For a turn around you can write some code to rotate the image in .NET and then get that image.

Thanks.

Adeel Ahmad
Support Developer
Aspose Changsha Team

Hi,


CAN YOU MAKE IT DIAGONAL???

THANKS,
ROLLY

Hi Rolly,


Thanks for contacting support.

You can add TextStamp to PDF file and can rotate the Text inside PDF file. For more information, please visit Adding Text Stamp in the PDF File.

In the event of any further query, please feel free to contact.


You don't understanding my requirements. I'm sure others expecting the same too.
I want the TEXT/IMAGE/STAMP to be rotated on DIAGONAL position.
What you're class is dong only vertical and horizontal. Do you know your class? Have you tried using your class?

rollyhernando:

You don’t understanding my requirements. I’m sure others expecting the same too.
I want the TEXT/IMAGE/STAMP to be rotated on DIAGONAL position.
What you’re class is dong only vertical and horizontal. Do you know your class? Have you tried using your class?

Hi Rolly,

Thanks for sharing the details.

Please try using following code snippet to render diagonal TextStamp in PDF document.

[C#]

// Instantiate Document object<o:p></o:p>

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

// Add page to PDF document<o:p></o:p>

Page page = doc.Pages.Add();<o:p></o:p>

TextStamp stamp = new TextStamp(“Watermark String”);<o:p></o:p>

stamp.RotateAngle = 45;<o:p></o:p>

//set origin<o:p></o:p>

stamp.XIndent = 100;<o:p></o:p>

stamp.YIndent = 100;<o:p></o:p>

//set text properties<o:p></o:p>

stamp.TextState.Font = FontRepository.FindFont(“Arial”);<o:p></o:p>

stamp.TextState.FontSize = 16.0F;<o:p></o:p>

stamp.TextState.FontStyle = FontStyles.Bold;<o:p></o:p>

stamp.TextState.FontStyle = FontStyles.Italic;<o:p></o:p>

stamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Aqua);<o:p></o:p>

// Add TextStamp to page object<o:p></o:p>

page.AddStamp(stamp);<o:p></o:p>

// Set background of PDF page<o:p></o:p>

page.Background = Aspose.Pdf.Color.LightGray;<o:p></o:p>

// Save the PDF document<o:p></o:p>

doc.Save(“c:/pdftest/Diagnal_StampObject.pdf”);


In order to render image a diagonal in PDF file, please try using following code snippet.

[C#]

//
Instantiate Document object
<o:p></o:p>

Document doc = new Document();

// Add page to PDF document

Page page = doc.Pages.Add();

ImageStamp stamp = new ImageStamp("c:/pdftest/SearchResults.png");

stamp.RotateAngle = 45;

//set origin

stamp.XIndent = 100;

stamp.YIndent = 100;

//set text properties

// Add ImageStamp to page object

page.AddStamp(stamp);

// Set background of PDF page

page.Background = Aspose.Pdf.Color.LightGray;

// Save the PDF document

doc.Save("c:/pdftest/Image_Diagnal_StampObject.pdf");

Yeah! thanks it’s good. My mind was on the .Rotate not .RotateAngle but I tested it.

Now I have to work on what color so that I’ll show somewhat real watermark.
Till next time.

Thanks,
Rolly

Hi Rolly,


Thanks for the feedback.

We are glad to hear that your requirement is accomplished. Please continue using our API’s and in the event of any further query, please feel free to contact.

@rollyhernando

You can now rotate text inside PDF by setting Rotation property to TextParagraph and TextFragments objects. They expect the value of rotation angle specified in degrees and will work in document generating scenarios. Please consider using following code snippet with latest version of Aspose.PDF for .NET:

Rotate text Using TextFragment + TextBuilder

//open document 
Document pdfDocument = new Document(); 
//get particular page 
Page pdfPage = (Page)pdfDocument.Pages.Add();

//create text fragment
TextFragment textFragment1 = new TextFragment("main text");
textFragment1.Position = new Position(100, 600);

//set text properties
textFragment1.TextState.FontSize = 12;
textFragment1.TextState.Font = FontRepository.FindFont("TimesNewRoman");

//create rotated text fragment
TextFragment textFragment2 = new TextFragment("rotated text");
textFragment2.Position = new Position(200, 600);

//set text properties
textFragment2.TextState.FontSize = 12;
textFragment2.TextState.Font = FontRepository.FindFont("TimesNewRoman");
textFragment2.TextState.Rotation = 45;

//create rotated text fragment
TextFragment textFragment3 = new TextFragment("rotated text");
textFragment3.Position = new Position(300, 600);

//set text properties
textFragment3.TextState.FontSize = 12;
textFragment3.TextState.Font = FontRepository.FindFont("TimesNewRoman");
textFragment3.TextState.Rotation = 90;

// create TextBuilder object
TextBuilder textBuilder = new TextBuilder(pdfPage);
// append the text fragment to the PDF page
textBuilder.AppendText(textFragment1);
textBuilder.AppendText(textFragment2);
textBuilder.AppendText(textFragment3);
//save document
pdfDocument.Save("TextFragmentTests_Rotated1.pdf");

Rotate text using TextParagraph + TextBuilder (rotated fragments)

//open document 
Document pdfDocument = new Document(); 
//get particular page 
Page pdfPage = (Page)pdfDocument.Pages.Add();

TextParagraph paragraph = new TextParagraph();
paragraph.Position = new Position(200, 600);

//create text fragment
TextFragment textFragment1 = new TextFragment("rotated text");

//set text properties
textFragment1.TextState.FontSize = 12;
textFragment1.TextState.Font = FontRepository.FindFont("TimesNewRoman");
//set rotation
textFragment1.TextState.Rotation = 45;

//create text fragment
TextFragment textFragment2 = new TextFragment("main text");

//set text properties
textFragment2.TextState.FontSize = 12;
textFragment2.TextState.Font = FontRepository.FindFont("TimesNewRoman");

//create text fragment
TextFragment textFragment3 = new TextFragment("another rotated text");

//set text properties
textFragment3.TextState.FontSize = 12;
textFragment3.TextState.Font = FontRepository.FindFont("TimesNewRoman");
//set rotation
textFragment3.TextState.Rotation = -45;

// append the text fragments to the paragraph
paragraph.AppendLine(textFragment1);
paragraph.AppendLine(textFragment2);
paragraph.AppendLine(textFragment3);

// create TextBuilder object
TextBuilder textBuilder = new TextBuilder(pdfPage);
// append the text paragraph to the PDF page
textBuilder.AppendParagraph(paragraph);
//save document
pdfDocument.Save("TextFragmentTests_Rotated2.pdf");

Rotate text using TextFragment + Page.Paragraphs

1. Document pdfDocument = new Document(); 
//get particular page 
Page pdfPage = (Page)pdfDocument.Pages.Add();

//create text fragment
TextFragment textFragment1 = new TextFragment("main text");

//set text properties
textFragment1.TextState.FontSize = 12;
textFragment1.TextState.Font = FontRepository.FindFont("TimesNewRoman");

//create text fragment
TextFragment textFragment2 = new TextFragment("rotated text");

//set text properties
textFragment2.TextState.FontSize = 12;
textFragment2.TextState.Font = FontRepository.FindFont("TimesNewRoman");
//set rotation
textFragment2.TextState.Rotation = 315;

//create text fragment
TextFragment textFragment3 = new TextFragment("rotated text");

//set text properties
textFragment3.TextState.FontSize = 12;
textFragment3.TextState.Font = FontRepository.FindFont("TimesNewRoman");
//set rotation
textFragment3.TextState.Rotation = 270;

pdfPage.Paragraphs.Add(textFragment1);
pdfPage.Paragraphs.Add(textFragment2);
pdfPage.Paragraphs.Add(textFragment3);
//save document
pdfDocument.Save("TextFragmentTests_Rotated3.pdf");