Skip to content

Instantly share code, notes, and snippets.

@datalogics-pgallot
Created May 12, 2016 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save datalogics-pgallot/ce98a0551e9ebfc2120413ecbd336d96 to your computer and use it in GitHub Desktop.
Save datalogics-pgallot/ce98a0551e9ebfc2120413ecbd336d96 to your computer and use it in GitHub Desktop.
APDFL sample app: draw a part of a page to memory
/*
DrawToMemory - Sample for the Adobe PDF Library distributed by Datalogics.
Copyright (c) 2007-2016, Datalogics, Inc. All rights reserved.
This sample code is licensed under the terms listed at
http://dev.datalogics.com/adobe-pdf-library/license-for-downloaded-pdf-samples/
This PDF Library sample demonstrates the process of rasterizing a PDF page
and placing the resulting raster as an image into a different PDF document.
This file contains functions for the DrawToMemory class.
*/
#include "drawtomemory.h"
DrawToMemory::DrawToMemory(PDPage &pdPage, char *colorSpace, char *filterName, ASInt32 inBPC, float inResolution, ASFixedRect *pUpdateRect)
{
//If you are using a decode filter such as FlateDecode, the filterArray values will be set here
//
filterArray = SetFilter(filterName);
//Set the resolution. In terms of PDF settings, the default resolution is 72 units per inch. A resolution
//value of 72.0, then, is the same as the default. To double the resolution, set the value to 144.0, e.g.
resolution = SetResolution(inResolution);
//Get the colorspace atom, set the number of components per colorspace
//and store the appropriate colorspace for an output PDEImage
csAtom = SetColorSpace(colorSpace);
//The stream of data being rasterized to memory is divided into units of n bits each, where n is the
//number of bits per component.
bpc = SetBPC(inBPC);
PDPageGetCropBox(pdPage, &pageRect);
pgRotation = PDPageGetRotate(pdPage);
updateRect = *pUpdateRect;
SetDestRect(updateRect);
PDPageGetFlippedMatrix(pdPage, &matrix);
ASFixedMatrix translateM = { fixedOne, 0, 0, fixedOne, 0, 0 };//initialized to identity matrix
switch (pgRotation)//because of flipped matrix mirroring...
{
case pdRotate270:
translateM.h = -(pageRect.top - updateRect.top);
translateM.v = -(pageRect.right - updateRect.right);
break;
case pdRotate180:
translateM.h = -(pageRect.right - updateRect.right );
translateM.v = -(updateRect.bottom - pageRect.bottom);
break;
case pdRotate90:
translateM.h = -(updateRect.bottom - pageRect.bottom);
translateM.v = -(updateRect.left - pageRect.left);
break;
case pdRotate0:
default:
translateM.h = -(updateRect.left - pageRect.left);
translateM.v = -(pageRect.top - updateRect.top);
break;
}
ASFixedMatrixConcat(&matrix, &translateM, &matrix);
//Set the scale matrix that will be concatenated to the user space matrix
//
scaleMatrix = SetScaleMatrix(resolution);
ASFixedMatrixConcat(&matrix, &scaleMatrix, &matrix);
ASFixedMatrixTransformRect(&scaledDestRect, &scaleMatrix, &destRect);
//Allocate and initialize the buffer to store the rendered page content
//
bufferSize = SetBufferSize(pdPage, matrix, csAtom, bpc, scaledDestRect);
buffer = new char[bufferSize];
if (csAtom == ASAtomFromString("DeviceRGB"))
memset(buffer, 0x7f, bufferSize); // Initialize to an RGB background of white.
else if (csAtom == ASAtomFromString("DeviceCMYK"))
memset(buffer, 0x00, bufferSize); // Initialize to a CMYK background of white.
else if (csAtom == ASAtomFromString("DeviceGray"))
memset(buffer, 0xff, bufferSize); // Initialize to a DeviceGray background of white.
// Leave the memory buffer uninitialized for non-Device color spaces.
// Render page content to the bitmap buffer
//
PDPageDrawContentsToMemory(pdPage,kPDPageDoLazyErase | kPDPageUseAnnotFaces,
&matrix, pUpdateRect, kPDPageDrawSmoothText | kPDPageDrawSmoothLineArt | kPDPageDrawSmoothImage,
csAtom, bpc, &scaledDestRect, buffer, bufferSize, NULL, NULL);
// Set up attributes for the PDEImage made by MakePDEImage - these attributes are also
// used to repad the output buffer (from 32-bit row alignment to 8-bit alignment) in
// the PadCompute function below
SetImageAttrs(scaledDestRect, bpc);
//If the number of data bits per row is not a multiple of 8, the end of the row is padded with extra bits
//to fill out the last byte. A PDF consumer application ignores these padding bits.
attrs.width = PadCompute(attrs, bpc, nComps, buffer, bufferSize);
}
DrawToMemory::~DrawToMemory()
{
if(buffer)
{
delete[] buffer;
}
buffer = 0;
PDERelease(reinterpret_cast<PDEObject>(image));
PDERelease(reinterpret_cast<PDEObject>(cs));
}
char * DrawToMemory::GetImageBuffer()
{
return buffer;
}
ASInt32 DrawToMemory::GetImageBufferSize()
{
return bufferSize;
}
ASFixedRect DrawToMemory::GetImageRect()
{
return destRect;
}
PDEImage DrawToMemory::MakePDEImage()
{
//Prepare the image attributes
//
attrs = SetImageAttrs(scaledDestRect, bpc);
//Create the image matrix using the height/width attributes and apply the resolution.
//
imageMatrix = SetImageMatrix(attrs, resolution);
// Create an image XObject from the bitmap buffer to embed in the output document
//
image = PDEImageCreate(&attrs, sizeof(attrs),
&imageMatrix, 0,
cs, NULL,
&filterArray, 0,
(unsigned char*) buffer, bufferSize);
return image;
}
/* * * Internal (private) methods used in the above * * */
PDEImageAttrs DrawToMemory::SetImageAttrs(ASFixedRect scaledDestRect, ASInt32 bpc)
{
memset(&attrs, 0, sizeof(PDEImageAttrs));
attrs.flags = kPDEImageExternal;
attrs.height = abs(ASFixedRoundToInt16(scaledDestRect.top) - ASFixedRoundToInt16(scaledDestRect.bottom));
attrs.width = abs(ASFixedRoundToInt16(scaledDestRect.right) - ASFixedRoundToInt16(scaledDestRect.left));
attrs.bitsPerComponent = bpc;
return attrs;
}
PDEFilterArray DrawToMemory::SetFilter(char *filterName)
{
memset(&filterArray, 0, sizeof(PDEFilterArray));
if(filterName)
{
filterArray.numFilters = 1;
filterArray.spec[0].name = ASAtomFromString(filterName);
}
return filterArray;
}
ASFixedMatrix DrawToMemory::SetImageMatrix(PDEImageAttrs attrs, float resolution)
{ //applying the proportional resolution width & height to the image matrix.
imageMatrix.a = FloatToASFixed(attrs.width / (resolution / 72.0));
imageMatrix.d = FloatToASFixed(attrs.height / (resolution / 72.0));
imageMatrix.b = imageMatrix.c = 0;
imageMatrix.h = imageMatrix.v = 0;
return imageMatrix;
}
ASFixedMatrix DrawToMemory::SetScaleMatrix(float resolution)
{ //Create a matrix to use to increase or decrease the default matrix via concatenation
scaleMatrix.a = scaleMatrix.d = FloatToASFixed(resolution / 72.0);
scaleMatrix.b = scaleMatrix.c = scaleMatrix.h = scaleMatrix.v = 0;
return scaleMatrix;
}
ASFixedRect DrawToMemory::SetDestRect(ASFixedRect newRect)
{
// ASFixedRect: left, top, right, bottom:
ASFixedRect tmpRect = { 0, newRect.top - newRect.bottom, newRect.right - newRect.left, 0 };
ASFixedRect tmpRotdRect = { tmpRect.bottom, tmpRect.right,tmpRect.top, tmpRect.left };
switch (pgRotation)
{
case pdRotate90:
case pdRotate270:
destRect = tmpRotdRect;
break;
case pdRotate180:
case pdRotate0:
default:
destRect = tmpRect;
}
return destRect;
}
ASAtom DrawToMemory::SetColorSpace(char *colorSpace)
{ //initialize colorspace atoms and set the channels per color
if(!strcmp(colorSpace,"DeviceGray")){
sDeviceGray_K = ASAtomFromString("DeviceGray");
csAtom = sDeviceGray_K;
nComps = 1;
}
else if(!strcmp(colorSpace,"DeviceRGB")){
sDeviceRGB_K = ASAtomFromString("DeviceRGB");
csAtom = sDeviceRGB_K;
nComps = 3;
}
else if(!strcmp(colorSpace,"DeviceCMYK")){
sDeviceCMYK_K = ASAtomFromString("DeviceCMYK");
csAtom = sDeviceCMYK_K;
nComps = 4;
} else {
// Not a valid colorspace
ASRaise(genErrBadParm);
}
// initialize the output colorspace for the PDEImage we'll generate in MakePDEImage
cs = PDEColorSpaceCreateFromName(csAtom);
return csAtom;
}
ASInt32 DrawToMemory::SetBPC(ASInt32 bitsPerComp)
{ //bpc must be set properly, depending on the colorspace being used
bpc = bitsPerComp;
if(csAtom==sDeviceRGB_K || csAtom==sDeviceCMYK_K)
{
if(bitsPerComp!=8)
{
printf("Resetting incorrect BPC value of %d ", bitsPerComp);
bpc = 8;
printf("to %d for the chosen colorspace...\n", bpc);
}
}
if(csAtom==sDeviceGray_K)
{
if(bitsPerComp == 1 || bitsPerComp == 8 || bitsPerComp == 24)
{}else{
printf("Resetting incorrect BPC value of %d ", bitsPerComp);
bpc = 8;
printf("to an acceptable value of %d for the chosen colorspace...\n", bpc);
}
}
return bpc;
}
ASInt32 DrawToMemory::PadCompute(PDEImageAttrs attrs, ASInt32 bpc, ASInt32 nComps, char *buffer, ASInt32 bufferSize)
{ // The bitmap data generated by PDPageDrawContentsToWindow is 32-bit aligned.
// The PDF image operator expects, however, 8-bit aligned image data.
// To remedy this difference, we check to see if the 32-bit aligned width
// is different from the 8-bit aligned width. If so, we fix the image data by
// stripping off the padding at the end
//
if (((((attrs.width * bpc * nComps) + 31) / 32) * 4) != ((attrs.width * bpc * nComps) / 8))
{
char *src, *dest; // temporary pointers to the bitmap data buffer
// created by PDPageDrawContentsToMemory
int sw, dw;
sw = ((((attrs.width * bpc * nComps) + 31) / 32) * 4);
if (bpc == 1)
dw = attrs.width / 8 + ((attrs.width % 8) ? 1 : 0);
else
dw = (attrs.width * bpc * nComps) / 8;
src = dest = buffer;
//
// Copy the source bytes to the destination
//
for (int i = 0; i < attrs.height; i++) {
for (int j = 0; j < dw; j++)
dest[j] = src[j];
src += sw; dest += dw;
}
//
// Recalculate buffer size
//
bufferSize = dw * attrs.height;
} else {
attrs.width = (((( attrs.width* bpc * nComps) + 31) / 32) * 4) * 8 / (bpc * nComps);
}
return attrs.width;
}
ASInt32 DrawToMemory::SetBufferSize(PDPage pdPage, ASFixedMatrix &matrix, ASAtom csAtom, ASInt32 bpc, ASFixedRect scaledDestRect)
{ //calculate buffer size needed to for page contents
bufferSize = PDPageDrawContentsToMemory(pdPage, kPDPageDoLazyErase,
&matrix, &updateRect, 0, csAtom,
bpc,&scaledDestRect,NULL,
0, NULL, NULL);
return bufferSize;
}
float DrawToMemory::SetResolution(float inResolution)
{ //Correct for a resolution lower than or equal to zero
if(inResolution<=0.0)
resolution=72.0;
else
resolution = inResolution;
return resolution;
}
/*
DrawToMemory - Sample for the Adobe PDF Library distributed by Datalogics.
Copyright (c) 2007-2016, Datalogics, Inc. All rights reserved.
This sample code is licensed under the terms listed at
http://dev.datalogics.com/adobe-pdf-library/license-for-downloaded-pdf-samples/
This PDF Library sample demonstrates the process of rasterizing a PDF page
and placing the resulting raster as an image into a different PDF document.
This file contains declarations for the DrawToMemory class.
*/
#ifndef MAC_PLATFORM
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#endif
#ifdef MAC_ENV
#include "MacUtils.h"
#endif
#include <iostream>
#include <string>
#include "PDFInit.h"
#include "CosCalls.h"
#include "ASCalls.h"
#include "PDCalls.h"
#include "PSFCalls.h"
#include "PERCalls.h"
#include "PEWCalls.h"
#include "PagePDECntCalls.h"
#include "MyPDFLibUtils.h"
static ASAtom sDeviceRGB_K, sDeviceCMYK_K, sDeviceGray_K; //to hold colorspace ASAtoms
class DrawToMemory
{
private:
PDPage pdPage;
PDEImage image;
PDEImageAttrs attrs;
PDEColorSpace cs;
PDEFilterArray filterArray;
ASFixedMatrix matrix;
ASFixedMatrix scaleMatrix;
ASFixedMatrix imageMatrix;
ASFixedRect destRect;
ASFixedRect scaledDestRect;
ASFixedRect pageRect;
ASAtom csAtom;
ASInt32 nComps;
ASInt32 bufferSize;
ASInt32 bpc;
char* buffer;
char* colorSpace;
char* filterName;
float resolution;
ASFixedRect updateRect;
PDRotate pgRotation;
PDEImageAttrs SetImageAttrs(ASFixedRect scaledDestRect, ASInt32 bpc);
PDEFilterArray SetFilter(char *filterName);
ASFixedMatrix SetImageMatrix(PDEImageAttrs attrs, float resolution);
ASFixedMatrix SetScaleMatrix(float resolution);
ASFixedRect SetDestRect(ASFixedRect destRect);
ASAtom SetColorSpace(char *colorSpace);
ASInt32 SetBPC(ASInt32 bitsPerComp);
ASInt32 PadCompute(PDEImageAttrs attrs, ASInt32 bpc, ASInt32 nComps, char *buffer, ASInt32 bufferSize);
ASInt32 SetBufferSize(PDPage pdPage, ASFixedMatrix &matrix, ASAtom csAtom, ASInt32 bpc, ASFixedRect scaledDestRect);
float SetResolution(float resolution);
public:
DrawToMemory(PDPage &pdPage, char *colorSpace, char *filterName, ASInt32 bpc, float resolution, ASFixedRect *pUpdateRect);
~DrawToMemory();
char* GetImageBuffer();
ASInt32 GetImageBufferSize();
PDEImage MakePDEImage();
ASFixedRect GetImageRect();
};
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.21005.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "drawtomemory", "drawtomemory.vcxproj", "{65FB48A9-238C-4B34-ADCB-FF6E549F0B47}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Debug|x64 = Debug|x64
Release|Win32 = Release|Win32
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{65FB48A9-238C-4B34-ADCB-FF6E549F0B47}.Debug|Win32.ActiveCfg = Debug|Win32
{65FB48A9-238C-4B34-ADCB-FF6E549F0B47}.Debug|Win32.Build.0 = Debug|Win32
{65FB48A9-238C-4B34-ADCB-FF6E549F0B47}.Debug|x64.ActiveCfg = Debug|x64
{65FB48A9-238C-4B34-ADCB-FF6E549F0B47}.Debug|x64.Build.0 = Debug|x64
{65FB48A9-238C-4B34-ADCB-FF6E549F0B47}.Release|Win32.ActiveCfg = Release|Win32
{65FB48A9-238C-4B34-ADCB-FF6E549F0B47}.Release|Win32.Build.0 = Release|Win32
{65FB48A9-238C-4B34-ADCB-FF6E549F0B47}.Release|x64.ActiveCfg = Release|x64
{65FB48A9-238C-4B34-ADCB-FF6E549F0B47}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{65FB48A9-238C-4B34-ADCB-FF6E549F0B47}</ProjectGuid>
<RootNamespace>drawtomemory</RootNamespace>
<Keyword>Win32Proj</Keyword>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>v120</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>v120</PlatformToolset>
<CharacterSet>NotSet</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>v120</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>v120</PlatformToolset>
<CharacterSet>NotSet</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\utils\WinSDKConfigurations\Win32ReleaseSettings.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\utils\WinSDKConfigurations\Win32DebugSettings.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\utils\WinSDKConfigurations\Win64ReleaseSettings.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\utils\WinSDKConfigurations\Win64DebugSettings.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<_ProjectFileVersion>12.0.21005.1</_ProjectFileVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<OutDir>$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(Platform)\$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutDir>$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(Platform)\$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<OutDir>$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(Platform)\$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<OutDir>$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(Platform)\$(Configuration)\</IntDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Midl>
<TargetEnvironment>X64</TargetEnvironment>
</Midl>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Midl>
<TargetEnvironment>X64</TargetEnvironment>
</Midl>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="drawtomemory.cpp" />
<ClCompile Include="mainproc.cpp" />
<ClCompile Include="..\utils\MyPDFLibApp.cpp" />
<ClCompile Include="..\utils\MyPDFLibUtils.cpp" />
<ClCompile Include="..\..\Include\Source\PDFLInitCommon.c" />
<ClCompile Include="..\..\Include\Source\PDFLInitHFT.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\utils\MyPDFLibUtils.h" />
<ClInclude Include="drawtomemory.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
/*
DrawToMemory - Sample for the Adobe PDF Library distributed by Datalogics.
Copyright (c) 2007-2015, Datalogics, Inc. All rights reserved.
This sample code is licensed under the terms listed at
http://dev.datalogics.com/adobe-pdf-library/license-for-downloaded-pdf-samples/
This PDF Library sample demonstrates the process of rasterizing a PDF page
and placing the resulting raster as an image into a different PDF document.
This file contains the non-rasterizing portions of this sample:
PDF document opening, creation and saving, page creation, etc.
*/
#include "PDFInit.h"
#include "CosCalls.h"
#include "CorCalls.h"
#include "ASCalls.h"
#include "PDCalls.h"
#include "PSFCalls.h"
#include "PERCalls.h"
#include "PEWCalls.h"
#include "PIExcept.h"
#include "PagePDECntCalls.h"
#include "MyPDFLibUtils.h"
#include "drawtomemory.h"
#ifdef MAC_ENV
#include "macUtils.h"
#endif
#define INPUT_FILE "../../../APDFL/Samples/_Data/drawtomemory.pdf"
#define OUTPUT_FILE "DrawToMemory-out.pdf"
#define RESOLUTION 150.0 //typically 72.0, 150.0, 200.0, 300.0, or 600.0
#define COLORSPACE "DeviceRGB" //typically this, DeviceGray or DeviceCMYK
#define FILTER "FlateDecode" //or ASCIIHexDecode, LZWDecode, DCTDecode
#define BPC 8 //this must be 8 for DeviceRGB & DeviceCYMK, or 1, 8, or 24 for DeviceGray
void MainProc(int argc, char **argv )
{
ASInt32 err = 0;
volatile ASPathName outputPathName = NULL;
volatile PDDoc outputPDDoc = NULL;
PDPage pdPage = NULL;
volatile PDPage outputPDPage = NULL;
volatile PDEContent content = NULL;
ASFixedRect updateRect;
PDDoc pdDoc = MyPDDocOpen(argc>1?argv[1]:INPUT_FILE);
if (pdDoc)
{
DURING
if (argc > 6)
{
int pageNum = atoi(argv[2]);
pdPage = PDDocAcquirePage(pdDoc, pageNum);
updateRect.left = FloatToASFixed(atof(argv[3]));
updateRect.bottom = FloatToASFixed(atof(argv[4]));
updateRect.right = FloatToASFixed(atof(argv[5]));
updateRect.top = FloatToASFixed(atof(argv[6]));
}
else if (argc > 5)
{
pdPage = PDDocAcquirePage(pdDoc, 0);
updateRect.left = FloatToASFixed(atof(argv[2]));
updateRect.bottom = FloatToASFixed(atof(argv[3]));
updateRect.right = FloatToASFixed(atof(argv[4]));
updateRect.top = FloatToASFixed(atof(argv[5]));
}
else
{
pdPage = PDDocAcquirePage(pdDoc, 0);
PDPageGetCropBox(pdPage, &updateRect);
}
// The constructor for this class will do the scaling, position, etc. and rasterize
// the supplied PDPage.
DrawToMemory drawPage(pdPage, COLORSPACE, FILTER, BPC, RESOLUTION, &updateRect);
outputPDDoc = PDDocCreate();
outputPDPage = PDDocCreatePage(outputPDDoc, PDBeforeFirstPage, drawPage.GetImageRect());
content = PDPageAcquirePDEContent(outputPDPage, 0);
// The call to MakePDEImage synthesizes a PDEImage object from the rasterized PDF page
// created in the constructor, suitable for placing onto a PDF page.
PDEContentAddElem(content, 0, (PDEElement) drawPage.MakePDEImage() );
/* DLADD dtom 10Feb2010:
Use PDPageSetPDEContentCanRaise instead of PDPageSetPDEContent.*/
PDPageSetPDEContentCanRaise(outputPDPage, 0);
PDPageReleasePDEContent(outputPDPage, 0);
#if WIN_PLATFORM || UNIX_PLATFORM
outputPathName = ASFileSysCreatePathName(NULL, ASAtomFromString("Cstring"), (char*)OUTPUT_FILE, 0);
#elif MAC_PLATFORM
outputPathName = GetMacPath(OUTPUT_FILE);
#endif
PDDocSave(outputPDDoc, PDSaveFull | PDSaveCollectGarbage, outputPathName, 0, 0, 0);
ASFileSysReleasePath(NULL, outputPathName);
PDPageRelease(outputPDPage);
PDPageRelease(pdPage);
HANDLER
char buf[256];
ASGetErrorString(ERRORCODE, buf, sizeof(buf));
fprintf(stderr, "Error code: 0x%x, Error Message: %s\n", ERRORCODE, buf);
END_HANDLER
PDDocClose(outputPDDoc);
PDDocClose(pdDoc);
}
}
#define INCLUDE_MYPDFLIBAPP_CPP 1
#include "MyPDFLibApp.cpp"
#undef INCLUDE_MYPDFLIBAPP_CPP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment