Formatted Rotated Text (singe line) in a Vertical Bar

I am looking for the Method or subclass for the programming in C# and Aspose PDF to do the the following.

Method Name: InsertSiloLabel

Method Description:

 The method creates a vertical bar (Silo) and places rotated text (90%) that is centered inside the silo 
 The text is composed of two chunks (text1 and text2) that are concatenated and centered inside the
 silo.   text1 and text2 have different font size and colors. 

Parameters (inputs):

01. inputPath           // The PDF input file 
02. outputPath         // The PDF output file 
// ---------------------------------
03. siloTop               //  where to start the vertical column (silo)  from top of document
04. siloLeft               //  where to start the vertical column (silo) from left of document
05. siloWidth            // The width of the column (sil)
06. siloHeight           // The height of the column 
07. siloGgColor        // the background fill color
08. siloFont              // The font

// ------------------------------------
09. text1 // 1 word
10. text1Color
11. text1Size
// ----------------------------------
12. text2 // 3 words of text
13. text2Color
14 .text2Size

Comments:

I am not sure how to pass all these inputs to the method.  The formatted text1 and text2 seem hard to accomplish int the Aspose.PDF

.

@Toberville

Thank you for contacting support.

You may achieve your requirements with TextStamp Class which exposes several methods and properties which can be used as per your requirements as explained in Adding Text Stamp in the PDF File.

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

Thanks Farhan,

Digging a little deeper,

How do I change the font colors within the textStamp. I want the first word to be lightgrey and the remaining words to be white. How do I extend the TextStamp so it has a specific height and width while not affecting the font size?

@Toberville

One of the constructors of TextStamp Class can take instance of FormattedText Class as parameter so you may apply several formatting styles as per your requirements. Moreover, changing the length and width of stamp will affect font size as it needs to contain text within that space.

OK, I will try the FormattedText Class paramater to get the text all tidy in the Text Stamp.

Is there anyway to paint a black box first (in the background) and then overlay the TextStamp on top of the black box?

I guest is there some type of z-index for layering stuff with Aspose.PDF

On another Notes, Is there a way to send an HTML formatted string into a text stamp?

Thanks,

@Toberville

You may set the background color of TextStamp with BackgroundColor property as the line of code below. Whereas, we are afraid that HTML formatted string may not be used for text stamp.

textStamp.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Black);

We hope this will be helpful.

test.pdf (54.1 KB)

Cheers Farhan,
Here is an example of what I am trying to create. Given a one page PDF (this one has a truck as a background image), I am looking for a Method that writes the black rectangle with text just like you see in the attached PDF).

Can this be done with Aspose.PDF? If so, then can you please provide me with the CODE BLOCK or METHOD.

I can not figure out how to program this very important requirement in Aspose.PDF

Thanks do much,

@Toberville

We are looking into your requirements and will get back to you soon.

@Toberville

Thank you for being patient.

You can meet your requirements as explained in the PDF document shared by you, with below code snippet that adds an image, then a FloatingBox to the page and later adds the text using Table approach. You may modify it further as per your requirements.

// Instantiate Document Object
Document doc = new Document();
// Add a page to pages collection of document
Page page = doc.Pages.Add();
// Load the source image file to Stream object
FileStream fs = new FileStream(dataDir + @"Chrome.PNG", FileMode.Open);
// Set margins so image will fit
page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
// Create an image object
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
// Add the image into paragraphs collection of the section
page.Paragraphs.Add(image1);
// Set the image file stream
image1.ImageStream = fs;

Aspose.Pdf.Table table = new Aspose.Pdf.Table();

// Create text fragment
TextFragment tf = new TextFragment("TEST ASPOSE");
tf.TextState.Font = FontRepository.FindFont("Arial"); ;
tf.TextState.FontSize = 9;
tf.TextState.LineSpacing = 2f;
tf.TextState.Rotation = 270;

FloatingBox floatingBox = new FloatingBox(30, (float)page.PageInfo.Height);
floatingBox.Left = page.PageInfo.Width - 80;
floatingBox.Top = 0;
floatingBox.BackgroundColor = Aspose.Pdf.Color.Yellow;

table.Left = (float)floatingBox.Left + 5;
table.Top = (float)((page.PageInfo.Height / 2) - (tf.Rectangle.Height / 2));

Aspose.Pdf.Row row = table.Rows.Add();
row.MinRowHeight = 200;
Aspose.Pdf.Cell cell = row.Cells.Add();
page.Paragraphs.Add(floatingBox);
cell.Paragraphs.Add(tf);
floatingBox.Paragraphs.Add(table);

// Save resultant PDF file
doc.Save(dataDir + "Sample_19.3.pdf");

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

Hello Farhan,

v-bar-for-departmenst.pdf (3.4 MB)

I tried the code snippet you provided and thank you. I need to extend the snippet a little and I can not find a solution. I have attached a PDF for what I am trying to create. Basically, I need to have two strings inside the FloatingBox.

Rotated and together.
Each a different color and font size.
Vertically and Horizontally centered (the 2 strings concatenated).
The text string may vary. For example, the departments could be "Accounting, Sales, Customer Support, etc…)

I tried playing around with nested tables, etc… but my skills fall short. Can you find a way to make this happen.

Thank you,

@Toberville

Thank you for your kind feedback.

Please note that a TextFragment contains several TextSegment(s) and a TextSegment contains several Characters so you may format the text on TextSegment level in order to apply different formatting. Rotating text and adding it to a floating box is explained already in previous code snippet.

// Instantiate Document object
Document doc = new Document();
// Create a page in the Pdf object
Page page = doc.Pages.Add();
TextFragment fragment = new TextFragment("");
TextSegment segment1 = new TextSegment("This is a sample TextFragment using ");
TextSegment segment2 = new TextSegment(" multiple TextSegments");
segment1.TextState.FontSize = 12;
segment1.TextState.ForegroundColor = Aspose.Pdf.Color.Red;
segment2.TextState.FontSize = 20;
segment2.TextState.ForegroundColor = Aspose.Pdf.Color.Blue;
fragment.Segments.Add(segment1);
fragment.Segments.Add(segment2);
page.Paragraphs.Add(fragment);
dataDir = dataDir + "Segments_19.5.pdf";
// Save PDF Document
doc.Save(dataDir);

Please feel free to get back to us if you need any further assistance.

v-bar-for-department.pdf (3.4 MB)
v-bar-for-department-out.pdf (3.4 MB)

Hello Farhan,

I would appreciate some of your expertise on this problem. I have attached a before and after PDFs for the result I am trying achieve. My Code is below. I have tried a few different techniques but I can not get the formatted text to display. I

    public void InsertVerticalTab()
    {
            Document doc = new Document(inputFile);
            Page page = doc.Pages[1];
            page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);      

            double pageWidth = page.GetPageRect(true).Width;     
            double pageHeight = page.GetPageRect(true).Height;    
         
            FloatingBox floatingBox = new FloatingBox();
            floatingBox.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.ColorTranslator.FromHtml("#3570c8")); 
            floatingBox.Margin = new MarginInfo(0, 0, 0, 0);
            floatingBox.Top = 74;
            floatingBox.Left = 420;
            floatingBox.Height = 536;
            floatingBox.Width = 58;  
            floatingBox.ZIndex = 100; 

            TextFragment fragment = new TextFragment("");

            TextSegment segment1 = new TextSegment("Department: ");
            segment1.TextState.Font = FontRepository.FindFont("Arial");
            segment1.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.ColorTranslator.FromHtml("#FFFF33"));
            segment1.TextState.FontSize = 18;
            segment1.TextState.FontStyle = FontStyles.Bold;
            segment1.TextState.FontStyle = FontStyles.Italic;

            TextSegment segment2 = new TextSegment("Engineering");
            segment2.TextState.Font = FontRepository.FindFont("Arial");
            segment2.TextState.ForegroundColor = Aspose.Pdf.Color.White;
            segment2.TextState.FontSize = 30;
            segment2.TextState.FontStyle = FontStyles.Bold;
            segment2.TextState.FontStyle = FontStyles.Italic;

            fragment.Segments.Add(segment1);
            fragment.Segments.Add(segment2);

            fragment.TextState.Rotation = 270;
            fragment.VerticalAlignment = VerticalAlignment.Center;

            Aspose.Pdf.Table table = new Aspose.Pdf.Table();
            Aspose.Pdf.Row row = table.Rows.Add(); 
            Aspose.Pdf.Cell cell = row.Cells.Add(); 

            cell.Paragraphs.Add(fragment);
            floatingBox.Paragraphs.Add(table);
            page.Paragraphs.Add(floatingBox);
            string outster = inputFile.Replace(".pdf", "-out.pdf");
            doc.Save( outster );
    }

@Toberville

We are investigating your requirements and will get back to you with our findings soon.

@Toberville

Thank you for being patient.

We have tried to enhance your code while setting MinRowHeight property for the row of Table. However, ideal output is still not generated so we have logged a ticket with ID PDFNET-46676 to further investigate if we can support some overload constructors of other classes to achieve your requirements even better. We will let you know as soon as some further update will be available in this regard.