@@ -6325,16 +6325,36 @@ - (void)setStampImageData:(NSString *)annotationId pageNumber:(NSInteger)pageNum
63256325
63266326- (void )setCustomImage : (PTImage*)image OnAnnotation : (PTAnnot*)annot onDoc : (PTPDFDoc*)doc
63276327{
6328- // Initialize a new PTElementWriter and PTElementBuilder
6328+ // Save the original bounding box
6329+ PTPDFRect* original_bbox = [annot GetRect ];
6330+ double orig_x1 = [original_bbox GetX1 ];
6331+ double orig_y1 = [original_bbox GetY1 ];
6332+ double orig_x2 = [original_bbox GetX2 ];
6333+ double orig_y2 = [original_bbox GetY2 ];
6334+
6335+ double original_width = orig_x2 - orig_x1;
6336+ double original_height = orig_y2 - orig_y1;
6337+
6338+ // Get image dimensions
6339+ double image_width = [image GetImageWidth ];
6340+ double image_height = [image GetImageHeight ];
6341+
6342+ // Calculate aspect-fit scaling ratio to retain size
6343+ double aspectFitRatio = fmin (original_width / image_width, original_height / image_height);
6344+
6345+ // Compute new image dimensions
6346+ double new_width = image_width * aspectFitRatio;
6347+ double new_height = image_height * aspectFitRatio;
6348+
6349+ // Initialize a new PTElementWriter, PTElementBuilder and new markup object
63296350 PTElementWriter* writer = [[PTElementWriter alloc ] init ];
63306351 PTElementBuilder* builder = [[PTElementBuilder alloc ] init ];
6352+ PTMarkup *markupAnnot = [[PTMarkup alloc ] initWithAnn: annot];
63316353
63326354 [writer WriterBeginWithSDFDoc: [doc GetSDFDoc ] compress: YES ];
6333-
6334- int w = [image GetImageWidth ], h = [image GetImageHeight ];
6335-
6355+
63366356 // Initialize a new image element
6337- PTElement* img_element = [builder CreateImageWithCornerAndScale: image x: 0 y: 0 hscale: w vscale: h ];
6357+ PTElement* img_element = [builder CreateImageWithCornerAndScale: image x: 0 y: 0 hscale: image_width vscale: image_height ];
63386358
63396359 // Write the element
63406360 [writer WritePlacedElement: img_element];
@@ -6344,12 +6364,28 @@ - (void)setCustomImage:(PTImage*)image OnAnnotation:(PTAnnot*)annot onDoc:(PTPDF
63446364
63456365 // Configure the appearance stream that will be written to the annotation
63466366 PTObj* appearance_stream = [writer End ];
6347-
6367+
63486368 // Set the bounding box to be the rect of the new element
63496369 [appearance_stream PutRect: @" BBox" x1: [bbox GetX1 ] y1: [bbox GetY1 ] x2: [bbox GetX2 ] y2: [bbox GetY2 ]];
6350-
6370+
63516371 // Overwrite the annotation's appearance with the new appearance stream
63526372 [annot SetAppearance: appearance_stream annot_state: e_ptnormal app_state: 0 ];
6373+
6374+ // Compute the final annotation rect from the appearance bounding box
6375+ PTPDFRect* new_annot_rect = [[PTPDFRect alloc ] initWithX1: orig_x1
6376+ y1: orig_y1
6377+ x2: (orig_x1 + new_width)
6378+ y2: (orig_y1 + new_height)];
6379+
6380+ // Apply the computed annotation rect
6381+ [annot SetRect: new_annot_rect];
6382+
6383+ // Rotate the new appearance
6384+ [markupAnnot RotateAppearance: [[[annot GetSDFObj ] FindObj: @" Rotate" ] GetNumber ]];
6385+
6386+ // Apply original bbox to maintain size
6387+ [annot SetRect: original_bbox];
6388+
63536389}
63546390
63556391- (void )setForceAppTheme : (NSString *)forcedAppTheme
0 commit comments