Unplanned
Last Updated: 22 Jul 2021 11:22 by ADMIN
Suren
Created on: 22 Jul 2021 08:53
Category: PdfProcessing
Type: Feature Request
5
PdfProcessing: Provide API for inserting Table of Contents (TOC)

This could be implemented by linking to RadFixedPages or Bookmarks.

Until this feature is developed this could be custom created with the help of Link annotations:

RadFixedPage toc = new RadFixedPage();
document1.Pages.Insert(0, toc);

FixedContentEditor editor = new FixedContentEditor(toc);

foreach (RadFixedPage page in document1.Pages)
{
	int pageNumber = document1.Pages.IndexOf(page);

	if (pageNumber > 0)
	{
		int factor = 20;

		int offsetX = 70;
		int offsetY = 20 + factor * pageNumber;
		editor.Position.Translate(offsetX, offsetY);

		Block block = new Block();
		block.GraphicProperties.FillColor = new RgbColor(255, 5, 99, 193);
		block.InsertText($"Page {pageNumber}");
		Size blockSize = block.Measure();
		editor.DrawBlock(block);

		Location location = new Location
		{
			Left = 0,
			Top = 0,
			Zoom = 0,
			Page = page
		};

		GoToAction goToAction = new GoToAction();
		goToAction.Destination = location;

		Link uriLink = toc.Annotations.AddLink(goToAction);
		uriLink.Rect = new Rect(offsetX, offsetY, blockSize.Width, blockSize.Height);
	}
}

4 comments
ADMIN
Martin
Posted on: 22 Jul 2021 11:22

Hi Suren,

I am happy you have a working solution now. Do not hesitate to contact us if any additional questions arise.

Regards,
Martin
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Suren
Posted on: 22 Jul 2021 10:49

Thanks , it is working now... great help

 

public void CreateIndex()
{
PdfFormatProvider provider = new PdfFormatProvider();
PdfImportSettings importSettings = new PdfImportSettings
{
CopyStream = true
};

provider.ImportSettings = importSettings;
var inputSteam = File.OpenRead("D:\\DPS\\Bundling - V3\\TestFile\\out\\out.pdf");
RadFixedDocument document1 = provider.Import(inputSteam);

using (Stream output = File.OpenWrite("D:\\DPS\\Bundling - V3\\TestFile\\out\\out12.pdf"))
{
RadFixedPage toc = new RadFixedPage();
document1.Pages.Insert(0, toc);
FixedContentEditor editor = new FixedContentEditor(toc);

foreach (RadFixedPage page in document1.Pages)
{
int pageNumber = document1.Pages.IndexOf(page);

if (pageNumber > 0)
{
int factor = 20;

int offsetX = 70;
int offsetY = 20 + factor * pageNumber;
editor.Position.Translate(offsetX, offsetY);

Block block = new Block();
block.GraphicProperties.FillColor = new RgbColor(255, 5, 99, 193);
block.InsertText($"Page {pageNumber}");
Size blockSize = block.Measure();
editor.DrawBlock(block);

Location location = new Location
{
Left = 110,
Top = 110,
Zoom = 10,
Page = page
};

GoToAction goToAction = new GoToAction();
goToAction.Destination = location;

Link uriLink = toc.Annotations.AddLink(goToAction);
uriLink.Rect = new Rect(offsetX, offsetY, blockSize.Width, blockSize.Height);
}

provider.Export(document1, output);
}
}
}
Suren
Posted on: 22 Jul 2021 10:48

Hi Thank you for the feed back..not its working...

 

 

public void CreateIndex()
{
PdfFormatProvider provider = new PdfFormatProvider();
PdfImportSettings importSettings = new PdfImportSettings
{
CopyStream = true
};

provider.ImportSettings = importSettings;
var inputSteam = File.OpenRead("D:\\DPS\\Bundling - V3\\TestFile\\out\\out.pdf");
RadFixedDocument document1 = provider.Import(inputSteam);

using (Stream output = File.OpenWrite("D:\\DPS\\Bundling - V3\\TestFile\\out\\out12.pdf"))
{
RadFixedPage toc = new RadFixedPage();
document1.Pages.Insert(0, toc);
FixedContentEditor editor = new FixedContentEditor(toc);

foreach (RadFixedPage page in document1.Pages)
{
int pageNumber = document1.Pages.IndexOf(page);

if (pageNumber > 0)
{
int factor = 20;

int offsetX = 70;
int offsetY = 20 + factor * pageNumber;
editor.Position.Translate(offsetX, offsetY);

Block block = new Block();
block.GraphicProperties.FillColor = new RgbColor(255, 5, 99, 193);
block.InsertText($"Page {pageNumber}");
Size blockSize = block.Measure();
editor.DrawBlock(block);

Location location = new Location
{
Left = 110,
Top = 110,
Zoom = 10,
Page = page
};

GoToAction goToAction = new GoToAction();
goToAction.Destination = location;

Link uriLink = toc.Annotations.AddLink(goToAction);
uriLink.Rect = new Rect(offsetX, offsetY, blockSize.Width, blockSize.Height);
}

provider.Export(document1, output);
}
}
}
Suren
Posted on: 22 Jul 2021 10:43

 

seems to be Rect not visible in doc, please check below my sample code.

 

public void CreateIndex()
{
PdfFormatProvider provider = new PdfFormatProvider();
PdfImportSettings importSettings = new PdfImportSettings
{
CopyStream = true
};

provider.ImportSettings = importSettings;
var inputSteam = File.OpenRead("D:\\DPS\\Bundling - V3\\TestFile\\out\\out.pdf");
RadFixedDocument document1 = provider.Import(inputSteam);

using (Stream output = File.OpenWrite("D:\\DPS\\Bundling - V3\\TestFile\\out\\out12.pdf"))
{
RadFixedPage toc = new RadFixedPage();
FixedContentEditor editor = new FixedContentEditor(toc);

foreach (RadFixedPage page in document1.Pages)
{
int pageNumber = document1.Pages.IndexOf(page);

if (pageNumber > 0)
{
int factor = 20;

int offsetX = 70;
int offsetY = 20 + factor * pageNumber;
editor.Position.Translate(offsetX, offsetY);

Block block = new Block();
block.GraphicProperties.FillColor = new RgbColor(255, 5, 99, 193);
block.InsertText($"Page {pageNumber}");
Size blockSize = block.Measure();
editor.DrawBlock(block);

Location location = new Location
{
Left = 110,
Top = 110,
Zoom = 10,
Page = page
};

GoToAction goToAction = new GoToAction();
goToAction.Destination = location;

Link uriLink = toc.Annotations.AddLink(goToAction);
uriLink.Rect = new Rect(offsetX, offsetY, blockSize.Width, blockSize.Height);
}

provider.Export(document1, output);
}
}
}