We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Insert first page

Hello,


I’m trying to insert a new page to an existing pdf file at the index 1, but i’m getting an white empty page with 0 width and 0 height. If i try to insert the page to any different index it works fine.
I’m using the aspose.pdf 8.5
Below is the code i’m using :

 public MemoryStream InsertPages(MemoryStream inputFile, int index, MemoryStream pageStream)
{
MemoryStream retVal = new MemoryStream();
if (inputFile != null)
{
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(inputFile);
  <span style="color:#2b91af;">Page</span> newPage = pdfDocument.Pages.Insert(index);

   <span style="color:#2b91af;">PageCollection</span> pageCollection = pdfDocument.Pages;

   <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)
   {
      
      pdfPageWith = newPage.Rect.Width;
      pdfPageHeight = newPage.Rect.Height;
      lly = newPage.Rect.LLY;
      llx = newPage.Rect.LLX;
      ury = newPage.Rect.URY;
      urx = newPage.Rect.URX;
          
      newPage.SetPageSize(pdfPageWith, pdfPageHeight);

      Aspose.Pdf.Facades.<span style="color:#2b91af;">PdfFileMend</span> fileMend = <span style="color:blue;">new</span> Aspose.Pdf.Facades.<span style="color:#2b91af;">PdfFileMend</span>(pdfDocument);
      <span style="color:blue;">using</span> (pageStream)
      {
       
        <span style="color:blue;">float</span> pdfPageHeightLLY = (<span style="color:blue;">float</span>)lly;
        <span style="color:blue;">float</span> pdfPageWidthLLX = (<span style="color:blue;">float</span>)llx;
        <span style="color:blue;">float</span> pdfPageHeightURY = (<span style="color:blue;">float</span>)ury;
        <span style="color:blue;">float</span> pdfPageWidthURX = (<span style="color:blue;">float</span>)urx;

         <span style="color:blue;">if</span> (pageStream != <span style="color:blue;">null</span>)
         {
             fileMend.AddImage(pageStream, index, pdfPageWidthLLX, pdfPageHeightLLY, pdfPageWidthURX, pdfPageHeightURY);
         }
                
       }
            pdfDocument.Save(retVal);
            fileMend.Close();
        }
        <span style="color:blue;">return</span> retVal;
    }</pre></div><div>Am I missing something ?</div><div><br></div><div>Regards, </div><div><br></div>
Dears,

Please find attached a sample of the pdf document i'm using

Regards,

Hi Hiba,


Thanks for contacting support.

I have tested the scenario using Aspose.Pdf for .NET 9.0.0 and as per my observations, the page being inserted is very small size. It becomes visible when setting zoom factor as 6400% in Adobe Reader. Whereas the existing page in PDF is visible at 133%. Even though, when using following simple code lines, the above status appears.

[C#]

Aspose.Pdf.Document
pdfDocument = new Aspose.Pdf.Document(“c:/pdftest/FirstPage_Test.pdf”);<o:p></o:p>

Page newPage = pdfDocument.Pages.Insert(1);

pdfDocument.Save(“c:/pdftest/PageInserted_Index1.pdf”);



For the sake of correction, I have logged this problem
as PDFNEWNET-36695 in our issue tracking system. We will further
look into the details of this problem and will keep you updated on the status
of correction. Please be patient and spare us little time. We are sorry for
this inconvenience.

Hello,


Please notice that if we’re trying to insert an image to the page and not only an empty page, the image is always white.
Can i have an approximate time for this fix? so i can set an estimation for the project ?
I’m waiting your update, thank you

Regards,

Hi Hiba,


Thanks for sharing the details.

I have tested the scenario while using following code snippet where I have first inserted a page at first position and then have tried adding an image to newly inserted page and I have also manged to notice the same problem that first page is appearing as blank. For the sake of correction, I have separately logged this issue as PDFNEWNET-36709 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for your inconvenience.

[C#]

//open document<o:p></o:p>

Document pdfDocument = new Document("c:/pdftest/Test_ImagetoPage.pdf");

//set coordinates

int lowerLeftX = 100;

int lowerLeftY = 100;

int upperRightX = 200;

int upperRightY = 200;

pdfDocument.Pages.Insert(1);

//get the page where image needs to be added

Page page = pdfDocument.Pages[1];

//load image into stream

FileStream imageStream = new FileStream("c:/pdftest/PB90191655x295_5957.pdf_image1.png", FileMode.Open);

//add image to Images collection of Page Resources

page.Resources.Images.Add(imageStream);

//using GSave operator: this operator saves current graphics state

page.Contents.Add(new Operator.GSave());

//create Rectangle and Matrix objects

Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);

Aspose.Pdf.DOM.Matrix matrix = new Aspose.Pdf.DOM.Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });

//using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed

page.Contents.Add(new Operator.ConcatenateMatrix(matrix));

XImage ximage = page.Resources.Images[page.Resources.Images.Count];

//using Do operator: this operator draws image

page.Contents.Add(new Operator.Do(ximage.Name));

//using GRestore operator: this operator restores graphics state

page.Contents.Add(new Operator.GRestore());

//save updated document

pdfDocument.Save(“c:/pdftest/ImageAdded_output.pdf”);

Hi Hiba,


Thanks for your patience. We have further investigated the PDFNEWNET-36709 and suggest to use following code snippet to add image to PDF file. Hopefully it will help you to accomplish the task.

Moreover, we will keep you updated about the other reported issue, PDFNEWNET-36695, resolution progress via this forum thread.

using (Document pdfDocument = new
Document(myDir+“Test.pdf”))<o:p></o:p>

{

//set coordinates

int lowerLeftX = 100;

int lowerLeftY = 100;

int upperRightX = 200;

int upperRightY = 200;

Page page = pdfDocument.Pages.Insert(1);

// Explicitly Set the box size of the new page the same as existing second page

page.MediaBox = pdfDocument.Pages[2].MediaBox;

//load image into stream

// Change AnyPngImage to yours

using (FileStream imageStream = new FileStream(myDir + "aspose.pdf.png", FileMode.Open))

{

//add image to Images collection of Page Resources

page.Resources.Images.Add(imageStream);

//using GSave operator: this operator saves current graphics state

page.Contents.Add(new Operator.GSave());

//create Rectangle and Matrix objects

Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX,

upperRightY);

Aspose.Pdf.DOM.Matrix matrix =

new Aspose.Pdf.DOM.Matrix(new double[]

{

rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX,

rectangle.LLY

});

//using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed

page.Contents.Add(new Operator.ConcatenateMatrix(matrix));

XImage ximage = page.Resources.Images[page.Resources.Images.Count];

//using Do operator: this operator draws image

page.Contents.Add(new Operator.Do(ximage.Name));

//using GRestore operator: this operator restores graphics state

page.Contents.Add(new Operator.GRestore());

//save updated document

pdfDocument.Save(myDir+"ImageAdded_output.pdf");

}

}

Please feel free to contact us for any further assistance.


Best Regards,

The issues you have found earlier (filed as PDFNEWNET-36709) have been fixed in Aspose.Pdf for .NET 9.2.0.

The blog post for this release is created over this link


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.
(2)

The issues you have found earlier (filed as PDFNEWNET-36695) have been fixed in Aspose.Pdf for .NET 9.3.0.

Blog post for this release can be viewed over this link


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.