Getting Started with ArtX Annotations
The following tutorial explains how to add a text annotation as a watermark to a PDF document.
At the top of your main program module, you need to place the main ArtX public header file. It is required for the ArtX prototypes and macros:
If you plan on using ArtX's GUI tools, including the toolbar and property you need to include:
Before you can load an image that can be marked up using ArtX functions, you need to attach the ArtX component using the ImageGear function IG_comm_comp_attach(). If you plan on working with PDF Annotations, then in addition to the PDF component, you will need to attach the Vector and U3D components (see Attaching Components).
|
Copy Code
|
// Attach ArtX component.
if ((IG_comm_comp_attach("ArtX") != IGE_SUCCESS))
{
// This error usually occurs because the DLL cannot be found.
cout << "ArtX component error." << endl;
return false;
}
// Attach U3D component.
if ((IG_comm_comp_attach("U3D") != IGE_SUCCESS))
{
// This error usually occurs because the DLL cannot be found.
cout << "U3D component error." << endl;
return false;
}
// Attach Vector component.
if ((IG_comm_comp_attach("Vect") != IGE_SUCCESS))
{
// This error usually occurs because the DLL cannot be found.
cout << "Vector component error." << endl;
return false;
}
// Attach and initialize the PDF component.
if ((IG_comm_comp_attach("PDF") != IGE_SUCCESS) ||
(IG_PDF_initialize(NULL) != IGE_SUCCESS))
{
// This error usually occurs because a DLL or the Resource directory
// cannot be found.
cout << "PDF component error." << endl;
return false;
}
|
The following method loads in a PDF document and iterates through all of the pages, putting a “Draft” text annotation on each page. The PDF document is then saved out with the PDF annotation on every page.
|
Copy Code
|
void ApplyTextWatermarkToAllPages(const string& fileIn)
{
const int BorderThicknessInPixels = 2;
const int OpacityTranslucent = 100;
const AT_DIMENSION MaxHeightInImagePixels = 200;
// Load the multi-page document.
HMIGEAR document = 0;
IG_mpi_create(&document, 0);
IG_mpi_file_open((LPSTR)fileIn.c_str(), document, IG_FORMAT_PDF,
IG_MP_OPENMODE_READONLY);
// Get the page count.
unsigned int pageCount = 0;
IG_mpf_page_count_get(document, &pageCount);
for (unsigned int pageNumber = 0; pageNumber < pageCount; pageNumber++)
{
// Get a handle to the page.
HIGEAR page = 0;
IG_mpi_page_get(document, pageNumber, &page);
// Get the dimensions of the image and watermark text.
AT_DIMENSION imageWidth = 0;
AT_DIMENSION imageHeight = 0;
UINT bitsPerPixel = 0;
IG_image_dimensions_get(page, &imageWidth, &imageHeight, &bitsPerPixel);
AT_DIMENSION boxWidth = imageWidth;
AT_DIMENSION boxHeight = (std::min)(MaxHeightInImagePixels, imageHeight);
// Choose a font size that is 85% height of the box height.
float fontSize = boxHeight * 0.85f;
// Create the ArtX page.
HIG_ARTX_PAGE artPage = 0;
IG_ARTX_page_create(&artPage);
// Create the text mark.
// X and Y coordinates to place image.
AT_DIMENSION x = 1;
AT_DIMENSION y = (imageHeight - boxHeight) / 2;
AT_RECTANGLE rectangle = { x, y, boxWidth, boxHeight };
AT_ARTX_FONT font = { fontSize, IG_ARTX_FONT_REGULAR, FALSE, "Arial" };
AT_ARTX_BORDER border = { IG_ARTX_PEN_SOLID, BorderThicknessInPixels,
{128, 128, 128, 0} };
AT_BOOL borderShading = FALSE;
AT_RGBQUAD fillColor = { 0, 0, 255, 0 };
AT_RGBQUAD textColor = { 0, 0, 0, 0 };
HIG_ARTX_TEXT mark = NULL;
IG_ARTX_text_create(&rectangle, "Draft", IG_ARTX_TEXT_DIRECT_TEXT, &font,
&border, borderShading, &fillColor, &textColor, OpacityTranslucent,
NULL, NULL, &mark);
// Add the text mark to the page.
HIG_ARTX_TEXT markInPage = NULL;
IG_ARTX_page_mark_add(artPage, mark, IG_ARTX_COORD_IMAGE, &markInPage);
IG_ARTX_page_save(artPage, page);
IG_ARTX_page_delete(artPage);
IG_ARTX_mark_delete(mark);
}
// Save the file out with the new text watermark.
const UINT firstPage = 0;
IG_mpi_file_save("WatermarkText.pdf", document, firstPage, 0, pageCount,
IG_FORMAT_PDF, IG_MPI_SAVE_OVERWRITE);
// Cleanup the document and its pages.
IG_mpi_delete(document);
}
|
Call this routine from your main function in your sample application passing in the path to a PDF document. Now you can compile and run your sample application. To see this code in a working sample, please see the AddWatermarkToPDF sample.