Add New Image to a new page with specific width and height

Hi,

Is there a way to fit an new image into a pdf by setting width or height when using PdfFileMend instance ?


I have the below code snippet :

if (!string.IsNullOrEmpty(filePath))
 {
    Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(filePath);
    Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
    PageCollection pageCollection = pdfDocument.Pages;
    Page newPage = pdfDocument.Pages.Insert(index);
<span style="color:blue;">double</span> pdfPageWith, pdfPageHeight;
<span style="color:blue;">double</span> lly, llx, urx, ury;
<span style="color:blue;">if</span> (pageCollection.Count != 0)
{
   <span style="color:#2b91af;">Page</span> pdfPage = pageCollection[1];
   pdfPageWith = (<span style="color:blue;">double</span>)pdfPage.Rect.Width;
   pdfPageHeight = (<span style="color:blue;">double</span>)pdfPage.Rect.Height;
   lly = pdfPage.Rect.LLY;
   llx = pdfPage.Rect.LLX;
   ury = pdfPage.Rect.URY;
   urx = pdfPage.Rect.URX;
}
<span style="color:blue;">else</span>
{
   pdfPageWith = pdf.PageSetup.PageWidth; <span style="color:green;">// Aspose.Pdf.Generator.PageSize.A4Width; </span>
   pdfPageHeight = pdf.PageSetup.PageHeight;<span style="color:green;">// Aspose.Pdf.Generator.PageSize.A4Height;</span>
   lly = newPage.Rect.LLY;
   llx = newPage.Rect.LLX;
   ury = newPage.Rect.URY;
   urx = newPage.Rect.URX;

}
newPage.SetPageSize(pdfPageWith, pdfPageHeight);
Aspose.Pdf.Facades.PdfFileMend fileMend = new Aspose.Pdf.Facades.PdfFileMend(pdfDocument, retVal);
using (pageStream)
{
MemoryStream stream = pageStream;
float pdfPageHeightLLY = (float)lly;
float pdfPageWidthLLX = (float)llx;
float pdfPageHeightURY = (float)ury;
float pdfPageWidthURX = (float)urx;
if (pageStream != null)
{
fileMend.AddImage(pageStream, index, pdfPageWidthLLX, pdfPageHeightLLY, pdfPageWidthURX, pdfPageHeightURY);
}

   }
   pdfDocument.Save(filePath);
  fileMend.Close();

}


Any idea how to do this?

Thank you in advanced 

Hi there,


Thanks for your inquiry. Please check following code snippet, hopefully it will serve the purpose.

PdfFileInfo fileInfo = new PdfFileInfo(input);
float height = fileInfo.GetPageHeight(1);
float width = fileInfo.GetPageWidth(1);

using (FileStream inImgStream = new FileStream(imagepath, FileMode.Open))
{
PdfFileMend mendor = new PdfFileMend(input, output);
mendor.AddImage(inImgStream, 1, 0, 0, width, height);
mendor.Close();
inImgStream.Close();
}

Please correct me if there is any difference in my understanding and your requirement.

Best Regards,

hi,


GetPageHeight and GetPageWidth gives the same value as the one i used for the passed parameters.
The thing is that i’m rotating the page after adding it.
Without rotation it works perfectly, but with rotation the saved page is wrong.
First it does not display all on the page and second the angle is wrong.
So if i’m rotating the page for 90 degree it looks like it was rotated for 180 degree.

for rotation i’m using the following code snippet:
 Aspose.Pdf.Facades.PdfViewer viewer = new Aspose.Pdf.Facades.PdfViewer();
viewer.AutoRotateMode = Aspose.Pdf.Facades.AutoRotateMode.AntiClockWise;
Aspose.Pdf.Facades.PdfPageEditor pEdit = new Aspose.Pdf.Facades.PdfPageEditor();
pEdit.BindPdf(filePath);
using (FileStream fileStream = File.OpenWrite(filePath))
{
retVal.SetLength(fileStream.Length);
}
                <span style="color:#2b91af;">List</span><<span style="color:blue;">int</span>> pageIndexes = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span><<span style="color:blue;">int</span>>();
                <span style="color:blue;">string</span> tempFolder = <span style="color:#2b91af;">Path</span>.GetDirectoryName(filePath);
                <span style="color:blue;">foreach</span> (<span style="color:blue;">int</span> degree <span style="color:blue;">in</span> pages.Keys)
                {
                    pageIndexes = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span><<span style="color:blue;">int</span>>();
                    <span style="color:blue;">if</span> (pages[degree].Count > 0)
                    {
                        <span style="color:blue;">foreach</span> (<span style="color:#2b91af;">FilePage</span> page <span style="color:blue;">in</span> pages[degree])
                        {
                            pageIndexes.Add(page.PageIndex);
                        }

                        <span style="color:blue;">if</span> (pageIndexes.Count > 0)
                        {
                            pEdit.Pages = pageIndexes.ToArray();
                            pEdit.Rotation = degree;
                            pEdit.Save(retVal);
                        }
                    }
                }
             </pre></div><div>Please help and thanks in advanced </div><div><br></div><div><div style="font-family: Arial, Helvetica; font-size: 13px; background-color: rgb(221, 221, 221);"><br></div></div>

