Articles in this section
Category / Section

How to copy content from existing PDF to new PDF using C# in WinForms PDF?

9 mins read

The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can copy the content from an existing PDF document to the middle page of a new PDF document using C# and VB.NET in  Winforms PDF feature tour page.

Steps to copy the content from an existing PDF document to the middle page of a new PDF document programmatically:

  1. Create a new Windows Forms application project.

Create a new Windows Forms application

  1. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.

NuGet package reference

  1. Include the following namespaces in the Form1.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
using System.Drawing;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Parsing
Imports System.Drawing

 

  1. Add the following code to copy the content from an existing PDF document to the middle page of a new PDF document.

C#

string file = "../../Data/input.pdf";
 
//Crop an existing PDF document
MemoryStream croppedStream = CropPDF(file);
 
//Load the existing PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(croppedStream);
 
//Load the page
PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage;
 
//Create the template from the page
PdfTemplate template = loadedPage.CreateTemplate();
 
//Create a new PDF document
PdfDocument document = new PdfDocument();
 
//Add a page to the document
PdfPage page = document.Pages.Add();
 
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
 
//Draw the template
graphics.DrawPdfTemplate(template, new PointF(0, page.GetClientSize().Height / 2), new SizeF(page.GetClientSize().Width, page.GetClientSize().Height / 2));
 
//Save the document
document.Save("Output.pdf");
 
//Close the document
document.Close(true);
 
//This will open the PDF file and the result will be seen in the default PDF Viewer
Process.Start("Output.pdf");

 

Helper methods:

private MemoryStream CropPDF(string file)
{
    //Load the existing PDF document
    PdfLoadedDocument loadedDocument = new PdfLoadedDocument(file);
 
    //Load the page
    PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage;
 
    //Create the template from the page
    PdfTemplate template = loadedPage.CreateTemplate();
 
    //Create a new PDF document
    PdfDocument document = new PdfDocument();
 
    //Calculate the height from inches
    float inches = 3.5f;
    float height = inches * 72;
 
    //Set the document margin and height
    document.PageSettings.Height = height;
    document.PageSettings.Margins.All = 0;
 
    //Add the page to new PDF document
    PdfPage page = document.Pages.Add();
 
    //Create the graphics
    PdfGraphics graphics = page.Graphics;
 
    //Draw the template
    graphics.DrawPdfTemplate(template, new PointF(0, 0)); 
 
    MemoryStream stream = new MemoryStream();
    //Save the new document
    document.Save(stream);
 
    //Close the documents
    loadedDocument.Close(true);
    document.Close(true);
 
    return stream;
}

 

 

VB.NET

Dim file As String = "../../Data/input.pdf"
 
'Crop an existing PDF document 
Dim croppedStream As MemoryStream = CropPDF(file)
 
'Load the existing PDF document 
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument(croppedStream)
 
'Load the page 
Dim loadedPage As PdfLoadedPage = TryCast(loadedDocument.Pages(0), PdfLoadedPage)
 
'Create the template from the page 
Dim template As PdfTemplate = loadedPage.CreateTemplate()
 
'Create a new PDF document 
Dim document As PdfDocument = New PdfDocument()
 
'Add a page to the document
Dim page As PdfPage = document.Pages.Add()
 
'Create PDF graphics for the page
Dim graphics As PdfGraphics = page.Graphics
 
'Draw the template
graphics.DrawPdfTemplate(template, New PointF(0, page.GetClientSize().Height / 2), New SizeF(page.GetClientSize().Width, page.GetClientSize().Height / 2))
 
'Save the document
document.Save("Output.pdf")
 
'Close the document
document.Close(True)
 
'This will open the PDF file and the result will be seen in the default PDF Viewer
Process.Start("Output.pdf")

 

Helper methods:

Private Function CropPDF(ByVal file As String) As MemoryStream
 
    'Load the existing PDF document
    Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument(file)
 
    'Load the page
    Dim loadedPage As PdfLoadedPage = TryCast(loadedDocument.Pages(0), PdfLoadedPage)
 
    'Create the template from the page
    Dim template As PdfTemplate = loadedPage.CreateTemplate()
 
    'Create a new PDF document
    Dim document As PdfDocument = New PdfDocument()
 
    'Calculate the height from inches
    Dim inches As Single = 3.5F
    Dim height As Single = inches * 72
 
    'Set the document margin and height
    document.PageSettings.Height = height
    document.PageSettings.Margins.All = 0
 
    'Add the page to new PDF docment 
    Dim page As PdfPage = document.Pages.Add()
 
    'Create the graphics
    Dim graphics As PdfGraphics = page.Graphics
 
    'Draw the template
    graphics.DrawPdfTemplate(template, New PointF(0, 0))
 
    'Save the new document
    Dim stream As MemoryStream = New MemoryStream()
    document.Save(stream)
 
    'Close the documents
    loadedDocument.Close(True)
    document.Close(True)
 
    Return stream
 
End Function

 

A complete working sample can be download from CopyContentSample.zip.

 

By executing the program, you will get the output document as follows,

 

Output document screenshot

 

Take a moment to peruse the documentation. You can find other options like creating a new PDF template, creating templates from an existing PDF document, and creating document overlays.

 

Refer to this link to explore a rich set of Syncfusion Essential® PDF features.

 

Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or the NuGet feed, include a license key in your product. Refer to this link to learn about generating and registering the Syncfusion® license key in your application to use the components without trail message.

 

See Also:

How to crop PDF in Windows Forms

How to crop PDF in Xamarin Forms


Conclusion

I hope you enjoyed learning about how to copy content from existing PDF to new PDF using C# in WinForms PDF.

You can refer to our Winforms PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied