The pdf page insert into the new page changed its orientation from portrait to landscape

  Hi, 
  I am try to import a PDF pages into my newly created pdf file, so I created a new page in the new Pdf doc, then use "CreateTemplate" method to retrieve the old pdf page content into a template then draw on the new pdf doc page. It is working, but seems the page orientation changed from portrait to landscape which is very annoying. So I am wondering if anyway I can keep the original portrait orientation instead rotate automatically? 

The code snippet is as below.  And I also attached the original pdf doc, and you can see the original doc orientation is portrait.

   Dim loadedDocument As New PdfLoadedDocument(filepath)   
   Dim startIndex As Integer = 0
   Dim endIndex As Integer = loadedDocument.Pages.Count - 1
   ypos = 33 + 12 * catlines + 20
   For i As Integer = 0 To loadedDocument.Pages.Count - 1
            Dim r_page As PdfPage = pdfdoc.Pages.Add()
            Dim g As PdfGraphics = r_page.Graphics
            Dim attachmentpage As PdfLoadedPage = loadedDocument.Pages(i)
            Dim template As PdfTemplate = attachmentpage.CreateTemplate()
            g.DrawPdfTemplate(template, New PointF(40, ypos), New SizeF(510, 570)) 
   Next
   pdfdoc.Save(pdffullpath)
   pdfdoc.Close()



Attachment: DOC_backup_20c172ef.zip

4 Replies

KC Karthikeyan Chandrasekar Syncfusion Team October 18, 2018 08:41 AM UTC

Hi Tim, 
Thank you for your interest in Syncfusion products. 
We have checked the given input PDF document, It has a landscape orientation and the page is rotate as 90 degree. So the page looks like the portrait orientation. We have a feature to importing pages from an existing document to new document. You can find the UG documentation link for importing the pages in below link. 

UG link for importing pages: 
 
Importing page using PdfTemplate: 
Importing the pages to new page using PdfTemplate, we have to handle the all rotation and position of the template in sample level. Please find the sample code snippet that we have handled the orientation in sample level before drawing PdfTeamplate in PdfPage. 

Code snippet: 
 
Importing page using PdfTemplate: 
 
//Create new PDF document. 
PdfDocument document = new PdfDocument(); 
 
//Set marging all 0. 
document.PageSettings.Margins.All = 0; 
 
//Loads the input PDF document. 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("DOC_backup.pdf"); 
 
for (int i = 0; i <= loadedDocument.Pages.Count - 1; i++) 
{ 
PdfPage page = document.Pages.Add(); 
PdfGraphics g = page.Graphics; 
 
//Create PdfTeamplage of existing page. 
PdfTemplate template = loadedDocument.Pages[i].CreateTemplate(); 
 
//Get the rotation of the page. 
PdfPageRotateAngle angle = loadedDocument.Pages[i].Rotation; 
g.Save(); 
 
//Rotate the page graphics 
g.RotateTransform(GetRotationAngle(angle)); 
g.TranslateTransform(0, -page.GetClientSize().Width); 
 
//Draw PdfTemplate in PdfPage. 
g.DrawPdfTemplate(template, new PointF(0, 0), new SizeF(page.GetClientSize().Height, page.GetClientSize().Width)); 
 
g.Restore(); 
} 
//Save the document. 
document.Save("output.pdf"); 
document.Close(true); 
 
GetRotationAngle: 
private float GetRotationAngle(PdfPageRotateAngle angle) 
{ 
float m_angle = 0; 
switch (angle) 
{ 
case PdfPageRotateAngle.RotateAngle90: 
m_angle = 90; 
break; 
case PdfPageRotateAngle.RotateAngle180: 
m_angle = 180; 
break; 
case PdfPageRotateAngle.RotateAngle270: 
m_angle = 270; 
break; 
} 
return m_angle; 
} 
 
 
Please try the above code snippet in your side and let us know your result. we mostly suggest you to use the ImportPage method to import pages from an existing PDF document. 
Regards, 
Karthikeyan 



TQ Tony Qu October 18, 2018 05:47 PM UTC

I am really impressed with the quick effective response and appreciate your help and code. And it works partially, but I believe this is the right method to solve this issue we met.

The reason I cannot use  the ImportPage method is because we want the old pdf page to be inserted into a specific range of the new page. That's why I used "g.DrawPdfTemplate(template, New PointF(40, 60), New SizeF(510, 570))". 

And your code did rotate the orientation, but it seems not rotate to the right place. I tried adjust the parameter in "g.TranslateTransform(0, -page.GetClientSize().Width); ", but never hit a right place.  So if I want to draw the old pdf page into "New PointF(40, 60), New SizeF(510, 570)"  this range, what the parameters should be for "g.TranslateTransform"?

Can you please help and shed me another light?

Big thanks again!



TQ Tony Qu October 18, 2018 09:14 PM UTC

To let the old pdfpage be draw into the range "New PointF(40, 60), New SizeF(receiptwidth, receiptheight)", I tried  the following code for old page which rotated 90 degree and it seems working fine.

g.DrawPdfTemplate(template, New PointF(60, 40), New SizeF(receiptheight,receiptwidth)) 

However, when I use another pdf doc for testing, its rotation=270, then it doesn't work at all, the whole section missed and displays blank.  Can you please advise where the trick is?

Actually we are not care the rotation of the old document, we just want to insert the old pdf doc as it looks like (rotation =90, but looks like a portrait page), so I am wondering anyway we can change the original page rotation value to zero, then when it creattemplate, it will create as the page looks like. I tried " pdfdoc.Pages(i).Rotation=PdfPageRotateAngle.RotateAngle0", but it seems the Rotation property is readonly.  

The other thought is that maybe just export the PDF to a image and draw that image on the specific range of the new page?  But when I try to use "pdfloaddoc.pages(i).ExportAsImage()" method, the result is a Bitmap type, and graphic.drawimage needs a pdfBitmap, so how can I convert a Bitmap to a pdfBitmap?

Thanks.
Tony


KC Karthikeyan Chandrasekar Syncfusion Team October 23, 2018 10:08 AM UTC

Hi Tony, 
We have created the sample for drawing content in page by rotating the page graphics as 270 degree and using PdfTemplate the content was rotated to 0 degree. The page content preserved properly with the 90 degree rotation implementation. Please find the sample that we have created from our side. 


Kindly check the sample, if still issue exist in your input PDF document. Kindly share the input PDF, we will check and provide you the solution. 

Bitmap to PdfBitmap: 
We can pass the Bitmap as a parameter to the PdfBitmap constructor. Please find the code snippet to convert the Bitmap to PdfBitmap. 

PdfLoadedDocument ldoc = new PdfLoadedDocument("270.pdf"); 
 
Bitmap map = ldoc.ExportAsImage(0); 
 
PdfBitmap pdfBitmap = new PdfBitmap(map); 


Kindly try our suggestion and let us know if you need further assistance in this. 
Regards, 
Karthikeyan 


Loader.
Up arrow icon