Thursday, May 16, 2024
HomeC#Rotate Pages, Textual content, Tables, and Photos in a PDF Utilizing C#

Rotate Pages, Textual content, Tables, and Photos in a PDF Utilizing C#


The Syncfusion .NET PDF Library gives assist for rotating the weather in a PDF doc by setting the rotation angle of the weather.  

This text will cowl the method of rotating a web page, textual content, desk, and a picture in a PDF file utilizing the Syncfusion .NET PDF Library.

Agenda:

Getting began with app creation 

  1. First, create a brand new C# .NET console utility utilizing Visible Studio.Creating a Console App
  2. Then, set up the  Syncfusion.Pdf.Internet.Core  NuGet package deal as a reference in your .NET app from the  NuGet Gallery.Install Syncfusion.Pdf.Net.Core NuGet Package

Rotating a web page in a PDF doc

Rotating a PDF web page means altering the orientation of the web page to a specified diploma.

For instance, rotating a web page by 90 levels would flip it from portrait to panorama orientation or vice versa. This may be helpful when a web page has been scanned or saved in an inappropriate orientation or if you wish to view a web page from a unique perspective. The rotation may be completed both clockwise or counterclockwise. 

The next code instance exhibits rotate a web page in a PDF doc utilizing C#.

//Load PDF doc as a stream.  
FileStream inputStream = new FileStream("Enter.pdf", FileMode.Open, FileAccess.Learn, FileShare.ReadWrite); 
//Load the present PDF doc.  
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream); 
for (int i = 0; i < loadedDocument.PageCount; i++) 
{ 
    //Will get the web page. 
    PdfPageBase loadedPage = loadedDocument.Pages[i] as PdfPageBase; 
    //Set the rotation for the loaded web page.  
    loadedPage.Rotation = PdfPageRotateAngle.RotateAngle90; 
} 
//Create the file stream to avoid wasting the PDF doc.   
utilizing(FileStream fileStream = new FileStream("Output.pdf", FileMode.CreateNew, FileAccess.ReadWrite)) 
{ 
  loadedDocument.Save(fileStream); 
} 
//Shut the doc.  
loadedDocument.Shut(true);

By executing this code instance, you’ll get a PDF doc like within the following screenshot.

Rotating a page in a PDF document
Rotating a web page in a PDF doc

Rotating textual content in a PDF doc

Rotating PDF textual content refers to altering the orientation of the textual content from its default horizontal alignment to a vertical or diagonal alignment. This may be helpful in varied eventualities, akin to becoming extra textual content on a web page, enhancing the visible attraction of a doc, or making the textual content simpler to learn in sure circumstances. 

The next code instance exhibits rotate textual content in a PDF doc utilizing C#.

//Create a brand new PDF doc. 
PdfDocument pdfDocument = new PdfDocument();
//Add a web page to the PDF doc. 
PdfPage pdfPage = pdfDocument.Pages.Add();
//Create PDF graphics for the web page. 
PdfGraphics graphics = pdfPage.Graphics;
//Set the font. 
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Add watermark textual content. 
PdfGraphicsState state = graphics.Save();
graphics.RotateTransform(-40);
graphics.DrawString("Rotating a textual content in PDF doc", font, PdfPens.Pink, PdfBrushes.Pink, new PointF(-150, 450));
//Create the file stream to avoid wasting the PDF doc.  
utilizing (FileStream outputStream = new FileStream("Output.pdf", FileMode.CreateNew, FileAccess.ReadWrite))
{
    pdfDocument.Save(outputStream);
}
//Shut the doc. 
pdfDocument.Shut(true);

By executing this code, you’ll get a PDF like within the following screenshot.

Rotating text in a PDF document
Rotating textual content in a PDF doc

Rotating a picture in a PDF doc

We are able to additionally rotate a picture in a PDF utilizing the Syncfusion .NET PDF Library. This entails altering the orientation of the picture embedded within the doc to a specified diploma.

For instance, rotating a picture by 90 levels turns it from portrait to panorama orientation or vice versa. The rotation may be completed both clockwise or counterclockwise. 

The next code instance exhibits rotate a picture in a PDF doc utilizing C#.

