Add TOC in Existing PDF, Convert PDF to DOC and Save Output to Stream Object

Share on FacebookTweet about this on TwitterShare on LinkedIn

Aspose.Pdf iconWe are very much excited to announce the release of Aspose.Pdf for .NET 8.3.0 which comes with great set of features and enhancements. For quite some time, Aspose.Pdf for .NET has provided the capability to generate a table of content when creating a new PDF document as described in Manipulating List of Contents. However, in this new release, we have introduced the capability to create table of contents inside an existing PDF document. The Document class in the Aspose.Pdf namespace gives the power to create as well as manipulate existing PDF files, so we added Heading and TocInfo classes to allow our customer to create TOC in existing PDF files. Further details regarding this feature can be found over Add TOC in existing PDF.

Loading Font from Stream

When adding text to existing PDF files, we specify the font information for the TextFragment being added. Therefore we can either specify the font location from a file path or reference it from a stream object. The following code snippet shows how to load the font from a Stream object.


// Load input PDF file
Document doc = new Document("input.pdf");
// Create a text builder object for first page of document
TextBuilder textBuilder = new TextBuilder(doc.Pages[1]);
// Create text fragment with sample string
TextFragment textFragment = new TextFragment("Hello world");
// Load the TrueType font into stream object
using (FileStream fontStream = File.OpenRead(@"C:\CustomerFont.ttf"))
{
    // Set the font name for text string
    textFragment.TextState.Font = FontRepository.OpenFont(fontStream, FontTypes.TTF);
    // Specify the position for Text Fragment
    textFragment.Position = new Position(10, 10);
    // Add the text to TextBuilder so that it can be placed over the PDF file
    textBuilder.AppendText(textFragment);
}
// Save the updated PDF file
doc.Save("c:/TTF_FontAdded.pdf");

Implement Margin support for TextParagraph

When adding text to an existing PDF files using TextFragment, we can specify the obsolete position of the fragment using the Position property of TextFragment


// Load existing PDF files
Document doc = new Document(inFile);
// Get reference of first page of PDF
Page page = (Page)doc.Pages[1];
// Create text builder for specific page
TextBuilder builder = new TextBuilder(page);
// Create text paragraph
TextParagraph paragraph = new TextParagraph();
paragraph.Rectangle = new Aspose.Pdf.Rectangle(100, 600, 200, 700);
// Set the margin for paragraph
paragraph.Margin = new Aspose.Pdf.MarginInfo(5, 10, 10, 15);
// Set the wordwrap mode
paragraph.FormattingOptions.WrapMode = TextFormattingOptions.WordWrapMode.DiscretionaryHyphenation;
// Append lines to TextFragment
paragraph.AppendLine("the quick brown fox jumps over the lazy dog");
paragraph.AppendLine("line2");
paragraph.AppendLine("line3");
// Add paragraph
builder.AppendParagraph(paragraph);
// Save the resultant PDF files
doc.Save(outFile);

As well as the above features, we have also introduced many enhancements in terms of performance when converting large text files to PDF format, PDF to XPS conversion, PDF to TIFF conversion, HTML to PDF conversion and much more. Please download and evaluate the latest release of Aspose.Pdf for .NET 8.3.0.