We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Resize pages in order to fit on A4 when printing

Hi,

I am making an application that is merging pages from different existing PDF documents and afterwards saving to a new PDF document, which is silent printed.

My problem is that some of the merged pages isn't A4 format. I want to either resize when creating the new PDF, or resize when the new PDF is printed.
Can you please help with any solution on this?

Code used for merging pages into new PDF:
PdfDocument PdfToPrint = new PdfDocument();
PdfToPrint.PageSettings.Size = PdfPageSize.A4;

PdfLoadedDocument LoadedPdf = new PdfLoadedDocument(Filepath);
PdfToPrint.ImportPageRange(LoadedPdf, FromPage - 1, ToPage - 1);

PdfToPrint.Save("Generated files/" + Itemnumber + ".pdf");

Code used for printing the new PDF:
PdfDocumentView viewer = new PdfDocumentView();
viewer.Load("Generated files/" + filename + ".pdf");

PrintDialog dialog = new PrintDialog();
dialog.Document = viewer.PrintDocument;

dialog.Document.Print();

viewer.Dispose();

1 Reply

SK Sasi Kumar Sekar Syncfusion Team January 23, 2017 12:26 PM UTC

Hi Jesper, 

Thank you for contacting Syncfusion support, 

We can achieve your requirements by adding the all PDF documents into the single PDF document in A4 size using the PdfTemplate. We have created the simple sample for your requirement. Please find the code snippet and sample for your reference. 

Cod snippet: 
//Gets the file directory 
  String[] inputDocuments = Directory.GetFiles("../../Data/"); 
 
//Creates a new instance. 
  PdfDocument document = new PdfDocument(); 
 
  document.PageSettings.Size = PdfPageSize.A4; 
  document.PageSettings.Margins.All = 0; 
 
//Optimizes memory while merging PDF documents. 
  document.EnableMemoryOptimization = true; 
 
//Iterates input PDF documents. 
  foreach (string inputDocument in inputDocuments) 
   { 
     //Loads PDF document in order to merge. 
     PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputDocument); 
     loadedDocument.EnableMemoryOptimization = true; 
 
    //Merges loaded document contents to the parent PDF document.  
    for (int i = 0; i < loadedDocument.Pages.Count; i++) 
      { 
         PdfPage page = document.Pages.Add(); 
         PdfGraphics g = page.Graphics; 
         PdfPageBase lpage = loadedDocument.Pages[i]; 
         PdfTemplate template = lpage.CreateTemplate(); 
         g.DrawPdfTemplate(template, System.Drawing.PointF.Empty, g.Size); 
      }                
               
       //Closes the document. 
       loadedDocument.Close(true) 
 
   } 
   //Saves and closes the document. 
   document.Save("Output.pdf"); 
   document.Close(true);        

Sample link: 

Please let us know if you need further assistance. 

Regards, 
Sasi Kumar S. 


Loader.
Up arrow icon