Hello, I am looking for some help with a small function. Given a one page Word Document (A Book Cover) I want to paint a vertical bar on the far right of the page from top bottom with text that is rotated and centered.
This is a C# function and takes the inputs
width = width of bar
Text = The text that is in the bar (the rotated part)
from top = gutter amount from top (points). I need a little space above bar
from bottom = gutter amount from top (points). I need a little space below bar
from right = gutter amount from right (points). I need a little space to the right of the bar and right side of page.
Thanks for your inquiry. Please use the following code example to get the desired output. Hope this helps you.
If you still face problem, please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.
Document doc = new Document();
Aspose.Words.Drawing.Shape watermark = new Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.Rectangle);
watermark.Width = 200;
watermark.Height = doc.FirstSection.PageSetup.PageHeight;
watermark.Fill.Color = Color.Gray;
watermark.StrokeColor = Color.Gray;
// Place the watermark in the page center.
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
watermark.WrapType = WrapType.None;
watermark.VerticalAlignment = VerticalAlignment.Top;
watermark.HorizontalAlignment = HorizontalAlignment.Right;
doc.FirstSection.Body.FirstParagraph.AppendChild(watermark);
Paragraph text = new Paragraph(doc);
text.AppendChild(new Run(doc, "Book Cover Text"));
text.Runs[0].Font.Size = 25;
watermark.AppendChild(text);
watermark.TextBox.LayoutFlow = LayoutFlow.TopToBottom;
doc.Save(MyDir + "output.docx");
Thank You Tahir,
This really helps a lot, but I am looking for something a little different and I can’t get the result. I have attached a pdf file as an example:
Great! The logic is what I need unfortunately I am using classic ASP with a ComWrapper. I need to take what you have above and transform into a ComWrapper. Below is my feeble attempt that fails. I know my programming is way off for a ComWrapper. Doing the best I can. Can you help me?
public void BookCover_InsertStepTab( object docSource, object b )
{
DocumentBuilder builder = (DocumentBuilder)b;
Document d = (Document)docSource;
I have the placement and display of the rectangle. All good! For some reason the displaying of the text is causing problems. I think this section may cause problems:
If I comment out the area above, it seems to work. In fact, I can not get the text to display. Can you please review this section and give me ideas on how to correct. Is there a better technique?
What is a Run anyway?
Also, the black rectangle has a border. Any ideas why?
You can change the border color of shape by using Shape.StrokeColor.
All text of the document is stored in runs of text. Run can only be a child of Paragraph. Run class represents a run of characters with the same font formatting.
Please ZIP and attach your problematic and expected output Word documents here for our reference. We will then provide you more information about your query.
Thanks for the help, I solved the problem by designating the Shape as a TextBox instead of a Rectangle. I have another question that i hope you can help.
I want to create a TextBox with formatted text. I mean some paragraphs, some word phrases that are italized and/or bold and some font size changes.
How do I do that? How do I do that in my COM Wrapper. I think first to initiate a shape :
Thanks for your inquiry. All text of the document is stored in runs of text and Run node can only be a child of Paragraph node. You can use Run.Font property to format the text. Please read the following article. Inserting a String of Text
In your case, you need to add paragraph node into text box (Shape node) and add run nodes into the paragraph.
Fantastic. I will get working on this and place with stuffing formatted text into a TextBox… I am looking for your guidance for some ideas on how to proceed.
One option I am considering is to pass an HTML string to the text box. Will the HTML work for font changes and font size?
Another option, is to send a coded string and parse he string and building the Run. This sounds like a lot of work.
Thirdly is the issue of storage. Can Aspose Word read an XML file instead of passing a string; there upon I would execute #1 or #2 above.
There is another idea I have. Can I initiated a Word document without actually reading one first I mean, can I designate a file name, set the Word Document size, build the document, (This is a one page document) and save the document to file.
Once you have inserted the image into document, you can work with image using Shape.ImageData property. This property provides access to the image of the shape. Returns null if the shape cannot have an image.
The ImageData.ImageSize property returns the information about image size and resolution. The ImageData.ImageType property returns the type of the image.
Aspose.Words does not provide API to read or parse XML. However, you can read the XML by using .NET API (DataSet.ReadXml method) and perform mail merge operation. We suggest you please read the following article. About Mail Merge How to Mail Merge from XML using DataSet
I have the COM Wrapper Method below. How do I determine and image size first (so I can calculate positioning) before I issue the builder.InsertImage. Please suggest alterations to code below.
DocumentBuilder builder = (DocumentBuilder)b;
Document d = (Document)docSource;
String photoPath = imgPath.ToString();
double top = 0.00;
double left = 0.00;
// *****************************************************
// * I need to determine image size HERE so
// * I can adjust the top and left properties before
// * the images is inserted below.
// ****************************************************
Shape coverPhoto = builder.InsertImage(photoPath,
RelativeHorizontalPosition.Page, left,
RelativeVerticalPosition.Page, top,
-1, -1,
WrapType.None );
You can not get the image’s size on document’s page without inserting it. Once you insert it into document, you can get its width and height using Shape.Width and Shape.Height properties. Please note that when you insert an image into document using DocumentBuilder.InsertImage method (String), it is scale to 100%.
Moreover, you can get the image’s size using .NET API as shown below.
If you still face problem, please ZIP and attach your input and expected output documents along with image here for our reference. We will then provide you more information about your query.
Great, I will get the file dimensions as recommended. I am using builder.InsertImage. How would I insert the image so it is centered within a rectangle? furthermore, if the image is too big then it would change scale to fit the rectangle. Always keeping the original aspect ratio. Anything with Aspose.Words? Do I need to use C#. Please provide a CODE SNIPPET.
Please ZIP and attach your input and expected output Word documents along with image here for our reference. We will then provide you code example according to your requirement.
Method Description: Draw a rectangular box from the input parameters (top, left, width, height) with a 2px border and transparent background. Place the input parameters photoPath inside the box that is centered horizontally and 20pts from top of the box border. If the photoPath is larger than the rectangular box then scale the photoPath it so it fits within the bounded rectangular box while maintaining the original aspect ratio of photoPath.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.