Support to Reference Single Image for Multiple Times and Add Text Inside a Graph Object in Aspose.Pdf for .NET 17.1.0

Share on FacebookTweet about this on TwitterShare on LinkedIn

We are pleased to announce the release of Aspose.Pdf for .NET 17.1.0. This month release introduces some new features. Some of these are to add reference of an image for multiple times, break text for a table broken across the pages and to set different TabLeaderType for different TOC levels along with an enhancement to add text inside the Graph Object. This release also contains the fixes of bugs reported in previous versions by our valued customers that improves the API functionality. Please check the detailed release notes of Aspose.Pdf for .NET 17.1.0, in order to get an idea about the new features/enhancements and fixes made in this release of Aspose.Pdf for .NET API.

Furthermore, If you are planning to upgrade the API from any previous version, we strongly recommend you to check the Public API Changes section of current release and other intermediate releases from release notes pages, to know what has been changed since your current revision of the API.

 

The following sections describe some details regarding these newly added features/enhancements.

Reference a Single Image Multiple Times

Some of our customers have requirement to use same image reference for multiple times in a PDF, it helps to reduce the PDF document file size. We have introduced XImageCollection.Add(Ximage) method in this release. It helps to refer existing image instance in the PDF document instead new Image.

Aspose.Pdf.Rectangle imageRectangle = new Aspose.Pdf.Rectangle(0, 0, 30, 15);
using (Aspose.Pdf.Document document = new Aspose.Pdf.Document("input.pdf"))
{
    using (var imageStream = File.Open("icon.png", FileMode.Open))
    {
        XImage image = null;
        foreach (Page page in document.Pages)
        {
            WatermarkAnnotation annotation = new WatermarkAnnotation(page, page.Rect);
            XForm form = annotation.Appearance["N"];
            form.BBox = page.Rect;

            string name;
            if (image == null)
            {
                name = form.Resources.Images.Add(imageStream);
                image = form.Resources.Images[name];
            }
            else
            {
                name = form.Resources.Images.Add(image);
            }

            form.Contents.Add(new Operator.GSave());
            form.Contents.Add(new Operator.ConcatenateMatrix(new Aspose.Pdf.Matrix(imageRectangle.Width, 0, 0, imageRectangle.Height, 0, 0)));
            form.Contents.Add(new Operator.Do(name));
            form.Contents.Add(new Operator.GRestore());

            page.Annotations.Add(annotation, false);
            imageRectangle = new Aspose.Pdf.Rectangle(0, 0, imageRectangle.Width * 1.01, imageRectangle.Height * 1.01);
        }
    }

    document.Save("output.pdf");
}

Set Different TabLeaderTypes for different TOC Levels

Previously, we have to use a single TabLeaderType for all the TOC entries. With the release of current version, now we can set different TabLeaderType for different TOC Levels. For this functionality, we can set LineDash property of TOC Level with required value of TabLeaderType enum. Please check following sample code for details:


string outFile = "TOC.pdf";
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
Page tocPage = doc.Pages.Add();
TocInfo tocInfo = new TocInfo();
//set LeaderType
tocInfo.LineDash = TabLeaderType.Solid;
TextFragment title = new TextFragment("Table Of Contents");
title.TextState.FontSize = 30;
tocInfo.Title = title;
//Add the list section to the sections collection of the Pdf document
tocPage.TocInfo = tocInfo;
//Define the format of the four levels list by setting the left margins
//and
//text format settings of each level
tocInfo.FormatArrayLength = 4;
tocInfo.FormatArray[0].Margin.Left = 0;
tocInfo.FormatArray[0].Margin.Right = 30;
tocInfo.FormatArray[0].LineDash = TabLeaderType.Dot;
tocInfo.FormatArray[0].TextState.FontStyle = FontStyles.Bold | FontStyles.Italic;
tocInfo.FormatArray[1].Margin.Left = 10;
tocInfo.FormatArray[1].Margin.Right = 30;
tocInfo.FormatArray[1].LineDash = TabLeaderType.None;
tocInfo.FormatArray[1].TextState.FontSize = 10;
tocInfo.FormatArray[2].Margin.Left = 20;
tocInfo.FormatArray[2].Margin.Right = 30;
tocInfo.FormatArray[2].TextState.FontStyle = FontStyles.Bold;
tocInfo.FormatArray[3].LineDash = TabLeaderType.Solid;
tocInfo.FormatArray[3].Margin.Left = 30;
tocInfo.FormatArray[3].Margin.Right = 30;
tocInfo.FormatArray[3].TextState.FontStyle = FontStyles.Bold;
//Create a section in the Pdf document
Page page = doc.Pages.Add();
//Add four headings in the section
for (int Level = 1; Level <= 4; Level++)

{ Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(Level);
    TextSegment segment2 = new TextSegment();
    heading2.Segments.Add(segment2);
    heading2.IsAutoSequence = true;
    heading2.TocPage = tocPage;
    segment2.Text = "Sample Heading" + Level;
    heading2.TextState.Font = FontRepository.FindFont("Arial Unicode MS");
    //Add the heading into Table Of Contents. 
    heading2.IsInList = true; 
    page.Paragraphs.Add(heading2); 
}
// save the Pdf 
doc.Save(outFile);

Add Break Text for the Table Broken in Two Pages

In legacy generator(Aspose.Pdf.Generator), we supported table break text to set some text string at the end of the page for the Table broken in two pages. In this release, we have implemented same feature in new generator(Aspsoe.Pdf).


string outFile = "Table.pdf";
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
Page page = doc.Pages.Add();
Aspose.Pdf.Table tab = new Aspose.Pdf.Table();
tab.Alignment = HorizontalAlignment.Center;
tab.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1f);
tab.Margin.Top = 10;

for (int i = 1; i <= 200; i++)

{ Aspose.Pdf.Row row1 = tab.Rows.Add(); row1.Cells.Add("row - " + i); }
tab.BreakText = new TextFragment("To be continued on next page");
tab.RepeatingRowsCount = 1;
page.Paragraphs.Add(tab);
doc.Save(outFile);

Add text inside Graph Object

A customer requested an enhancement to add text inside a Graph object, for his business need. We have added a text property in Graph object to add text inside the shape. Following code shows how we can add text inside a Rectangle shape.


// Open document
string outFile = "Graph.pdf";
Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document();
Aspose.Pdf.Page pdfPage = pdfDoc.Pages.Add();
Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph(500, 100);
pdfPage.Paragraphs.Add(graph);

//1st rectangle
Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(0, 30, 50, 40);
rect.GraphInfo.LineWidth = 1f;
rect.GraphInfo.Color = Aspose.Pdf.Color.Black;
rect.Text = new TextFragment("Rectangle");
graph.Shapes.Add(rect);
pdfDoc.Save(outFile);

Aspose.Pdf for .NET Resources

The following resources will help you work with Aspose.Pdf for .NET: