Coherent PDF Command Line Tools
Download Free Trial Evaluation
Buy
See manual
Blog
Resellers
Custom Development
News Archive
Links
Free PDF Library
User Manual (HTML)

PDF Tools for .NET Usage Examples

Back to main page

Here are some examples of the PDF command line tools in action, using C# syntax. VB.NET syntax is similar. This is just a small selection—for more examples, see the Full PDF Manual.

To see the same examples using the Command Line Tools, click here.

Index

1. Basic Usage
2. Merging and Splitting
3. Pages
4. Encryption and Decryption
5. Compression
6. Bookmarks
7. Presentations
8. Logos, Watermarks and Stamps
9. Multipage Facilities
10. Annotations
11. Document Information and Metadata
12. File Attachments
13. Miscellaneous

1. Basic Usage

Pdf.pdfdoc pdf = Cpdflib.FromFile("in.pdf");

int[] range = {1, 2, 3, 6};

Pdf.pdfdoc pages = Cpdflib.SelectPages(pdf, range);

Cpdflib.ToFile(pdf, "out.pdf", false, true);

Read in.pdf, select pages 1, 2, 3 and 6, and write those pages to out.pdf.

Pdfdoc.pdf evenpdf = Cpdflib.SelectPages(pdf, Cpdflib.Even(Cpdflib.All(pdf)));

Select the even pages (2, 4, 6...) from a pre-existing PDF.

2. Merging and Splitting

Pdf.pdfdoc [] pdfs = { pdf1, pdf2 };

Pdf.pdfdoc pdf = Cpdflib.MergeSimple(pdfs);

Merge pdf1 and pdf2 into one document, pdf.

Pdf.pdfdoc [] pdfs = Cpdflib.SplitOnBookmarks(pdf, 0);

Split pdf on bookmark boundaries.

3. Pages

Cpdflib.ScalePages(pdf, Cpdflib.All(pdf), 2.0, 2.0);

Scale both the dimensions and contents of pdf by a factor of two in x and y directions.

Cpdflib.ScaleToFit(pdf, Cpdflib.All(pdf), Cpdflib.usletterportrait);

Scale the pages in pdf to fit the US Letter page size.

Cpdflib.ShiftPageContents(pdf, Cpdflib.All(pdf), 26.0, Cpdflib.PtOfMm(18.0));

Shift the contents of the page by 26 pts in the x direction, and 18 millimetres in the y direction.

Cpdflib.RotateContents(pdf, Cpdflib.All(pdf), 90.0);

Rotate the contents of the pages in in.pdf by ninety degrees and write to out.pdf.

Cpdflib.SetCropping(pdf, Cpdflib.All(pdf), 0.0, 0.0, 600.0, 400.0);

Crop the pages in pdf to a 600 pts by 400 pts rectangle.

4. Encryption and Decryption

Pdfcrypt.permissions[] permissions =
  {Pdfcrypt.permission.NoEdit,
   Pdfcrypt.permission.NoAssemble};

Cpdflib.ToFileEncrypted
  (pdf,
   Pdfwrite.EncryptionMethod.AES128bit(false),
   permissions,
   "fred",
   "joe",
   false,
   "out.pdf);

Encrypt pdf using 128bit PDF encryption using the user password fred and the owner password joe, setting some permissions and writing the encrypted file to out.pdf

Pdf.pdfdoc pdf = Cpdflib.FromFileDecrypt("in.pdf", "fred");

Load and decrypt a file using the owner password.

5. Compression

Cpdflib.Compress(pdf);

Compress the data streams in pdf.

Cpdflib.Decompress(pdf);

Decompress the data streams in pdf.

6. Bookmarks

Pdfmarks.bookmark [] marks = Cpdflib.GetBookmarks(pdf);

List the bookmarks in a pdf.

Pdfmarks.bookmark [] marks =
  {Cpdflib.MakeBookmark(0, "Part 1", 1, true),
   Cpdflib.MakeBookmark(1, "Part 1A", 2, false),
   Cpdflib.MakeBookmark(1, "Part 1B", 3, false),
   Cpdflib.MakeBookmark(0, "Part 2", 8, false),
   Cpdflib.MakeBookmark(1, "Part 2a", 9, false)};

Cpdflib.AddBookmarks(pdf, marks);

Add bookmarks to a document.

7. Presentations

Cpdflib.AddPresentation
  (pdf,
   Cpdflib.All(pdf),
   Cpdflib.transition.Blinds,
   1.0,
   false,
   false,
   0,
   0.5);

Use the Blinds style to build a presentation from the pdf (See manual for full details of each argument).

8. Logos, Watermarks and Stamps

Cpdflib.StampOn(sourcepdf, pdf, Cpdflib.All(pdf))

Stamp the document sourcepdf on to each page of pdf.

Cpdflib.AddText
  (pdf,
   Cpdflib.All(pdf),
   "This is page %Page of %EndPage at Date %d-%n-%Y",
   Cpdf.position.TopLeft(10.0),
   1.0,
   0,
   Pdftext.standard_font.Courier,
   24.0,
   Cpdflib.red,
   false);

Add a red page number and date to all the pages in pdf using the Courier font at 24pt.

9. Multipage Facilities

Cpdflib.TwoUp(pdf);

Two up impose a document.

int[] range = {1, 3, 4};
Cpdflib.PadAfter(pdf, r);

Add extra blank pages after pages one, three and four of a document.

10. Annotations

Cpdflib.annotation [] annots = Cpdflib.ListAnnotations(pdf);

List the annotations in a file pdf

Cpdflib.CopyAnnotations(source_pdf, target_pdf);

Copy the annotations from source_pdf to target_pdf.

11. Document Information and Metadata

Cpdflib.SetTitle(pdf, "The New Title");

Set the document title of pdf.

Cpdflib.HideToolbar(pdf);

Set the document pdf to open with the Acrobat Viewer's toolbar hidden.

Cpdflib.SetMetadataFromFile(pdf, "metadata.xml");

Set the metadata in pdf to the contents of the file metadata.xml.

Cpdflib.SetPageLayout(pdf, Cpdflib.layout.TwoColumnRight);

Set the document pdf to open in Acrobat Viewer showing two columns of pages, starting on the right.

Cpdflib.SetPageMode(pdf, Cpdflib.pagemode.FullScreen)

Set the document pdf to open in Acrobat Viewer in full screen mode.

12. File Attachments

Cpdflib.AttachFile(pdf, "sheet.xls");

Attach the file sheet.xls to pdf.

Cpdflib.RemoveAttachments(pdf):

Remove any attachments from pdf.

13. Miscellaneous

Cpdf.BlackText(pdf);

Blacken all the text in pdf.

Cpdf.ThinLines(pdf, Cpdflib.All(pdf), 2.0);

Make sure all lines in pdf are at least 2 pts wide.