Created
November 9, 2015 19:06
-
-
Save JoelGeraci-Datalogics/d6e25d49d9fe9dca5a3a to your computer and use it in GitHub Desktop.
Demonstrates the use of the Adobe PDF Library with the Datalogics Extensions to print a PDF file from the command line without user dialogs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Text; | |
using Datalogics.PDFL; | |
/* | |
* | |
* A sample which demonstrates the use of the DLE API to print a PDF file | |
* without any user dialogs. | |
* | |
* Copyright (c) 2007-2015, Datalogics, Inc. All rights reserved. | |
* | |
* The information and code in this sample is for the exclusive use of Datalogics | |
* customers and evaluation users only. Datalogics permits you to use, modify and | |
* distribute this file in accordance with the terms of your license agreement. | |
* Sample code is for demonstrative purposes only and is not intended for production use. | |
* | |
*/ | |
namespace PrintPDF | |
{ | |
class PrintPDF | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ string filename = null; | |
if (args.Length > 0) | |
{ | |
filename = args[0]; | |
} | |
else | |
{ | |
while (String.IsNullOrEmpty(filename)) | |
{ | |
Console.Write(@"Enter filename: "); | |
filename = Console.ReadLine(); | |
} | |
} | |
// ...just some housekeeping to prep a quoted string, etc. | |
filename = filename.Trim(); | |
filename = filename.TrimStart('"'); | |
filename = filename.TrimEnd('"'); | |
FileInfo finfo = new FileInfo(filename); | |
// To use DLE / APDFL one must always begin by initializing the library. This action | |
// is expensive (both time and resource wise) and should only be done when necessary. | |
// In a threaded product, a separate library must be resident on each thread. That | |
// said, it would not be wise to initialize/dispose a fresh library over and over | |
// again within a highly performance oriented inner method. It is also true that | |
// failure to reliably dispose of any individual library instance within a larger | |
// collection of threads / libraries WILL lead to uncontrolled behavior and possibly | |
// uncontrolled termination of one's entire application (all threads!). | |
using (Library lib = new Library()) | |
{ | |
// Open a PDF document (using will automatically .Close and .Dispose it)... | |
using (Document doc = new Document(finfo.FullName)) | |
{ | |
// Get some parameters (aka. print controls)... | |
using (PrintUserParams userParams = new PrintUserParams()) | |
{ | |
// Use the user's default printer. | |
userParams.UseDefaultPrinter(doc); | |
// Use the DeviceName parameter if you prefer to print to a specific printer | |
// userParams.DeviceName = @"Change Me to a valid Printer Name"; | |
// Use the StartPage and EndPage parameters to print a range of pages, | |
// if you don't want to print the whole document | |
// For example, to print only the last page of a document: | |
// userParams.StartPage = doc.NumPages - 1; // 0-based | |
// userParams.EndPage = doc.NumPages - 1; // 0-based | |
// Document collation is enabled by default for printers that support it. | |
// Uncomment this line to disable collation | |
// userParams.Collate = false; | |
// One copy of the document is emitted by default. | |
// Uncomment this line to print multiple copies of a document | |
// userParams.NCopies = 3; | |
// Markup and notes are not printed by default, only the document pages. | |
// Uncomment this line to include markup and notes | |
// userParams.PrintAnnots = 1; | |
// Documents are printed out at 100% scale by default. | |
// Uncomment this line to shrink oversized pages to fit on the printed pages | |
// userParams.ShrinkToFit = true; | |
doc.Print(userParams); | |
} | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(@"An exception occurred. Here is the related information:"); | |
Console.Write(ex.ToString()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment