123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFDocument.Action;
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace OutlineTest
- {
- internal class OutlineTest
- {
- private static string outputPath =Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) ?? string.Empty, "Output", "CS");
- static private int outlineCounter = 0;
- static private int outlineNumber = 0;
- static void Main(string[] args)
- {
- #region Perparation work
- Console.WriteLine("Running Outline test sample…\r\n");
- SDKLicenseHelper.LicenseVerify();
- if (!Directory.Exists(outputPath))
- {
- Directory.CreateDirectory(outputPath);
- }
- #endregion
- #region Sample 1: Print outline
- CPDFDocument document = CPDFDocument.InitWithFilePath("FourOutline.pdf");
-
- if (PrintOutline(document))
- {
- Console.WriteLine("Print outline done.");
- }
- else
- {
- Console.WriteLine("Print outline failed.");
- }
- document.Release();
- Console.WriteLine("--------------------");
- #endregion
- #region Sample 2: Create outline
- document = CPDFDocument.InitWithFilePath("FourOutline.pdf");
- if (CreateOutline(document))
- {
- Console.WriteLine("Create outline done.");
- }
- else
- {
- Console.WriteLine("Create outline failed.");
- }
- document.Release();
- Console.WriteLine("--------------------");
- #endregion
- #region Sample 3: Move outline
- document = CPDFDocument.InitWithFilePath("FourOutline.pdf");
- if (MoveOutline(document))
- {
- Console.WriteLine("Move outline done.");
- }
- else
- {
- Console.WriteLine("Move outline failed.");
- }
- document.Release();
- Console.WriteLine("--------------------");
- #endregion
- #region Sample 4: Remove outline
- document = CPDFDocument.InitWithFilePath("FourOutline.pdf");
- if (RemoveOutline(document))
- {
- Console.WriteLine("Remove outline done.");
- }
- else
- {
- Console.WriteLine("Remove outline failed.");
- }
- document.Release();
- Console.WriteLine("--------------------");
- #endregion
- Console.WriteLine("Done.");
- Console.WriteLine("--------------------");
- Console.ReadLine();
- }
- /// <summary>
- /// Traverse outline and print it as a list
- /// </summary>
- static private void TraverseOutline(List<CPDFOutline> outlineList)
- {
- foreach (var outline in outlineList)
- {
- for (var i = 0; i < outlineCounter; i++)
- {
- Console.Write(" ");
- }
- Console.Write("-> " + outline.Title + "\n");
- outlineNumber++;
- var childList = outline.ChildList;
- if (childList != null && childList.Count != 0)
- {
- outlineCounter++;
- TraverseOutline(childList);
- }
- else
- {
- if (outlineList.IndexOf(outline)+1 == outlineList.Count)
- {
- outlineCounter--;
- }
- }
- }
- }
- /// <summary>
- /// Print all outlines in the file
- /// </summary>
- /// <param name="document">Document with some outlines</param>
- static private bool PrintOutline(CPDFDocument document)
- {
- List<CPDFOutline> outlineList = document.GetOutlineList();
- outlineNumber = 0;
- outlineCounter = 0;
- TraverseOutline(outlineList);
- Console.WriteLine("Outline number: {0}", outlineNumber);
- return true;
- }
- /// <summary>
- /// Create an outline at the top of the first page
- /// </summary>
- /// <param name="document">Document with some outlines</param>
- static private bool CreateOutline(CPDFDocument document)
- {
- CPDFOutline outline = document.GetOutlineRoot();
- CPDFOutline childOutline = null;
- outline.InsertChildAtIndex(document, 0, ref childOutline);
- CPDFGoToAction gotoAction = new CPDFGoToAction();
- CPDFDestination dest = new CPDFDestination();
- dest.PageIndex = 1;
- gotoAction.SetDestination(document, dest);
- childOutline.SetAction(gotoAction);
- childOutline.SetTitle("New outline");
- PrintOutline(document);
- return true;
- }
- /// <summary>
- /// Create an outline at the top of the first page
- /// </summary>
- /// <param name="document"></param>
- /// <returns></returns>
- static private bool MoveOutline(CPDFDocument document)
- {
- CPDFOutline outline = document.GetOutlineRoot();
- outline.InsertChildAtIndex(document, outline.ChildList.Count, ref outline);
- outline.SetTitle("new outline");
- CPDFOutline targetOutline = document.GetOutlineList()[1];
- targetOutline.MoveChildAtIndex(document, outline, targetOutline.ChildList.Count);
- string path = Path.Combine(outputPath, "MoveOutlineTest.pdf");
- if (!document.WriteToFilePath(path))
- {
- return false;
- }
- CPDFDocument newDocument = CPDFDocument.InitWithFilePath(path);
- PrintOutline(newDocument);
- return true;
- }
- /// <summary>
- /// Remove outline
- /// </summary>
- static private bool RemoveOutline(CPDFDocument document)
- {
- document.GetOutlineList()[0].RemoveFromParent(document);
- string path = Path.Combine(outputPath, "RemoveOutlineTest.pdf");
- if (!document.WriteToFilePath(path))
- {
- return false;
- }
- CPDFDocument newDocument = CPDFDocument.InitWithFilePath(path);
- PrintOutline(newDocument);
- return true;
- }
- }
- }
|