Hi, for example I have a 4-page PDF file where each page has a different page size. I want to permanently normalize all pages so that every page width becomes 1024, and all page contents are scaled accordingly (Fit to 1024 width). The goal is that in any PDF viewer, every page appears with the same width.
First, I calculate the scaling factor:
Scale := 1024 / PdfIn.PageWidth;
Then I create the new page size:
IEGlobalSettings().PdfViewerDefaults.DPI := 300;
PdfOut.AddPage(1024 / 300 * 72, PdfIn.PageHeight * Scale / 300 * 72);
The steps above seem to work correctly.
Next, I try to migrate image objects by iterating through TPdfObject:
if obj.ObjectType = ptImage then
begin
bmp := TIEBitmap.Create();
if not PdfIn.Objects[j].GetImageRaw(bmp) then
PdfIn.Objects[j].GetImage(bmp);
PdfOut.Objects.AddImage(
obj.X * Scale,
obj.Y * Scale,
obj.Width * Scale,
obj.Height * Scale,
bmp
);
end;
However, I encountered several issues:
1.I understand that PDF coordinate origin is at the bottom-left, so I am ignoring Y-axis inversion issues for now.
2.No matter how I adjust the parameters passed to AddImage, the images are not placed correctly. For example, two images that are originally left-aligned end up with noticeable X-axis offsets in the output PDF. Also, two images that originally have the same size (e.g. two portrait images) may end up with different sizes after conversion.
3.Some images appear to lose transparency or indexed color effects after being transferred, resulting in color differences in the output PDF.
After that, I tried the following approach:
PdfIn.SelectAll;
PdfIn.CopyToClipboard;
PdfOut.AllowObjectEditing := True;
PdfOut.PasteFromClipboard;
PdfOut.ApplyChanges;
However, this results in a blank page with no objects being pasted.
I am aware that rendering each page as a single image and then scaling it back into a PDF is an option, but that would lose object-level structure (text would no longer be selectable or copyable), so I would like to avoid that as a last resort.
Is there a better way to properly scale and migrate all PDF objects while preserving their structure and appearance?
Thanks for any advice.