75
Creating and Modifying PDF Documents
Creating and Modifying PDF Files
4
72
Acrobat JavaScript Scripting Guide
Extracting, Moving, Deleting, Replacing, and Copying Pages
The Acrobat JavaScript
doc
object, in combination with the
app
object, can be used to
extract pages from one document and place them in another, and moving or copying
pages within or between documents.
The
app
object an be used to create or open any document. To create a new document,
invoke its
newDoc
method, and to open an existing document, invoke its
openDoc
method.
The
doc
object offers three useful methods for handling pages:
●
insertPages
: inserts pages from the source document into the current document
●
deletePages
: deletes pages from the document
●
replacePages
: replaces pages in the current document with pages from the source
document.
These methods enable you to customize the page content within and between documents.
Suppose you would like to remove pages within a document. Invoke the
doc
object’s
deletePages
method, which accepts two parameters:
●
nStart
: the zero-based index of the beginning page
●
nEnd
: the zero-based index of the last page
For example, the following code deletes pages 2 through 5 of the current document:
this.deletePages({nStart: 2, nEnd: 5});
Suppose you would like to copy pages from one document to another. Invoke the
doc
object’s
insertPages
method, which accepts four parameters:
●
nPage
: the zero-based index of the page after which to insert the new pages
●
cPath
: the device-independent pathname of the source file
●
nStart
: the zero-based index of the beginning page
●
nEnd
: the zero-based index of the last page
For example, the following code inserts pages 2 through 5 from
mySource.pdf
at the
beginning of the current document:
this.insertPages({
nPage: -1,
cPath: "/C/mySource.pdf",
nStart: 2,
nEnd: 5
});
60
Acrobat JavaScript Scripting Guide
73
Creating and Modifying PDF Documents
Creating and Modifying PDF Files
4
You can combine these operations to extract pages from one document and move them to
another (they will be deleted from the first document). The following code will extract
pages 2 through 5 in
mySource.pdf
and move them into
myTarget.pdf
:
// this represents myTarget.pdf
//
// First copy the pages from the source to the target document
this.insertPages({
nPage: -1,
cPath: "/C/mySource.pdf",
nStart: 2,
nEnd: 5
});
//
// Now delete the pages from the source document
var source = app.openDoc("/C/mySource.pdf");
source.deletePages({nStart: 2, nEnd: 5});
To replace pages in one document with pages from another document, invoke the target
document’s
replacePages
method, which accepts four parameters:
●
nPage
: the zero-based index of the page at which to start replacing pages
●
cPath
: the device-independent pathname of the source file
●
nStart
: the zero-based index of the beginning page
●
nEnd
: the zero-based index of the last page
In the following example, pages 2 through 5 from
mySource.pdf
replace pages 30 through
33 of
myTarget.pdf
:
// this represents myTarget.pdf
this.replacePages({
nPage: 30,
cPath: "/C/mySource.pdf",
nStart: 2,
nEnd: 5
});
To safely move pages within the same document, it is advisable to perform the following
sequence:
1. Copy the source pages to a temporary file.
2. Insert the pages in the temporary file at the new desired location in the original
document.
3. Delete the source pages from the original document.
56
Creating and Modifying PDF Documents
Creating and Modifying PDF Files
4
74
Acrobat JavaScript Scripting Guide
The following example moves pages 2-5 after page 30 in the document:
// First create the temporary document:
var tempDoc = app.newDoc("/C/temp.pdf");
// Copy pages 2-5 into the temporary file
tempDoc.insertPages({
cPath: "/C/mySource.pdf",
nStart: 2,
nEnd: 5
});
// Copy all of the temporary file pages back into the original:
this.insertPages({
nPage: 30,
cPath: "/C/temp.pdf"
});
// Now delete pages 2-5 from the source document
this.deletePages({nStart: 2, nEnd: 5});
Adding Watermarks and Backgrounds
The
doc
object’s
addWatermarkFromText
and
addWatermarkFromFile
methods
create watermarks within a document, and place them in optional content groups (OCGs).
The
addWatermarkFromFile
method adds a page as a watermark to the specified
pages in the document. The example below adds the first page of
watermark.pdf
as a
watermark to the center of all pages within the current document:
this.addWatermarkFromFile("/C/watermark.pdf");
In the next example, the
addWatermarkFromFile
method is used to add the second
page of
watermark.pdf
as a watermark to the first 10 pages of the current document. It is
rotated counterclockwise by 45 degrees, and positioned one inch down and two inches
over from the top left corner of each page:
this.addWatermarkFromFile({
cDIPath: "/C/watermark.pdf",
nSourcePage: 1,
nEnd: 9,
nHorizAlign: 0,
nVertAlign: 0,
nHorizValue: 144,
nVertValue: -72,
nRotation: 45
});
VB.NET PDF - Acquire or Save PDF Image to File NET Annotate PDF in WPF, C#.NET PDF Create, C#.NET search text in PDF, C#.NET edit PDF bookmark, C#.NET NET TWAIN Scanning DLLs: Scan Many Pages into One PDF.
create bookmark in pdf automatically; export pdf bookmarks to excel
50
Acrobat JavaScript Scripting Guide
75
Creating and Modifying PDF Documents
Converting PDF Documents to XML Format
4
It is also possible to use the
addWatermarkFromText
method to create watermarks. In
this next example, the word Confidential is placed in the center of all the pages of the
document, and its font helps it stand out:
this.addWatermarkFromText(
"Confidential",
0,
font.Helv,
24,
color.red
);
Adding Headers and Footers
As you learned in Adding Watermarks and Backgrounds, you may use Acrobat JavaScript’s
watermarking functionality to add headers and footers to your documents by invoking the
doc
object’s
addWatermarkFromText
method, which can be applied as a header or
footer by specifying the placement of the text at the top or bottom of the page. The
following example places a multiline header, one inch down and one inch over from the
top right corner of all the pages within the current document:
this.addWatermarkFromText({
cText: "Confidential Document",
nTextAlign: 2,
nHorizAlign: 2,
nVertAlign: 0,
nHorizValue: -72,
nVertValue: -72
});
Converting PDF Documents to XML Format
Since XML is often the basis for information exchange within Web Services and enterprise
infrastructures, it may often be useful to convert your PDF documents into XML format.
It is a straightforward process to do this using the
doc
object’s
saveAs
method, which not
only performs the conversion to XML, but also to a number of other formats.
In order to convert your PDF document to a given format, you will need to determine the
device-independent path to which you will save your file, and the conversion ID used to
save in the desired format. A list of conversion IDs for all formats is provided in the ASN
documentation. For XML, the conversion ID is "com.adobe.acrobat.xml-1-00".
The following code converts the current PDF file to C:\test.xml:
this.saveAs("/c/test.xml", "com.adobe.acrobat.xml-1-00");
C# PDF - Acquire or Save PDF Image to File scanners and digital cameras) automatically and saving the images to file in C#.NET application. C#.NET TWAIN Scanning DLLs: Scan Many Pages into One PDF.
editing bookmarks in pdf; add bookmarks to pdf
5
Creating and Modifying PDF Documents
Converting PDF Documents to XML Format
4
76
Acrobat JavaScript Scripting Guide
VB Imaging - VB ISBN Barcode Tutorial use .NET solution that is designed to create ISBN barcode Automatically compute and add check digit for ISBN barcode document files in VB.NET like PDF & Word.
copy pdf bookmarks; bookmark pdf documents C# Imaging - Scan Linear ISSN in C#.NET Detect orientation of scanned ISSN barcode automatically from image files using C#. Integrated with PDF controlling library to scan ISSN barcode from PDF
add bookmarks to pdf preview; how to add bookmarks to a pdf
20
Acrobat JavaScript Scripting Guide
77
5
Print Production
Introduction
This chapter will provide you with an in-depth understanding of the ways in which you may
manage print production workflows for PDF documents.
Chapter Goals
At the end of this chapter, you will be able to:
●
Use Acrobat JavaScript to automate and control print quality.
●
Determine whether a file will be sent to a printer or a PostScript file.
●
Control how PDF layers are printed.
Contents
Topics
Printing PDF Documents
Printing Documents with Layers
Setting Advanced Print Options
60
Print Production
Print Production
5
78
Acrobat JavaScript Scripting Guide
Print Production
Since printing involves sending pages to an output device, there are many options that can
affect print quality. Acrobat JavaScript can be used to enhance and automate the use of
these options in print production workflows, primarily through the use of the
printParams
object, whose properties and methods are described below in Table 5.1:
T
ABLE
5.1
PrintParams Properties
Property
Description
binaryOK
Binary printer channel is supported
bitmapDPI
DPI used for bitmaps or rasterizing transparency
colorOverride
Uses color override settings
colorProfile
Color profile based on available color spaces
constants
Wrapper object for
printParams
constants
downloadFarEastFonts Sends Far East fonts to the printer
fileName
Filename is used when printing to a file instead of a
printer
firstPage
The first zero-based page to be printed
flags
A bit field of flags to control printing options
fontPolicy
Used to determine when fonts are emitted
gradientDPI
The DPI used for rasterizing gradients
interactive
Sets the level of interaction for the user
lastPage
The last zero-based page to be printed
pageHandling
How pages will be handled (fit, shrink, or tiled)
pageSubset
Even, odd, or all pages are printed
printAsImage
Sends pages as large bitmaps
printContent
Determines whether form fields and comments will be
printed
printerName
The name of the destination printer
psLevel
The level of PostScript emitted to the printer
rasterFlags
A bit field of flags for outlines, clips, and overprint
72
Acrobat JavaScript Scripting Guide
79
Print Production
Print Production
5
In addition to the
printParams
object’s properties, the
app
object’s
printColorProfiles
and
printerNames
properties provide a list of available color
spaces and printer names, respectively. Also, the
field
object’s
print
method can be
used to determine whether an individual field will be printed.
Printing PDF Documents
It is possible to use Acrobat JavaScript to specify whether a PDF document is sent to a
printer or to a PostScript file. In either case, to print a PDF document, invoke the
doc
object’s
print
method. Its parameters are described below in Table 5.2:
reversePages
Prints pages in reverse order
tileLabel
Labels each page of tiled output
tileMark
Output marks to cut the page and where overlap
occurs
tileOverlap
The number of points that tiled pages have in
common
tileScale
The amount that tiled pages are scaled
transparencyLevel
The degree to which high level drawing operators are
preserved
userPrinterCRD
Determines whether the printer Color Rendering
Dictionary is used
useT1Conversion
Determines whether Type 1 fonts will be converted
T
ABLE
5.2
Print Method Parameters
Parameter
Description
bUI
Determines whether to present a user interface to the
user
nStart
The zero-based index of the beginning page
nEnd
The zero-based index of the last page
bSilent
Suppresses the Cancel dialog box while the document
is printed
bShrinkToFit
Determines whether the page is shrunk to fit the
imageable area of the printed page
T
ABLE
5.1
PrintParams Properties
Property
Description
61
Print Production
Print Production
5
80
Acrobat JavaScript Scripting Guide
In the first example below, pages 1-10 of the document are sent to the default printer, print
silently without user interaction, and are shrunk to fit the imageable area of the pages:
this.print({
bUI: false,
bSilent: true,
bShrinkToFit: true,
nStart: 1,
nEnd: 10
});
To print the document to a PostScript file, obtain the
printParams
object by invoking
the
doc
object’s
getPrintParams
method. Set its
printerName
property to the
empty string, and set its
fileName
property to a string containing the device-
independent path of the PostScript file to which it will be printed, as shown in the following
example:
var pp = this.getPrintParams();
pp.printerName = "";
pp.fileName = "/C/myPSDoc.ps";
this.print(pp);
If you would like send the file to a particular printer, you may specify the printer by setting
the
printerName
property of the
printParams
object, as shown in the following
example:
var pp = this.getPrintParams();
pp.interactive = pp.constants.interactionLevel.automatic;
pp.printerName = "hp officejet d series";
this.print(pp);
bPrintAsImage
Determines whether to print each page as an image
bReverse
Determines whether to print in reverse page order
bAnnotations
Determines whether to print annotations
printParams
The
printParams
object containing the printing
settings
T
ABLE
5.2
Print Method Parameters
Parameter
Description
Documents you may be interested
Documents you may be interested