How to use pdf-designer.js
[Table of contents]
1. Combine of PDF
2. Split(extraction) of PDF
3. Remove of PDF
4. Rotation of PDF
5. Add Bookmark of PDF
6. Property & Display settings of PDF
7. Overview of each class
8. Details of each class
TPDFAnalyst
* TPDFParser
* TPDFCode
TPDFDocInfo
TPDFDocView
* TReadStream
* TMemoryStream
* TFileStream
TPDFCombine
TPDFKnife
TPDFDeletePage
TPDFRotatePage
TPDFInfoMaker
TPDFOutLineMaker
TPDFPageLayout
TPDFPageMode
TPDFOpenActionType
TPDFOpenAction
TPDFViewerPreferences
TPDFNode
TPDFNodeList
TPDFOutLineManager
TColor
TRect
9. Common Functions
10. Callback function
First
You download pdf-designer.js, and install in your environment.
1. Combine of PDF
Important code is method of 22 line TPDFAnalyst.LoadFromStream(), and 52 line of TPDFCombine.SaveToFile()
TPDFAnalyst reads a PDF file. TPDFCombine combines the PDF files.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="pdf-designer.js" type="text/javascript"></script>
<script type="text/javascript">
var Analysts = new Array();
function onChangeFile(event,index) {
var reader = new FileReader();
var file = event.target.files;
document.getElementById("errmsg").innerHTML = '';
reader.onload = function (event) {
var Stream = new Uint8Array(reader.result);
Analysts[index] = new TPDFAnalyst();
try {
Analysts[index].LoadFromStream(Stream);
if (Analysts[index].Encrypt) {
document.getElementById("p"+(index+1)).innerHTML =
'No' + (index+1) + ' PDF File - Unsupported';
document.getElementById("errmsg").innerHTML =
'It does not support encrypted files.';
} else {
document.getElementById("p"+(index+1)).innerHTML =
'No' + (index+1) + ' PDF File - [' + Analysts[index].PageCount +
' Pages / PDF' + Analysts[index].Version + ']';
}
} catch (e) {
Analysts[index] = null;
document.getElementById("p"+(index+1)).innerHTML =
'No' + (index+1) + ' PDF File - Unsupported';
document.getElementById("errmsg").innerHTML =
'No' + (index+1) +' PDF File is Not supported.';
}
}
reader.readAsArrayBuffer(file[0]);
}
function run() {
var PDFCombine = new TPDFCombine();
if (Analysts[0] != null && Analysts[1] != null) {
try {
// PDF_GetDateTime_Now() return the current date and time.
PDFCombine.SaveToFile(PDF_GetDateTime_Now() + '.pdf', Analysts);
} catch (e) {
Analysts[0] = null;
Analysts[1] = null;
document.getElementById("errmsg").innerHTML =
'failed in the convert of the PDF file.';
}
} else {
document.getElementById("errmsg").innerHTML =
'Two PDF files, please select.';
}
}
</script>
</head>
<body>
<h1>Combine of PDF</h1>
<hr>
<p>Two PDF files, please select.</p>
<p id="errmsg" style="color:red;"></p>
<p id="p1">No1 PDF File</p>
<input type="file" id="inputfile1" accept="application/pdf" onchange="onChangeFile(event,0)"><br>
<p id="p2">No2 PDF File</p>
<input type="file" id="inputfile2" accept="application/pdf" onchange="onChangeFile(event,1)"><br>
<br>
<br>
<br>
<button id="run" style="width:200px;height:30px;" onclick="run();">Start Convert</button>
</body>
</html>
FileReader() is read the file asynchronously. Use the FileReaderSync() If you read in synchronization, in Worker object. These are the new features of HTML5.
Use class : TPDFAnalyst TPDFCombine
2. Split(extraction) of PDF
The following code to extract the first page of the PDF file.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="pdf-designer.js" type="text/javascript"></script>
<script type="text/javascript">
var Analyst = null;
function onChangeFile(event) {
var reader = new FileReader();
var file = event.target.files;
document.getElementById("errmsg").innerHTML = '';
reader.onload = function (event) {
var Stream = new Uint8Array(reader.result);
Analyst = new TPDFAnalyst();
try {
Analyst.LoadFromStream(Stream);
if (Analyst.Encrypt) {
document.getElementById("p1").innerHTML = 'PDF File - Unsupported';
document.getElementById("errmsg").innerHTML =
'It does not support encrypted files.';
} else {
document.getElementById("p1").innerHTML =
'PDF File - [' + Analyst.PageCount +
' Pages / PDF' + Analyst.Version + ']';
}
} catch (e) {
Analyst = null;
document.getElementById("p1").innerHTML = 'PDF File - Unsupported';
document.getElementById("errmsg").innerHTML = 'This File is Not supported.';
}
}
reader.readAsArrayBuffer(file[0]);
}
function run() {
var PDFKnife = new TPDFKnife();
if (Analyst != null) {
try {
PDFKnife.SaveToFile(PDF_GetDateTime_Now() + '.pdf', Analyst,1,1);
} catch (e) {
Analyst[0] = null;
Analyst[1] = null;
document.getElementById("errmsg").innerHTML =
'failed in the convert of the PDF file. ';
}
} else {
document.getElementById("errmsg").innerHTML =
'Please select a file.';
}
}
</script>
</head>
<body>
<h1>Split(extraction) of PDF</h1>
<hr>
<p>Extract the first page.</p>
<p id="errmsg" style="color:red;"></p>
<p id="p1"></p>
<input type="file" id="inputfile1" accept="application/pdf" onchange="onChangeFile(event)"><br>
<br>
<br>
<br>
<button id="run" style="width:200px;height:30px;" onclick="run();">Start</button>
</body>
</html>
Use class : TPDFAnalyst TPDFKnife
3. Remove of PDF
Remove the first page. Please select a more than one page PDF file.
var PDFDeletePage = new TPDFDeletePage(); PDFDeletePage.SaveToFile(PDF_GetDateTime_Now() + '.pdf', Analyst,1,1);
Use class : TPDFAnalyst TPDFDeletePage
4. Rotation of PDF
All of the pages will rotate right 90 degrees.
var PDFRotatePage = new TPDFRotatePage(); PDFRotatePage.SaveToFile(PDF_GetDateTime_Now() + '.pdf', Analyst,1,1,Analyst.PageCount);
Use class : TPDFAnalyst TPDFRotatePage
5. Add Bookmark of PDF
Add a bookmark of level 1 on all pages.
var PDFOutLineMaker = new TPDFOutLineMaker();
// Display settings (bookmark)
PDFOutLineMaker.View.PageMode = TPDFPageMode.pmUseOutlines;
// Adding a Root Node (parent node)
for (var i = 0; i < Analyst.PageCount; i++) {
PDFOutLineMaker.OutLine.AddRoot((i+1) +' page', (1+i), 0, '', false, false, null);
}
PDFOutLineMaker.SaveToFile(PDF_GetDateTime_Now() + '.pdf', Analyst);
Use class : TPDFAnalyst TPDFOutLineMaker
6. Property & Display settings of PDF
Change all of the document information. And makes the display settings on the thumbnail.
var PDFInfoMaker = new TPDFInfoMaker();
// Document information
PDFInfoMaker.Info.Title = 'Title';
PDFInfoMaker.Info.Subject = 'Subject';
PDFInfoMaker.Info.Author = 'Author';
PDFInfoMaker.Info.Keywords = 'Keywords';
PDFInfoMaker.Info.Creator = 'Creator';
PDFInfoMaker.Info.Producer = 'Producer';
PDFInfoMaker.Info.CreationDate = PDF_GetDateTime_Now();
PDFInfoMaker.Info.ModDate = PDF_GetDateTime_Now();
// Display settings (thumbnail)
PDFInfoMaker.View.PageMode = TPDFPageMode.pmUseThumbs;
PDFInfoMaker.SaveToFile(PDF_GetDateTime_Now() + '.pdf', Analyst);
Use class : TPDFAnalyst TPDFInfoMaker
7. Overview of each class
The main class is the class of TPDFAnalyst and various conversion. Class 22, Enum 3.
[Basic]
| Class | Use |
|---|---|
| TPDFAnalyst | Analyzes. PDF files must be read always in this class. |
| * TPDFParser | Parsing, Writing |
| * TPDFCode | Character code |
| TPDFDocInfo | Document properties |
| TPDFDocView | Display Settings |
[Stream]
| Class | Use |
|---|---|
| * TReadStream | Read-only stream |
| * TMemoryStream | Read-write stream |
| * TFileStream | Write-only stream |
[Various conversion]
| Class | Use |
|---|---|
| TPDFCombine | Combine,Merge |
| TPDFKnife | Split,Extraction |
| TPDFDeletePage | Remove |
| TPDFRotatePage | Rotate |
| TPDFInfoMaker | Property & Display settings |
| TPDFOutLineMaker | Add Bookmark |
[Display Settings]
| Class | Use |
|---|---|
| TPDFPageLayout | Page layout(Enum) |
| TPDFPageMode | Page mode(Enum) |
| TPDFOpenActionType | Display magnification(Enum) |
| TPDFOpenAction | Display magnification(Class) |
| TPDFViewerPreferences | Window options, user interface options(Class) |
[Bookmark]
| Class | Use |
|---|---|
| TPDFNode | "Node" for the bookmark |
| TPDFNodeList | "NodeList" for the bookmark |
| TPDFOutLineManager | Write bookmark |
[Other]
| Class | Use |
|---|---|
| TColor | Store the RGB values |
| TRect | Store the page size (MediaBox) |
8. Details of each class
Each class, and Enum type detail. The default is green color.
[TPDFAnalyst]
| Property | Use |
|---|---|
| Version | PDF Version(String) |
| Stream | Stream(TReadStream) |
| ObjectCount | Object Count(Int) |
| ObjectPosArray | Array of the position of object(Array) |
| DeleteObject | Deleted Objects(Array) |
| XrefTableList | Xref Table list(Array) |
| Encrypt | Encryption(true/false) |
| Optimize | Optimized for Web(true/false) |
| Tag | Tagged PDF(true/false) |
| PageCount | Page Count(Int) |
| PageSize | Page Size(TRect) |
| RootID | Root ID(Int) |
| InfoID | Info ID(Int) |
| MetadataID | Metadata ID(Int) |
| FirstPageID | FirstPage ID(Int) |
| FirstPagesID | FirstPages ID(Int) |
| OutlinesID | Outlines ID(Int) |
| Method | LoadFromStream(Uint8Array) |
|---|---|
| Use | Read a PDF file |
| Argument | Uint8Array: 8-bit array of unsigned integer value(It is a JavaScript object. Pass a binary array of files) |
| Return | None |
[TPDFParser]
It only used internally.
[TPDFCode]
It only used internally.
[TPDFDocInfo]
| Property | Use |
|---|---|
| Title | Title(String) |
| Subject | Subject(String) |
| Author | Author(String) |
| Keywords | Keywords(String) |
| Creator | Application(String) |
| Producer | PDF Conversion(String) |
| CreationDate | Creation Date(String) |
| ModDate | Mod Date(String) |
| Trapped | Trapp(true/false) |
| Method | GetPDFDocInfo(TPDFAnalyst) |
|---|---|
| Use | Set the property of class by obtaining the document properties from PDF files. |
| Return | None |
Please update the ModDate when changing only the document information. May not be displayed normally in "Acrobat Reader" when do not update it. Probably think of is reading the cache.
[TPDFDocView]
| Property | Use |
|---|---|
| PageMode | TPDFPageMode(Enum) |
| PageLayout | TPDFPageLayout(Enum) |
| OpenAction | TPDFOpenAction |
| ViewerPreferences | TPDFViewerPreferences |
| Method | GetPDFDocView(TPDFAnalyst) |
|---|---|
| Use | Set the property of class by get the display settings from PDF files. |
| Return | None |
[TReadStream]
It only used internally.
[TFileStream]
It only used internally.
[TMemoryStream]
It only used internally.
[TPDFCombine]
| Property | Use |
|---|---|
| Info | TPDFDocInfo |
| View | TPDFDocView |
| Method | SaveToFile(FileName,TPDFAnalyst of Array,CallBack) |
|---|---|
| Use | Combine of PDF |
| Argument | FileName: File name to download TPDFAnalyst of Array: TPDFAnalyst of Array CallBack: Callback function(Optional) |
| Return | None |
[TPDFKnife]
| Property | Use |
|---|---|
| Info | TPDFDocInfo |
| View | TPDFDocView |
| Method | SaveToFile(FileName,TPDFAnalyst,begin,end) |
|---|---|
| Use | Split(extraction) of PDF |
| Argument | begin,end: Set the page number you want to extraction(1,2,3...N) |
| Return | None |
[TPDFDeletePage]
| Property | Use |
|---|---|
| Info | TPDFDocInfo |
| View | TPDFDocView |
| Method | SaveToFile(FileName,TPDFAnalyst,begin,end) |
|---|---|
| Use | Remove of PDF |
| Argument | begin,end: Set the page number you want to remove(1,2,3...N) |
| Return | None |
[TPDFRotatePage]
| Method | SaveToFile(FileName,TPDFAnalyst,Rotate,begin,end) |
|---|---|
| Use | Rotation of PDF |
| Argument | Rotate: Specify of 1-3 (1: Right 90 degrees 2: Left 90 degrees 3: 180 degrees) begin,end: Set the page number you want to Rotation(1,2,3...N) |
| Return | None |
[TPDFInfoMaker]
| Property | Use |
|---|---|
| Info | TPDFDocInfo |
| View | TPDFDocView |
| Method | SaveToFile(FileName,TPDFAnalyst) |
|---|---|
| Use | Property & Display settings of PDF |
| Return | None |
[TPDFOutLineMaker]
| Property | Use |
|---|---|
| Info | TPDFDocInfo |
| View | TPDFDocView |
| OutLine | TPDFOutLineManager |
| Method | SaveToFile(FileName,TPDFAnalyst) |
|---|---|
| Use | Add Bookmark of PDF. Add a node in OutLine property. |
| Return | None |
[TPDFPageLayout]
| Enum | Use |
|---|---|
| plDefault | Default |
| plSinglePage | Single page |
| plOneColumn | Continuous page |
| plTwoColumnLeft | Facing page(left) |
| plTwoColumnRight | Facing page(right) |
[TPDFPageMode]
| Enum | Use |
|---|---|
| pmDefault | Default |
| pmUseNone | None |
| pmUseOutlines | Bookmark and page |
| pmUseThumbs | Thumbnail and page |
| pmFullScreen | Full screen |
[TPDFOpenActionType]
| Enum | Use |
|---|---|
| oaDefault | Default |
| oaXYZ | Display magnification. It is necessary to set the value of TPDFOpenAction.Zoom If you select this item |
| oaFit | Entire display |
| oaFitH | Fit width |
| oaFitV | (unused) |
| oaFitR | (unused) |
| oaFitB | (unused) |
| oaFitBH | Fit drawing area |
| oaFitBV | (unused) |
[TPDFOpenAction]
| Property | Use |
|---|---|
| Left | (unused) |
| Top | (unused) |
| Right | (unused) |
| Bottom | (unused) |
| Zoom | Specifying the display magnification. specify one of the 0.25, 0.5, 0.75, 1, 1.25, 1.5, 2, 4, 8, 16. Default : -1 |
| ObjectID | Object number of the page to display first. (Default: -1 First page is displayed) |
| GenerationID | Generation number of the page to display first. Specified with the ObjectID property. (Default: -1 First page is displayed) Hint? |
| SubType | TPDFOpenActionType(Enum) |
[TPDFViewerPreferences]
| Property | Use |
|---|---|
| HideToolbar | Hide Toolbar(true/false) |
| HideMenubar | Hide Menubar(true/false) |
| HideWindowUI | Hide window controls(true/false) |
| FitWindow | Adjust the window size to the page(true/false) |
| CenterWindow | Place the window in the middle of the screen(true/false) |
| Direction | Binding(right)(true/false) |
| DisplayDocTitle | Display the title of the document(true/false) |
| NonFullScreenPageMode | TPDFPageMode(After full screen) Default:pmDefault |
[TPDFNode]
| Property | Use |
|---|---|
| Page | Page number(1,2,3 ... N) (Int) |
| ShowPos | Coordinate position of the page(Basic is 0 recommended) (Int) |
| Caption | Bookmark title(String) |
| URL | URL(Page property is valid at the time of -1. Click the bookmark to open the URL) (String) |
| Level | Hierarchy level. Parent node level 0 Child-grandchild nodes can be up to Level 4 (Int) |
| Italic | Italic(true/false) |
| Bold | Bold(true/false) |
| Color | Caption Color(TColor) Specify a null if not needed |
| Parent | Parent node(TPDFNode) |
| ChildNodes | Child node(TPDFNodeList) |
| Method | Get(Index) |
|---|---|
| Use | Get the child node |
| Return | TPDFNode |
| Method | AddChild(Caption, Page, ShowPos, URL, Bold, Italic, Color) |
|---|---|
| Use | Add a child node |
| Argument | TPDFNode See the property |
| Return | TPDFNode |
| Method | GetCount() |
|---|---|
| Use | Get the number of child nodes |
| Return | Number of child nodes |
| Method | GetChildCount() |
|---|---|
| Use | Get the total number of child node and a grandchild node |
| Return | Total number of child node and a grandchild node |
[TPDFNodeList]
| Method | Get(Index) |
|---|---|
| Use | Get the node |
| Return | TPDFNode |
| Method | Add(Parent, Level, Caption, Page, ShowPos, URL, Bold, Italic, Color) |
|---|---|
| Use | Add a node |
| Argument | TPDFNode See the property |
| Return | TPDFNode |
| Method | GetCount() |
|---|---|
| Use | Get the number of nodes |
| Return | Number of nodes |
[TPDFOutLineManager]
| Property | Use |
|---|---|
| Node | List of the parent node(TPDFNodeList) |
| Method | Get(Index) |
|---|---|
| Use | Get the parent node |
| Return | TPDFNode |
| Method | AddRoot(Caption, Page, ShowPos, URL, Bold, Italic, Color) |
|---|---|
| Use | Add a parent node |
| Argument | TPDFNode See the property |
| Return | TPDFNode |
| Method | GetCount() |
|---|---|
| Use | Get the number of the parent node |
| Return | Number of parent node |
| Method | GetNodeCount() |
|---|---|
| Use | Get the total number of nodes of all (including parent-child-grandchild) |
| Return | Total number of all (including parent-child-grandchild) |
[TColor]
| Property | Use |
|---|---|
| Red | Red. numerical value of 0-255 |
| Green | Green. numerical value of 0-255 |
| Blue | Blue. numerical value of 0-255 |
Use the PDF_RGB() common function to generate this class.
[TRect]
| Property | Use |
|---|---|
| Left | Left |
| Top | Top |
| Right | Right |
| Bottom | Bottom |
9. Common Functions
| Function | PDF_GetDateTime_Now() |
|---|---|
| Use | Get the current time(yyyymmddhhmmss) |
| Return | Current time |
| Function | PDF_RGB(Red, Green, Blue) |
|---|---|
| Use | Return the TColor from RGB values |
| Argument | Red, Green, Blue: numerical value of 0-255 |
| Return | TColor |
10. Callback function
| Function | function PDFCallBack(value, filepos, files){} |
|---|---|
| Use | Callback function upon binding of the PDF file. |
| Argument | value: Current progress(Max 100) filepos: Current file number(1,2,3...N) files: Total number of files |
| Return | None |
Hidden function
Here there may be a hidden method of functions and classes that are not introduced :-)
Advertisements