Hi there,


Sorry for the delayed response. I’m afraid, I’m unable to test your sample due to missing references. Can you please share your input/output documents and sample code here? So we test the scenario and provide your more information accordingly.

Sorry for the inconvenience faced.

Best Regards,

Hi,


Please find attached my input/output document. the InitialImage is inserted, but not saved yet to the pdf, the inputImage is Rotated but not saved to the document i’m saving the degree of the rotation then sending it as parameter. when saving the document i got the third image.
To get there i’m doing this in two step:
1- Insert new page to the pdf by adding the inserted Image (that come rotated)
2- Rotate the page

to Insert the page i’m using :
public MemoryStream InsertPages(string filePath, int index, MemoryStream pageStream)
{
MemoryStream retVal = new MemoryStream();
if (!string.IsNullOrEmpty(filePath))
{
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(filePath);
Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
PageCollection pageCollection = pdfDocument.Pages;
Page newPage = pdfDocument.Pages.Insert(index);

double pdfPageWith, pdfPageHeight;
double lly, llx, urx, ury;
if (pageCollection.Count != 0)
{
Page pdfPage = pageCollection[1];

pdfPageWith = (double)pdfPage.Rect.Width;
pdfPageHeight = (double)pdfPage.Rect.Height;
lly = pdfPage.Rect.LLY;
llx = pdfPage.Rect.LLX;
ury = pdfPage.Rect.URY;
urx = pdfPage.Rect.URX;
}
else
{
pdfPageWith = pdf.PageSetup.PageWidth;
pdfPageHeight = pdf.PageSetup.PageHeight;
lly = newPage.Rect.LLY;
llx = newPage.Rect.LLX;
ury = newPage.Rect.URY;
urx = newPage.Rect.URX;
}

Aspose.Pdf.Facades.PdfFileMend fileMend = new Aspose.Pdf.Facades.PdfFileMend(pdfDocument, retVal);

using (pageStream)
{
MemoryStream stream = pageStream;
float pdfPageHeightLLY = (float)lly;
float pdfPageWidthLLX = (float)llx;
float pdfPageHeightURY = (float)ury;
float pdfPageWidthURX = (float)urx;

if (pageStream != null)
{
fileMend.AddImage(pageStream, index, pdfPageWidthLLX, pdfPageHeightLLY, pdfPageWidthURX, pdfPageHeightURY);
}
stream = null;
}
pdfDocument.Save(filePath);
fileMend.Close();

}

return retVal;                                      
}

the saved file in this function is used to the rotate function as follow:
MemoryStream retVal = new MemoryStream();
try
{
if (!string.IsNullOrEmpty(filePath))
{
Aspose.Pdf.Facades.PdfViewer viewer = new Aspose.Pdf.Facades.PdfViewer();
viewer.AutoRotateMode = Aspose.Pdf.Facades.AutoRotateMode.AntiClockWise;
Aspose.Pdf.Facades.PdfPageEditor pEdit = new Aspose.Pdf.Facades.PdfPageEditor();
pEdit.BindPdf(filePath);
using (FileStream fileStream = File.OpenWrite(filePath))
{
retVal.SetLength(fileStream.Length);
}

List<int> pageIndexes = new List<int>();

   pageIndexes.Add(1);
   pEdit.Pages = pageIndexes.ToArray();
pEdit.Rotation = 90;
pEdit.Save(retVal);

}
pEdit = null;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return retVal;


Is there a away to reset the rotation of the page initially inserted?
and save the document in the correct form ?? 

hello,


Any news ?? Is there anything I can do for this??

Thank you

Hi there


Sorry for the delayed response. As suggested here, to fulfill your requirements you need to either rotate image to desired angle before adding into PDF document or later need to rotate PDF page to desired angle. In the case of shared image you need to rotate image or page to 270 to get upright image.

Please feel free to contact us for any further assistance.

Best Regards,