Knowledgebase (2344)
Children categories
Reset the size and position for the shape on presentation slides in C#
2017-01-04 07:40:20 Written by KoohjiEvery time when we change the slide's size, we need to reset the size and position for the shape to ensure the shape on the slide shows orderly. This tutorial will focus on demonstrating how to reset the size and position for the shape in C#.
Firstly, please view the original shape:

Code snippet of how to set the size and position of the shape:
Step 1: Create a new presentation document and load the document from file.
Presentation presentation = new Presentation();
presentation.LoadFromFile("Sample.pptx");
Step 2: Define the original slide size.
float currentHeight = presentation.SlideSize.Size.Height; float currentWidth = presentation.SlideSize.Size.Width;
Step 3: Change the slide size as A3.
presentation.SlideSize.Type = SlideSizeType.A3;
Step 4: Define the new slide size.
float newHeight = presentation.SlideSize.Size.Height; float newWidth = presentation.SlideSize.Size.Width;
Step 5: Define the ratio from the old and new slide size.
float ratioHeight = newHeight / currentHeight; float ratioWidth = newWidth / currentWidth;
Step 6: Reset the size and position of the shape on the slide.
foreach (ISlide slide in presentation.Slides)
{
foreach (IShape shape in slide.Shapes)
{
//Reset the shape size
shape.Height = shape.Height * ratioHeight;
shape.Width = shape.Width * ratioWidth;
//Reset the shape position
shape.Left = shape.Left * ratioHeight;
shape.Top = shape.Top * ratioWidth;
}
Step 7: Save the document to file.
presentation.SaveToFile("resize.pptx", FileFormat.Pptx2010);
Effective screenshot after reset the size and position of the shape on the slide:

Full codes:
using Spire.Presentation;
namespace ResetSizeandPosition
{
class Program
{
static void Main(string[] args)
{
{
Presentation presentation = new Presentation();
presentation.LoadFromFile("Sample.pptx");
float currentHeight = presentation.SlideSize.Size.Height;
float currentWidth = presentation.SlideSize.Size.Width;
presentation.SlideSize.Type = SlideSizeType.A3;
float newHeight = presentation.SlideSize.Size.Height;
float newWidth = presentation.SlideSize.Size.Width;
float ratioHeight = newHeight / currentHeight;
float ratioWidth = newWidth / currentWidth;
foreach (ISlide slide in presentation.Slides)
{
foreach (IShape shape in slide.Shapes)
{
shape.Height = shape.Height * ratioHeight;
shape.Width = shape.Width * ratioWidth;
shape.Left = shape.Left * ratioHeight;
shape.Top = shape.Top * ratioWidth;
}
presentation.SaveToFile("resize.pptx", FileFormat.Pptx2010);
}
}
}
}
}
The GoToE (or embedded go-to) action is similar to a remote go-to action but allows jumping to a PDF file that is embedded in another PDF file. With Spire.PDF, it’s possible to create a GoToE action to direct from the current PDF file to the embedded PDF file. This article demonstrates how to accomplish this goal programmatically with C#.
At first, it's necessary to introduce you the parameters of public PdfEmbeddedGoToAction(string fileName, PdfDestination dest, bool newWindow);
Parameters:
- fileName - the name of the target file.
- dest - the destination inside the target file.
- newWindow - if true open the file in a new window, if false the current file is replaced by the new file.
Now Follow below Detail steps:
Step 1: Create a PDF file and add a page to it.
PdfDocument pdf = new PdfDocument(); PdfPageBase page = pdf.Pages.Add();
Step 2: Attach a PDF file to the newly created file.
PdfAttachment attachment = new PdfAttachment("New Zealand.pdf");
pdf.Attachments.Add(attachment);
Step 3: Draw text on the page.
string text = "Test embedded go-to action! Click this will open the attached PDF in a new window.";
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 13f));
float width = 490f;
float height = font.Height * 2.2f;
RectangleF rect = new RectangleF(0, 100, width, height);
page.Canvas.DrawString(text, font, PdfBrushes.Black, rect);
Step 4: Create an embedded go-to action (/GoToE) which allows jumping to the attached PDF file and open it in a new window at the 2nd page and 200% zoom factor.
PdfDestination dest = new PdfDestination(1, new PointF(0, 842), 2f); PdfEmbeddedGoToAction action = new PdfEmbeddedGoToAction(attachment.FileName, dest, true);
Step 5: Create an action annotation with the embedded go-to action and then add it to the page.
PdfActionAnnotation annotation = new PdfActionAnnotation(rect, action); (page as PdfNewPage).Annotations.Add(annotation);
Step 6: Save the document.
pdf.SaveToFile("result.pdf");
Effective Screenshot:

Full code:
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Annotations;
using Spire.Pdf.Attachments;
using Spire.Pdf.General;
using Spire.Pdf.Graphics;
namespace Create_A_GoToE_Action
{
class Program
{
static void Main(string[] args)
{
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();
PdfAttachment attachment = new PdfAttachment("New Zealand.pdf");
pdf.Attachments.Add(attachment);
string text = "Test embedded go-to action! Click this will open the attached PDF in a new window.";
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 13f));
float width = 490f;
float height = font.Height * 2.2f;
RectangleF rect = new RectangleF(0, 100, width, height);
page.Canvas.DrawString(text, font, PdfBrushes.Black, rect);
PdfDestination dest = new PdfDestination(1, new PointF(0, 842), 2f);
PdfEmbeddedGoToAction action = new PdfEmbeddedGoToAction(attachment.FileName, dest, true);
PdfActionAnnotation annotation = new PdfActionAnnotation(rect, action);
(page as PdfNewPage).Annotations.Add(annotation);
pdf.SaveToFile("result.pdf");
}
}
}
With Spire.Doc for .NET, developers can easily operate the word fields from code. We have already shown how to create an IF field and remove Custom Property Fields in C#. From Spire.Doc V5.8.33, our developers add a new event UpdateFields to handle the Ask Field. This article will focus on demonstrating how to update the ASK field on the word document in C#.
Firstly, please view the sample document with an Ask filed which will be updated later:

Step 1: Create a new instance of Spire.Doc.Document class and load the document from file.
Document doc = new Document();
doc.LoadFromFile("Sample.docx");
Step 2: Call UpdateFieldsHandler event to update the ASK field.
doc.UpdateFields += new UpdateFieldsHandler(doc_UpdateFields);
Step 3: Update the fields in the document.
doc.IsUpdateFields = true;
Step 4: Save the document to file.
doc.SaveToFile("output.docx", FileFormat.Docx);
The following doc_UpdateFields () method shows how to update the ask field:
private static void doc_UpdateFields(object sender, IFieldsEventArgs args)
{
if (args is AskFieldEventArgs)
{
AskFieldEventArgs askArgs = args as AskFieldEventArgs;
askArgs.ResponseText = "Female";
}
}
Effective screenshot after updating the Ask Field in C#:

Full codes:
using Spire.Doc;
using Spire.Doc.Fields;
namespace Askfield
{
class Program
{
public void Field()
{
Document doc = new Document();
doc.LoadFromFile("Sample.docx");
doc.UpdateFields += new UpdateFieldsHandler(doc_UpdateFields);
doc.IsUpdateFields = true;
doc.SaveToFile("output.docx", FileFormat.Docx);
}
private static void doc_UpdateFields(object sender, IFieldsEventArgs args)
{
if (args is AskFieldEventArgs)
{
AskFieldEventArgs askArgs = args as AskFieldEventArgs;
askArgs.ResponseText = "Female";
}
}
}
}