Manipulate PDF Document

Validate PDF Document for PDF A Standard (A 1A and A 1B)

To validate a PDF document for PDF/A-1a or PDF/A-1b compatibility, use the Document class Validate method. This method allows you to specify the name of the file in which the result is to be saved and the required validation type PdfFormat enumeration : PDF_A_1A or PDF_A_1B.

The following code snippet shows you how to validate PDF document for PDF/A-1A.

The following code snippet shows you how to validate PDF document for PDF/A-1b.

Working with TOC

Add TOC to Existing PDF

Aspose.PDF API allows you to add a table of content either when creating a PDF, or to an existing file. The ListSection class in the Aspose.Pdf.Generator namespace allows you to create a table of contents when creating a PDF from scratch. To add headings, which are elements of the TOC, use the Aspose.Pdf.Generator.Heading class.

To add a TOC to an existing PDF file, use the Heading class in the Aspose.Pdf namespace. The Aspose.Pdf namespace can both create new and manipulate existing PDF files. To add a TOC to an existing PDF, use the Aspose.Pdf namespace. The following code snippet shows how to create a table of contents inside an existing PDF file.

Set different TabLeaderType for different TOC Levels

Aspose.PDF also allows setting different TabLeaderType for different TOC levels. You need to set LineDash property of FormatArray with the appropriate value of TabLeaderType enum as following.

C#

 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);

Hide Page Numbers in TOC

In case if you do not want to display page numbers, along with the headings in TOC, you can use IsShowPageNumbers property of TOCInfo Class as false. Please check following code snippet to hide page numbers in the table of contents:

Customize Page Numbers while adding TOC

It is common to customize the page numbering in the TOC while adding TOC in a PDF document. For example, we may need to add some prefix before page number like P1, P2, P3 and so on. In such a case, Aspose.PDF for .NET provides PageNumbersPrefix property of TocInfo class that can be used to customize page numbers as shown in the following code sample.

How to set PDF expiry date

We apply access privileges on PDF files so that a certain group of users can access particular features/objects of PDF documents. In order to restrict the PDF file access, we usually apply encryption and we may have a requirement to set PDF file expiration, so that the user accessing/viewing the document gets a valid prompt regarding PDF file expiry.

In order to accomplish the above stated requirement, we can use JavascriptAction object. Please take a look over the following code snippet.

Determine Progress of PDF File Generation

A customer asked us to add a feature that allows developers to determine the progress of PDF file generation. Here’s the response to that request.

The DocSaveOptions class CustomerProgressHandler allows you to determine how PDF generation is going. The handler has the following types:

  • DocSaveOptions.ConversionProgessEventHandler
  • DocSaveOptions.ProgressEventHandlerInfo
  • DocSaveOptions.ProgressEventType)

The code snippets below shows how to use CustomerProgressHandler.