Here it is. Just copy the procedure below in a Delphi Unit. I've also attached the PDF file that I start with.
Code: Select all
procedure VectorTest;
var
hr: HRESULT;
hDoc, hImg, hPage: Cardinal;
B: TBitmap;
ADir, APDFFile, ABmpFile, ATIFFile, ANewPDFFile, ARegKey, ADevCode: String;
AWidth, AHeight: Double;
H, W: Integer;
AWholePage: TRect;
AParams: PXV_CommonRenderParameters;
bUseVector: Boolean;
begin
//VECTOR RENDERING NOTE:
//When bUseVector is True, ANewPDFFile results in a Blank PDF
//When bUseVector is False, works fine.
bUseVector := False;
ARegKey := '<YOUR REG CODE>';
ADevCode := '<YOUR DEV CODE>';
ADir := 'F:\PROJECTS\Tracker Software\Distrib\';
APDFFile := 'OCR.pdf';
ABmpFile := 'BmpFile.bmp';
ATIFFile := 'TIFFile.tif';
ANewPDFFile := 'OCR_Result.pdf';
B := TBitmap.Create;
//STEP 1: OPEN PDF IN VIEWER
hr := 0;
hr := PXCV_Init(@hDoc, PChar(ARegKey), PChar(ADevCode));
hr := PXCV_ReadDocumentW(hDoc, PWChar(WideString(ADir + APDFFile)), 0);
//STEP 2: DRAW PAGE TO DC
PXCV_GetPageDimensions(hDoc, 0, @AWidth, @AHeight); //Sizes are in Points
W := Round((AWidth / 72) * 100 * 3); //Convert to Pixels
H := Round((AHeight / 72) * 100 * 3); //Convert to Pixels
with AWholePage do
begin
Left := 0;
Top := 0;
Right := Left + W;
Bottom := Top + H;
end;
B.Width := W;
B.Height := H;
AParams.WholePageRect := @AWholePage;
AParams.DrawRect := nil;
AParams.RenderTarget := pxvrm_Exporting;
if bUseVector then AParams.Flags := pxvrpf_UseVectorRenderer; //ISSUES HERE???
hr := PXCV_DrawPageToDC(hDoc, 0, B.Canvas.Handle, @AParams);
hr := PXCV_Delete(hDoc);
hDoc := 0;
//STEP 3: SAVE BMP TO FILE
B.SaveToFile(ADir + ABmpFile);
//STEP 4: CONVERT BMP To TIFF
hImg := 0;
IMG_Init(PChar(ARegKey), PChar(ADevCode));
IMG_ImageCreateEmpty(hImg);
IMG_PageCreateFromHBITMAP(B.Handle, 0, @hPage);
hr := IMG_PageSetFormatLongParameter(hPage, IXC_FP_ID_FORMAT, IXC_FMT_TIFF_ID);
hr := IMG_PageSetFormatLongParameter(hPage, IXC_FP_ID_ITYPE, IXC_ImageFormat_RGB_8);
hr := IMG_ImageInsertPage(hImg, 0, hPage);
hr := IMG_ImageSaveToFileA(hImg, PAnsiChar(ADir + ATIFFile), IXC_CreationDisposition_Overwrite);
//STEP 5: INSERT BMP To NEW PDF FILE
hr := PXC_NewDocument(@hDoc, PChar(ARegKey), PChar(ADevCode));
AWidth := 8.5 * 72;
AHeight := 11 * 72;
PXC_AddPage(hDoc, AWidth, AHeight, @hPage);
PXC_SetCompression(hDoc, True, False, ComprType_C_Deflate, 75, ComprType_I_Deflate, ComprType_M_Deflate);
PXC_AddImageFromHBITMAP(hDoc, B.Handle, 0, @hImg);
PXC_PlaceImage(hPage, hImg, 0, AHeight, AWidth, AHeight); //Note: seems to fail with a Vector BMP
hr := PXC_WriteDocumentA(hDoc, PAnsiChar(ADir + ANewPDFFile));
B.Free;
end;