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).
Enable AllowObjectEditing to allow your users to edit objects in a PDF page.



Note:
You must call ApplyChanges before saving to apply object changes to the document
If you edit objects, PageModified will become true


Demo

Demo  Demos\PDF\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;
  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.SaveToFile( '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.SaveToFile( 'D:\PDFTest\Object_' + i.ToString + '.png' );
    bmp.Free;
  end;


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


// Replace all the images in the page
bmp := TIEBitmap.Create();
try
  bmp.LoadFromFile( '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 selected object from the PDF page
if ImageEnView1.PdfViewer.SelectedObjects >= 0 then
  ImageEnView1.PdfViewer.Objects.RemoveObject( ImageEnView1.PdfViewer.SelectedObjects );


// 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
      ImageEnView1.PdfViewer.Objects.RemoveObject( idx );
end;

// Add a spot wherever user clicks on a PDF page
procedure TfrmMain.ImageEnView1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
const
  Spot_Size         = 20;
  Spot_Border_Color = clBlack;
  Spot_Border_Size  = 2;
  Spot_Fill_Color   = clRed;
  Spot_Opacity      = 255;
var
  clickPos: TDPoint;
  obj: TPdfObject;
begin
  clickPos := ImageEnView1.PdfViewer.ScrToPage( X, Y, True );

  // Center ellipse over click position
  obj := ImageEnView1.PdfViewer.Objects.AddEllipse( clickPos.X - Spot_Size div 2, clickPos.Y - Spot_Size div 2, Spot_Size, Spot_Size );

  obj.StrokeColor := TColor2TRGBA( Spot_Border_Color, Spot_Opacity );
  obj.PathStrokeWidth := Spot_Border_Size;
  obj.FillColor := TColor2TRGBA( Spot_Fill_Color, Spot_Opacity );
  obj.PathFillMode := pfAlternate; // Ensure path is filled
end;


See Also

FormFields