ImageEn, unit iexUserInteractions

TIEPdfViewer.Objects

TIEPdfViewer.Objects


Declaration

property Objects: TPdfObjectList;


Description

Returns a list of all objects on the current page (images, text, lines, etc).




Demo

Demo  Demos\Other\PDFPageObjects\PDFPageObjects.dpr


Examples

// Output details of all page objects to a memo
var
  i, p, ptCount: Integer;
  s: string;
  r: TRect;
  bmp: TIEBitmap;
  objType: TPdfObjectType;
  fontName: String;
  fontSize, strokeWidth: Single;
  fillMode: TPdfFillMode;
  fontWeight, italicAngle: Integer;
  isStroked: Boolean;
  strokeColor, fillColor, FontColor: TRGBA;
  pointArray: TIEDPointArray;
  pts: string;
begin
  Memo1.Lines.BeginUpdate();
  Memo1.Lines.Clear();
  Memo1.Lines.Add( format( 'Page %d: %d x %d', [ ImageEnView1.PdfViewer.PageIndex, Round( ImageEnView1.PdfViewer.CurrentPage.Width ), Round( ImageEnView1.PdfViewer.CurrentPage.Height )] ));
  for i := 0 to ImageEnView1.PdfViewer.Objects.Count - 1 do
  begin
    Memo1.Lines.Add( 'OBJECT ' + i.ToString );

    // Type
    s := '';
    objType := ImageEnView1.PdfViewer.Objects[i].ObjectType;
    case objType of
      ptUnknown  : s := 'Unknown';
      ptText     : s := 'Text';
      ptPath     : s := 'Path';
      ptImage    : s := 'Image';
      ptShading  : s := 'Shading';
      ptForm     : s := 'Form';
    end;
    Memo1.Lines.Add( '  Type: ' + s );

    // Bounds
    r := ImageEnView1.PdfViewer.Objects[i].Bounds;
    Memo1.Lines.Add( Format( '  Bounds: %d,%d,%d,%d', [ r.Left, r.Top, r.Right, r.Bottom ]));

    // Text properties
    if objType = ptText then
    begin
      Memo1.Lines.Add( '  Text: ' + ImageEnView1.PdfViewer.Objects[i].Text );

      ImageEnView1.PdfViewer.Objects[i].GetTextFont( fontName, fontSize, fontColor, fontWeight, italicAngle );
      Memo1.Lines.Add( '  Font Name: ' + fontName );
      Memo1.Lines.Add( '  Font Size: ' + fontSize.ToString );
      Memo1.Lines.Add( Format( '  Font Color: %d,%d,%d/%d', [ fontColor.r, fontColor.g, fontColor.b, fontColor.a ]));
      Memo1.Lines.Add( '  Font Weight: ' + fontWeight.ToString );
      Memo1.Lines.Add( '  Italic Angle: ' + italicAngle.ToString );
    end
    else
    // Path properties
    if objType = ptPath then
    begin
      strokeColor := ImageEnView1.PdfViewer.Objects[i].StrokeColor;
      strokeWidth := ImageEnView1.PdfViewer.Objects[i].PathStrokeWidth;
      fillColor   := ImageEnView1.PdfViewer.Objects[i].FillColor;
      fillMode    := ImageEnView1.PdfViewer.Objects[i].FillMode;

      Memo1.Lines.Add( Format( '  Stroke Color: %d,%d,%d/%d', [ strokeColor.r, strokeColor.g, strokeColor.b, strokeColor.a ]));
      Memo1.Lines.Add( '  Stroke Width: ' + strokeWidth.ToString );
      Memo1.Lines.Add( Format( '  Fill Color: %d,%d,%d/%d', [ fillColor.r, fillColor.g, fillColor.b, fillColor.a ]));
      Memo1.Lines.Add( Format( '  Fill Mode: %d', [ ord( fillMode )]));

      ImageEnView1.PdfViewer.Objects[i].GetPath( pointArray, ptCount );
      Memo1.Lines.Add( '  Point Count: ' + ptCount.ToString );

      pts := '';
      for p := 0 to Length( pointArray ) - 1 do
      begin
        if pointArray[p].x = IE_PointArray_ClosingBreak then
          pts := pts + format( ',CLOSE', [ Round( pointArray[p].X ), Round( pointArray[p]. Y )])
        else
        if pointArray[p].Y = IE_PointArray_Break then
          pts := pts + format( ',BREAK', [ Round( pointArray[p].X ), Round( pointArray[p]. Y )])
        else
          pts := pts + format( ',(%d,%d)', [ Round( pointArray[p].X ), Round( pointArray[p]. Y )]);
      end;
      if pts <> '' then
        Delete(pts, 1, 1 );
      Memo1.Lines.Add( '  Points: ' + pts );
    end
    else
    // Image properties
    if objType = ptImage then
    begin
      bmp := TIEBitmap.Create();
      if ImageEnView1.PdfViewer.Objects[i].GetImage( bmp ) then
      begin
        Memo1.Lines.Add( format( '  Image Size: %d x %d', [ bmp.Width, bmp.height ]));
        // Save?  bmp.Write( 'D:\_PDF\Image_'+i.ToString+ '.png');
        // Show in MView?  ImageEnMView1.AppendImage( bmp );
      end;
      bmp.Free();
    end;
  end;
  Memo1.Lines.EndUpdate();
end;


// Save all images in the page to file
for i := 0 to ImageEnView1.PdfViewer.Objects.Count - 1 do
  if ImageEnView1.PdfViewer.Objects[i].ObjectType = ptImage then
  begin
    bmp := TIEBitmap.Create();
    if ImageEnView1.PdfViewer.Objects[i].GetImage( bmp ) then
      bmp.Write( 'D:\PDFTest\Object_' + i.ToString + '.png' );
    bmp.Free;
  end;


// Set the text of all text objects in page
for i := 0 to ImageEnView1.PdfViewer.Objects.Count - 1 do
  if objType = ptText then
    ImageEnView1.PdfViewer.Objects[i].Text := 'Text Object ' + i.ToString;
ImageEnView1.Invalidate();


// Replace all the images in the page
bmp := TIEBitmap.Create();
try
  bmp.Read( 'D:\MyLogo.jpg' );
  for i := 0 to ImageEnView1.PdfViewer.Objects.Count - 1 do
    if ImageEnView1.PdfViewer.Objects[i].ObjectType = ptImage then
    begin
      if ImageEnView1.PdfViewer.Objects[i].SetImage( bmp ) = False then
        BREAK;
    end;
finally
  bmp.Free;
  ImageEnView1.PdfViewer.ReloadPage();
end;


// Remove the highlighted object from the PDF page
ImageEnView1.PdfViewer.Objects.RemoveObject( ImageEnView1.PdfViewer.Objects.HighlightedIndex );
ImageEnView1.Invalidate();


// Delete the item clicked on
procedure TfrmMain.ImageEnView1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  idx: Integer;
  pt: TDPoint;
begin
  pt := ImageEnView1.PdfViewer.ScrToPage( X, Y, True );
  idx := ImageEnView1.PdfViewer.Objects.FindObjectAt( pt );
  if idx > -1 then
    if MessageDlg( format( 'Delete object %d?', [ idx + 1 ]), mtConfirmation, [ mbYes,mbNo ], 0 ) = mrYes then
    begin
      ImageEnView1.PdfViewer.Objects.RemoveObject( idx );
      ImageEnView1.Invalidate();
    end;
end;


See Also

- FormFields