//Created the PDF doc. 
PdfDocument doc = new PdfDocument();
//Add a brand new web page. 
PdfPage web page = doc.Pages.Add();
FileStream imageStream = new FileStream("Enter.png", FileMode.Open, FileAccess.Learn, FileShare.ReadWrite);
//Load a bitmap. 
PdfBitmap picture = new PdfBitmap(imageStream);
//Save the present graphics state. 
PdfGraphicsState state = web page.Graphics.Save();
//Rotate the coordinate system. 
web page.Graphics.RotateTransform(-40);
//Draw picture. 
picture.Draw(web page, -150, 450);
//Restore the graphics state. 
web page.Graphics.Restore(state);
//Create the file stream to avoid wasting the PDF doc.  
utilizing (FileStream fileStream = new FileStream("Output.pdf", FileMode.CreateNew, FileAccess.ReadWrite))
{
    doc.Save(fileStream);
}
//Shut the doc. 
doc.Shut(true);

By executing this code instance, we’ll get a PDF doc like within the following screenshot.

Rotating an image in a PDF document
Rotating a picture in a PDF doc

Rotating a desk in a PDF doc

By rotating a desk in a PDF, we are able to change the orientation from its unique place to a unique angle. This may be helpful when the desk’s unique orientation just isn’t handy for viewing or when the desk’s information must be introduced in a unique orientation. 

The next code instance exhibits rotate a desk in a PDF doc utilizing C#.

//Create a brand new PDF doc.  
PdfDocument doc = new PdfDocument(); 
//Add a web page.  
PdfPage web page = doc.Pages.Add(); 
//Create a PdfGrid.  
PdfGrid pdfGrid = new PdfGrid(); 
//Add a handler to rotate the desk.  
pdfGrid.BeginPageLayout += PdfGrid_BeginPageLayout; 
//Create a DataTable.  
DataTable dataTable = new DataTable(); 
//Add columns to the DataTable.  
dataTable.Columns.Add("ID", typeof(int)); 
dataTable.Columns.Add("Title", typeof(string)); 
//Add rows to the DataTable.  
for (int i = 0; i < 10; i++) 
{ 
    dataTable.Rows.Add(57, "AAA"); 
    dataTable.Rows.Add(130, "BBB"); 
} 
//Assign information supply.  
pdfGrid.DataSource = dataTable; 
//Set a repeat header for the desk.   
pdfGrid.RepeatHeader = true; 
//Draw a grid to the web page of a PDF doc.  
pdfGrid.Draw(web page, new RectangleF(100, 20, 0, web page.GetClientSize().Width)); 
//Create the file stream to avoid wasting the PDF doc.   
utilizing (FileStream fileStream = new FileStream("Output.pdf", FileMode.CreateNew, FileAccess.ReadWrite)) 
{ 
    doc.Save(fileStream); 
} 
//Shut the doc.  
doc.Shut(true); 
void PdfGrid_BeginPageLayout(object sender, Syncfusion.Pdf.Graphics.BeginPageLayoutEventArgs e) 
{ 
    PdfPage web page = e.Web page; 
    PdfGraphics graphics = e.Web page.Graphics; 
    //Translate the coordinate system to the required place.  
    graphics.TranslateTransform(web page.GetClientSize().Width, 0); 
    //Rotate the coordinate system.  
    graphics.RotateTransform(90); 
}

By executing this code instance, we’ll get a PDF doc like within the following screenshot.

Rotating a table in a PDF
Rotating a desk in a PDF

GitHub reference

For extra particulars, try the examples within the GitHub repository.

Conclusion

Thanks for studying! On this weblog, we’ve got discovered rotate pages, textual content, tables, and pictures in a PDF utilizing the Syncfusion .NET PDF Library. Check out these strategies and go away your suggestions within the feedback part of this weblog put up!

Take a second to take a look at the documentation, the place you can see different choices and options, all with accompanying code samples.

For present prospects, the brand new model of Important Studio is offered for obtain from the License and Downloads web page. In case you are not but a Syncfusion buyer, you’ll be able to strive our 30-day free trial to take a look at our accessible options.

For questions, you’ll be able to contact us by our assist discussion board, assist portal, or suggestions portal. We’re delighted to help you!

Associated blogs

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments