Nayyer Shahbaz February 23, 2015one Comment

Determine License initialization, get page count before saving PDF, characters spacing for text elements, enhancements in TextStamp class with Aspose.Pdf for .NET 10.1.0

Determine License initialization, get page count before saving PDF, characters spacing for text elements, enhancements in TextStamp class with Aspose.Pdf for .NET 10.1.0

February 23, 2015
Share on FacebookTweet about this on TwitterShare on LinkedIn

Aspose.Pdf for .NET logo The new release version of Aspose.Pdf for .NET 10.1.0 has become available to offer some great new features. With every new release, the demanding features from our customers are made available and keeping the tradition alive, we have introduced some new features, as well as fixes for issues reported in earlier release versions.

Determine License initialization

When using the API in trial mode, you get the limitations to manipulate/add certain number of elements inside PDF file. The limitation includes adding/removing number of pages from PDF file, adding/manipulating Attachments or Annotations inside PDF file, conversion of only first 4 pages to Image format and so on. Also when using the API in trial mode (without using the license), a watermark text is added in resultant file and you also come across an error message stating that API is being used in evaluation mode. So before performing any operation, we can also check the license initialization and for this purpose, a static read-only property named IsLicensed is added in Document class.

// determine if the API is licensed
if (Document.IsLicensed)
{
    // print message that license is applied
    Console.WriteLine("Licensed");
}

We generate the PDF files on the fly and during PDF file creation, we may come across the requirement (creating Table Of Contents etc) to get page count of PDF file without saving the file over system or stream. So in order to cater this requirement, a method ProcessParagraphs(…) has been introduced in Document class. Please take a look over following code snippet which shoes the steps to get page count without saving the document. For further information, please visit Get Number of Pages in a PDF File

// instantiate Document instance
Document doc = new Document();
// add page to pages collection of PDF file
Page page = doc.Pages.Add();
// create loop instance
for (int i = 0; i < 300; i++)
    // add TextFragment to paragraphs collection of page object
    page.Paragraphs.Add(new TextFragment("Pages count test"));
// process the paragraphs in PDF file to get accurate page count
doc.ProcessParagraphs();
// print number of pages in document
Console.WriteLine("Number of pages in document = "+ doc.Pages.Count);

Specify character Spacing when adding Text

A text can be added inside paragraphs collection of PDF file using TextFragment instance or by using TextParagraph object and even you can stamp the text inside PDF by using TextStamp class. While adding the text, we may have a requirement to specify character spacing for text object. In order to accomplish this requirement, a new property named CharacterSpacing property has been introduced. Please take a look over following approaches to fulfill this requirement. For more information, please visit How to specify character Spacing when adding Text.

Using TextBuilder and TextFragment

// create Document instance
Document pdfDocument = new Document();
// add page to pages collection of Document
Page page = pdfDocument.Pages.Add();
// create TextBuilder instance
TextBuilder builder = new TextBuilder(pdfDocument.Pages[1]);
// create text fragment instance with sample contents
TextFragment wideFragment = new TextFragment("Text with increased character spacing");
wideFragment.TextState.ApplyChangesFrom(new TextState("Arial", 12));
// specify character spacing for TextFragment
wideFragment.TextState.CharacterSpacing = 2.0f;
// specify the position of TextFragment
wideFragment.Position = new Position(100, 650);
// append TextFragment to TextBuilder instance
builder.AppendText(wideFragment);
// save resultant PDF file
pdfDocument.Save("output.pdf");

Using TextParagraph and TextParagraph

// create Document instance
Document pdfDocument = new Document();
// add page to pages collection of Document
Page page = pdfDocument.Pages.Add();
// create TextBuilder instance
TextBuilder builder = new TextBuilder(pdfDocument.Pages[1]);
// instantiate TextParagraph instance
TextParagraph paragraph = new TextParagraph();
// create TextState instance to specify font name and size
TextState state = new TextState("Arial", 12);
// specify the character spacing
state.CharacterSpacing = 1.5f;
// append text to TextParagraph object
paragraph.AppendLine("This is paragraph with character spacing", state);
// specify the position for TextParagraph
paragraph.Position = new Position(100, 550);
// append TextParagraph to TextBuilder instance
builder.AppendParagraph(paragraph);
// save resultant PDF file
pdfDocument.Save("c:/pdftest/output.pdf");

Using TextStamp

