41
// Adding a link to an embedded file is
// easy using the AddLinkToEmbeddedFile function.
QP.AddLinkToEmbeddedFile(200, 100, 80, 20, EmbeddedFileID , "My embedded file", 1);
// Hyperlinks and text are two separate
// elements in a PDF, so we'll draw some
// text now so that you know where the
// hyperlink is located on the page.
QP.DrawText(205, 114, "Double-Click Me!");
// When the QPL object is initiated a blank document
// is created and selected in memory by default. So
// all we need to do now is save the document to
// the local hard disk to see the changes that we've made.
QP.SaveToFile("link_to_embedded_file.pdf");
Add link to link to JavaScript
Use the AddLinkToJavaScript function to add a link to a JavaScript action.
/* Add a link that executes a snippet of JavaScript when clicked on */
// Load a PDF with JavaScript
QP.LoadFromFile("doc_with_javascript.pdf", “”);
// Use the AddLinkToJavaScript function to add a link
// that executes some JavaScript when clicked on
QP.AddLinkToJavaScript(220, 570, 190, 210, 'app.alert("Debenu Quick PDF Library
rocks",3,0,"Something you should know...");',1);
// Set the opening page and zoom factor
// for our sample file.
QP.SetOpenActionDestination(1, -1);
// Save the file to disk in the output folder.
QP.SaveToFile("link_to_javascript.pdf");
Add link to a destination
Use the AddLinkToDestination function to add a link to a destination in the same document. The
target page, position and zoom level are specified by a destination object which can be created
with the NewDestination function.
/* Add a link a link to a destination in the same document*/
// Set page origin to top left
QP.SetOrigin(1);
// Add a 2nd page to the default 1 page doc
QP.NewPages(1);
// Add a new destination on page 2, draw some text too
DestID = QP.NewDestination(2, 0, 7, 0, 200, 0, 0);
QP.DrawText(300, 300, "Page 2 Destination");
// Select page 1 again
QP.SelectPage(1);
41
// Use the AddLinkToDestination function to add
// a link to the destination that we've previously created
QP.AddLinkToDestination(200, 100, 60, 20, DestID, 1);
// Hyperlinks and text are two separate
// elements in a PDF, so we'll draw some
// text now so that you know where the
// hyperlink is located on the page.
QP.DrawText(205, 114, "Click me!");
// Save the file to disk.
QP.SaveToFile("link_to_destination.pdf");
Add link to named destination
Use the AddLinkToDestination and NewNamedDestination functions to add a link to a named
destination in the same document. The target page, position and zoom level are specified by a
destination object which can be created with the NewDestination function.
/* Add a link a link to a named destination in the same document */
// Set page origin to top left and add
// a 2nd page to the default 1 page doc
QP.SetOrigin(1);
QP.NewPages(1);
// Add a new destination on page 2
DestID = QP.NewDestination(2, 0, 7, 0, 200, 0, 0);
// Add a named destination reference to the
// destination object that we've just created
QP.NewNamedDestination("MyNamedDestination", DestID);
// Draw some text so we know where we are
QP.DrawText(300, 300, "Page 2 Named Destination");
// Select page 1 again
QP.SelectPage(1);
// Use the AddLinkToDestination function to add
// a link to the destination that we've previously created
QP.AddLinkToDestination(200, 100, 60, 20, DestID, 1);
// Hyperlinks and text are two separate
// elements in a PDF, so we'll draw some
// text now so that you know where the
// hyperlink is located on the page.
QP.DrawText(205, 114, "Click me!");
// Save the file to disk
QP.SaveToFile("link_to_destination.pdf");
PDF Bookmarks (Outlines)
Debenu Quick PDF Library provides extensive support for adding, editing and removing
bookmarks from PDF files. In the PDF specification bookmarks are technically referred to as
38
outlines and this is the terminology that we have used when naming our bookmark related
functions but in this guide we have referred to them as bookmarks.
Add new bookmarks to a PDF
Use the NewOutline function to add new bookmarks to a PDF.
/* Add new bookmarks to a PDF */
// Load a PDF with multiple pages
QP.LoadFromFile("100_pages.pdf", "");
// Add a new parent bookmark that links to page 50
outlineID1 = QP.NewOutline(0, "Bookmark to page 50", 50, QP.PageHeight());
// Now lets add a child bookmark to the parent bookmark and
// point that to page 100
outlineID2 = QP.NewOutline(outlineID1, "Bookmark to page 100", 100, QP.PageHeight());
// It's also possible to manipulate the style of bookmarks a little
// so lets try adding a little bold text and a bright color to the
// child bookmark that we just created
QP.SetOutlineColor(outlineID2, 0, 192, 0);
// It's not just other pages that you can add bookmarks to.
// Bookmarks can open web links, trigger JavaScript, open external
// files and other similar actions. Lets add a bookmark web link to finish.
outlineID3 = QP.NewStaticOutline(0, "Debenu Quick PDF Library Website");
QP.SetOutlineWebLink(outlineID3, "http://www.quickpdflibrary.com/");
// Finally, lets change the initial view settings so that
// when the PDF opens in Adobe Reader that it displays
// the bookmarks panel by default
QP.SetPageMode(1);
// Save the updated PDF to disk
QP.SaveToFile("100_pages_with_bookmarks.pdf");
Remove all bookmarks from a PDF
Use the the OutlineCount, RemoveOutline, GetFirstOutline, GetFirstChildOutline and
GetNextOutline functions to remove all bookmarks from a PDF.
/* Remove all bookmarks from a PDF */
// Load a PDF that has some bookmarks
QP.LoadFromFile("bookmarks.pdf", "");
// Count all bookmarks in the PDF
totalOutlines = QP.OutlineCount();
// Run through each bookmark in the PDF
// starting with the parent bookmark
// and then walking through the child
44
// bookmarks.
for (x = 1; x <= totalOutlines; x++)
{
parentOutline = QP.GetFirstOutline();
childOutline = QP.GetFirstChildOutline(parentOutline);
while (QP.GetNextOutline(childOutline) != 0)
{
nextOutline = QP.GetNextOutline(childOutline);
QP.RemoveOutline(nextOutline);
}
QP.RemoveOutline(childOutline);
QP.RemoveOutline(parentOutline);
}
// Save the PDF with no bookmarks to disk
QP.SaveToFile("no_bookmarks.pdf");
/* Remove all bookmarks from a PDF */
// Load a PDF that has some bookmarks
QP.LoadFromFile("bookmarks.pdf", "");
// Count all bookmarks in the PDF
totalOutlines = QP.OutlineCount();
// Run through each bookmark in the PDF
// starting with the parent bookmark
// and then walking through the child
// bookmarks.
for (x = 1; x <= totalOutlines; x++)
{
parentOutline = QP.GetFirstOutline();
childOutline = QP.GetFirstChildOutline(parentOutline);
while (QP.GetNextOutline(childOutline) != 0)
{
nextOutline = QP.GetNextOutline(childOutline);
QP.RemoveOutline(nextOutline);
}
QP.RemoveOutline(childOutline);
QP.RemoveOutline(parentOutline);
}
// Save the PDF with no bookmarks to disk
QP.SaveToFile("no_bookmarks.pdf");
Find and list all bookmarks
Use the OutlineCount and OutlineTitle functions to find and list all bookmarks found in a PDF.
/* Find all bookmarks in a document and display them in a message box */
// Load the sample file from the input folder
QP.LoadFromFile("bookmarks.pdf", “”);
iMainDocID = QP.GetDocumentID(QP.DocumentCount());
18
// Count the number of pages
iFilePages = QP.PageCount();
// Count the number of bookmarks
iNumOutlines = QP.OutlineCount();
// Declare the variable that we'll use for storing the bookmark titles
DocName = "";
// Cycle through each bookmark, saving the chunk as a separate file
for (n = 1; n < iNumOutlines; n++)
{
// Select the main document
QP.SelectDocument(iMainDocID);
// Select the current bookmark
iOutlineID = QP.GetOutlineID(n);
// Get the bookmark title
DocName = DocName + QP.OutlineTitle(iOutlineID) + "\n";
}
// Display a message box with all the bookmarks listed
MsgBox(DocName);
40
PDF Fonts
Debenu Quick PDF Library provides support for a wide range of different fonts, enabling you to
embed and subset fonts. Support for Unicode characters is also provided.
Add a TrueType font
Use the AddTrueTypeFont function to embed a TrueType font in a PDF.
/* Embed a TrueType font within a PDF */
// Use the AddTrueTypeFont function to add a font to
// the default blank document and get the return
// value which is the font ID.
fontID1 = QP.AddTrueTypeFont("Arial Rounded MT Bold", 1);
// Select the font using its font ID
QP.SelectFont(fontID1);
// Draw some text onto the document to see if
// everything is working OK.
QP.DrawText(100, 700, "Arial Rounded MT Bold");
// Repeat exercise to see what a couple of other
// fonts will look like as well.
fontID2 = QP.AddTrueTypeFont("Times New Roman", 1);
QP.SelectFont(fontID2);
QP.DrawText(100, 650, "Times New Roman");
fontID3 = QP.AddTrueTypeFont("Century Gothic", 1);
QP.SelectFont(fontID3);
QP.DrawText(100, 600, "Century Gothic");
// Save the new document to the output folder.
QP.SaveToFile("embedded_fonts.pdf");
Add a standard font
Use the AddStandardFont function to embed a standard font to a PDF.
/* Add a Windows standard font to a PDF */
// Use the AddStandardFont function to add a font to
// the default blank document and get the return
// value which is the font ID.
fontID1 = QP.AddStandardFont(0);
// Select the font using its font ID
QP.SelectFont(fontID1);
// Draw some text onto the document to see if
// everything is working OK.
QP.DrawText(100, 700, "Courier");
// Repeat exercise to see what a couple of other
// fonts will look like as well.
fontID2 = QP.AddStandardFont(1);
40
QP.SelectFont(fontID2);
QP.DrawText(100, 650, "CourierBold");
fontID3 = QP.AddStandardFont(2);
QP.SelectFont(fontID3);
QP.DrawText(100, 600, "CourierBoldOblique");
fontID4 = QP.AddStandardFont(3);
QP.SelectFont(fontID4);
QP.DrawText(100, 550, "Helvetica");
fontID5 = QP.AddStandardFont(4);
QP.SelectFont(fontID5);
QP.DrawText(100, 500, "HelveticaBold");
// Save the new document to the output folder.
QP.SaveToFile("embedded_standard_fonts.pdf");
Add a subsetted font
Use the AddSubsettedFont function to embed a subset of a font in a PDF. This means that
only the font information for the specified characters will be embedded, reducing the size of the
document.
/* Add subsetted text to a PDF */
// Our string of Unicode text goes here
drawstr = "Hello World";
// Add a subset font for the text string
QP.AddSubSettedFont("Verdana", 1, drawstr);
// Remap the string to ensure that the correct character
// codes are used.
substr = QP.GetSubsetstring(drawstr);
// Draw the Unicode text onto the page
QP.DrawText(100, 600, substr);
// Save the new file to disk
QP.SaveToFile("subsetted_text.pdf");
Add a subsetted font with Unicode text
Unicode text can be added to PDFs using the AddSubsettedFont function.
/* Add subsetted Unicode text to a PDF */
// Our string of Unicode text goes here
drawstr = "你好";
// Add a subset font for the text string
QP.AddSubSettedFont("Verdana", 7, drawstr);
// Remap the string to ensure that the correct character
// codes are used.
substr = QP.GetSubsetstring(drawstr);
// Draw the Unicode text onto the page
36
QP.DrawText(100, 600, substr);
// Save the new file to disk
QP.SaveToFile("unicode_text.pdf");
Add a Type 1 font
Use the AddType1Font function to embed PostScript Type 1 fonts in a PDF.
/* Add a Type 1 font to a PDF */
// Load and add the Type 1 font using the
// AddType1Font function. Read function
// description for full requirements.
FontID = QP.AddType1Font("Allandale.PFM");
// Select the font that we've just added
QP.SelectFont(FontID);
// Set page origin to top left
QP.SetOrigin(1);
// Draw some sample text
QP.DrawText(100, 100, "Hello World");
// Save the new file to disk
QP.SaveToFile("embedded_type1_font.pdf");
Add a CJK font
Use the AddCJKFont function to add a Chinese, Japanese or Korean font to a PDF. Read the
function description for full information.
/* Add a CJK font to a PDF */
// Add a CJK font and select it
FontID = QP.AddCJKFont(1);
QP.SelectFont(FontID);
// Specify text to be drawn
InputText = "中文文本"; // Chinese text
// Convert string of text to Unicode
UnicodeInputText = QP.ToPDFUnicode(InputText);
// Draw the string of text onto the PDF
QP.DrawText(100, 600, UnicodeInputText);
// Save the new file to disk
QP.SaveToFile("cjk_font.pdf");
Check PDF for font data
The HasFontResources function can be used to check a PDF for any font resources. If the PDF
does not have any font resources then it can be assumed to be an image only PDF.
Documents you may be interested
Documents you may be interested