Changing the width of an existing pdf document

Hello,

I want to change the width of every page in a pdf document, because later I want to add some handwritten notes to the left and right side of a pdf page.


  document.PageSettings.Size = new SizeF(20, 400);
                for (int i = 0; i < lDoc.Pages.Count; i++)
                {
                    document.ImportPage(lDoc, i);
 }

3 Replies

SP Sathya Ponnusamy Syncfusion Team November 24, 2017 12:02 PM UTC

Hi JJads, 

Thank you contacting Syncfusion support. 

Currently we do not have support to resize existing document. However, as a workaround we can achieve your requirement by using PdfTemplate. 
Please find below code snippet and workaround sample for your reference. 

Code snippet: 
string path = AppDomain.CurrentDomain.BaseDirectory + @"Data\sample.pdf"; 
 
// Create a new instance of PdfLoadedDocument class. 
PdfLoadedDocument document = new PdfLoadedDocument(path); 
 
// Create a new instance of PdfDocument class.  
PdfDocument doc = new PdfDocument(); 
 
//Set Margin for PdfDocument page 
doc.PageSettings.Margins.Left = 50; 
doc.PageSettings.Margins.Right = 50; 
doc.PageSettings.Margins.Top = 0; 
doc.PageSettings.Margins.Bottom = 0; 
 
for (int i = 0; i < document.Pages.Count; i++) 
{ 
   PdfTemplate template = document.Pages[i].CreateTemplate(); 
   PdfPage page = doc.Pages.Add(); 
 
   // Create Pdf graphics for the page. 
   PdfGraphics g = page.Graphics; 
 
   g.DrawPdfTemplate(template, new PointF(0, 0), new SizeF(page.GetClientSize().Width, page.GetClientSize().Height)); 
} 
 
doc.Save("output.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Open); 
 
return null; 
 

Sample Link:  

Please find below UG documentation link for PdfTemplate, 

Kindly try the given work around sample and let us know whether the given sample is satisfied your requirements.  

Please let us know if you need further assistance on this.  

Regards, 
Sathya P 



JJ jjads November 24, 2017 03:44 PM UTC

thanks, but when changing the margin, the document will be compressed, shrinked so that the text is not readable anymore (Margins.... =250 ) , it shouldnt be like that.


SP Sathya Ponnusamy Syncfusion Team November 27, 2017 12:36 PM UTC

Hi Srikanth, 

Thanks for your update. 

We have checked your query and this behavior is occurred due to margin value is larger. While, setting margin for PDF document, page width will be reduced based on left and right margin values. So, only this behavior is occurred. To, avoid this behavior we have to update document size. Please find the below code snippet, it will meet your requirement. 

Note: Please assign required margin value for highlighted variables. 

Code snippet: 
string path = AppDomain.CurrentDomain.BaseDirectory + @"Data\sample.pdf"; 
 
// Create a new instance of PdfLoadedDocument class. 
PdfLoadedDocument document = new PdfLoadedDocument(path); 
 
// Create a new instance of PdfDocument class.  
PdfDocument doc = new PdfDocument(); 
 
// define left and right margin here 
float left = 250; 
float right = 250; 
float top = 0; 
float bottom = 0; 
 
SizeF size = new SizeF(document.Pages[0].Size.Width + left + right, document.Pages[0].Size.Height + top + bottom); 
 
//Need to set page size here, if not it will use default size. 
doc.PageSettings.Size = size; 
 
if (size.Width > size.Height) 
   doc.PageSettings.Orientation = PdfPageOrientation.Landscape; 
 
//Set Margin for PdfDocument page  
doc.PageSettings.Margins.Left = left; 
doc.PageSettings.Margins.Right = right; 
doc.PageSettings.Margins.Top = top; 
doc.PageSettings.Margins.Bottom = bottom; 
 
for (int i = 0; i < document.Pages.Count; i++) 
{ 
   PdfTemplate template = document.Pages[i].CreateTemplate(); 
   PdfPage page = doc.Pages.Add(); 
 
   // Create Pdf graphics for the page. 
   PdfGraphics g = page.Graphics; 
 
   g.DrawPdfTemplate(template, new PointF(0, 0), new SizeF(page.GetClientSize().Width, page.GetClientSize().Height)); 
} 
 
doc.Save("output.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Open); 
 
return null; 


Please try this and let me know if you have any questions. 

Regards, 
Sathya 


Loader.
Up arrow icon