// create Document instance
Document pdfDocument = new Document();
// add page to pages collection of Document
Page page = pdfDocument.Pages.Add();
// instantiate TextStamp instance with sample text
TextStamp stamp = new TextStamp("This is text stamp with character spacing");
// specify font name for Stamp object
stamp.TextState.Font = FontRepository.FindFont("Arial");
// specify Font size for TextStamp
stamp.TextState.FontSize = 12;
// specify character specing as 1f
stamp.TextState.CharacterSpacing = 1f;
// set the XIndent for Stamp
stamp.XIndent = 100;
// set the YIndent for Stamp
stamp.YIndent = 500;
// add textual stamp to page instance
stamp.Put(page);
// save resultant PDF file
pdfDocument.Save("c:/pdftest/output.pdf");

Using Aspose.Pdf.Generator object

The TextInfo object already supports the feature to specify characters spacing using CharSpace property and this requirement can be accomplished by using following code snippet but we still recommend using new Document Object Model of Aspose.Pdf namespace as all the enhancements and updates are being made in new DOM approach.

Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();

//Create a new section in the Pdf object
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();

//Add 1st paragraph (inheriting the text format settings from the section)
//to the section 
sec1.Paragraphs.Add(new Aspose.Pdf.Generator.Text(sec1, "This is generator text paragraph with default character spacing"));

Aspose.Pdf.Generator.Section sec2 = pdf1.Sections.Add();

//Create 2nd paragraph (inheriting the text format settings from the section) 
Aspose.Pdf.Generator.Text t2 = new Aspose.Pdf.Generator.Text(sec1);

//Create a segment "seg1" in the paragraph "t2"
Aspose.Pdf.Generator.Segment seg1 = new Aspose.Pdf.Generator.Segment(t2);
//Assign some content to the segment
seg1.Content = "This is generator text paragraph with increased character spacing";
//Set character spacing of the segment to 2.0
seg1.TextInfo.CharSpace = 2.0f;

//Add segment (with character spacing) to the paragraph
t2.Segments.Add(seg1);
//Add 2nd text paragraph to the section with overridden text format settings
sec1.Paragraphs.Add(t2);

//save the document
pdf1.Save(outFile);

TextStamp rotation

Adding text stamps inside PDF file is one of the salient features of our API and while adding Text Stamp, we may have a requirement to rotate the text stamp. In order to accomplish this requirement, a property named RotateAngle is present inside TextStamp class. You can pass any angle value which can be applied to rotate Stamp instance. Please take a look over following code snippet which illustrates the steps to rotate TextStamp at 45 degree angle when placing it inside PDF file.

//open document
Document pdfDocument = new Document();
pdfDocument.Pages.Add();
//create text stamp
TextStamp textStamp = new TextStamp("Sample Stamp");
//set whether stamp is background
textStamp.Background = (true);
//set origin
textStamp.XIndent = (100);
textStamp.YIndent = (100);
//rotate stamp at 45 degree
textStamp.RotateAngle = 45;
//set text properties
textStamp.TextState.Font = FontRepository.FindFont("Arial");
textStamp.TextState.FontSize = (14.0F);
textStamp.TextState.FontStyle = (FontStyles.Bold);
textStamp.TextState.FontStyle = (FontStyles.Italic);
textStamp.TextState.ForegroundColor = (Aspose.Pdf.Color.Green);
//add stamp to particular page
pdfDocument.Pages[1].AddStamp(textStamp);
//save output document
pdfDocument.Save("TextStamp_output.pdf");

PDF to DOC - Paragraph break at the end of each paragraph

Recently one of the customers reported an issue where paragraph break was being added at the end of each line instead adding it at the end of paragraph. In order to cater this requirement, a new property named AddReturnToLineEnd is added in DocSaveOptions class. Please take a look over following code snippet.

// load input PDF
Aspose.Pdf.Document document = new Document("source.pdf");
// instantiate DocSave instance to save output in MS Word format
Aspose.Pdf.DocSaveOptions saveOptions = new Aspose.Pdf.DocSaveOptions();
// specify output format as DOCX
saveOptions.Format = DocSaveOptions.DocFormat.DocX;
// set the mode of contents as Flow
saveOptions.Mode = Aspose.Pdf.DocSaveOptions.RecognitionMode.Flow;
// donot add paragraph break at end of each line
saveOptions.AddReturnToLineEnd = false;
// save output as DOCX format
document.Save("Resultant.docx", SaveFormat.DocX);

As well as the enhancements and features discussed above, there have been specific improvement for PDF to HTML and HTML to PDF conversion features. Among these fixes, the PCL to PDF, SVG to PDF, PDF to Excel, PDF to TIFF and TIFF to PDF conversion, conversion of PDF to PDF/A compliant documents, text replacement, rendering PDF files to XPS format are also improved. Please download and try the latest release of Aspose.Pdf for .NET 10.1.0.

Join the Conversation

1 Comment

Leave a comment

Posted inAspose.PDF Product Family, Nayyer Shahbaz
 

Related Articles