Merge page into other page as layer

I am trying to create a layered PDF with 2 layers based on 2 input PDFS.


The main pdf is a design made by a customer. The second is a PDF we have which contains a cut path of a certain color and thickness.

We need to merge that path into the design made by the customer as a layer.

I tried the following, but the resultant PDF contains errors:

using (var cutDoc = new Aspose.Pdf.Document(“CUT.pdf”))
using (var doc = new Aspose.Pdf.Document(“Content.pdf”))
{
//Get the main document only page
var page = doc.Pages.OfType<Aspose.Pdf.Page>().First();

//Initiate the layers list
page.Layers = new List<Aspose.Pdf.Layer>();

//Add two layers
var cutLayer = new Aspose.Pdf.Layer(“0”, “CutContour”);
page.Layers.Add(cutLayer);
var contentLayer = new Aspose.Pdf.Layer(“1”, “Content”);
page.Layers.Add(contentLayer);

//Take the cutdocument only page
var cutPage = cutDoc.Pages.OfType<Aspose.Pdf.Page>().First();

//Copy the operators from the cutpage to the cutlayer
cutLayer.Contents.AddRange(cutPage.Contents.OfType<Aspose.Pdf.Operator>());

//Move the contents from the main content to the content layer
contentLayer.Contents.AddRange(page.Contents.OfType<Aspose.Pdf.Operator>());

//Clear the page main content so the content isn’t double
page.Contents.Clear();

//Save the document
doc.Save(“out.pdf”);
}

The operators from the CUT pdf reference to a certain color, but that color is missing from the PDF resources in the main document. I don’t know if that is the only error. The shape is a simple rectangle, but we also have CUT pdf files with more difficult paths we need to merge into PDF files.

Hi Stephan,

Thanks for contacting support.

I have tested the scenario and have managed to reproduce same problem. For the sake of correction, I have logged it as PDFNET-42218 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 this inconvenience.

As a workaround, you may consider adding PDF page as stamp object to other document. Please try using following code snippet.

[C#]

// Open document
Document pdfDocument = new Document(@"C:\pdftest\ Content.pdf");
Document cutDoc = new Document(@"C:\pdftest\ CUT.pdf");
// Create page stamp
PdfPageStamp pageStamp = new PdfPageStamp(cutDoc.Pages[1]);
pageStamp.Background = false;
//pageStamp.XIndent = 100;
// pageStamp.YIndent = 100;
// pageStamp.Rotate= Aspose.Pdf.Rotation.on180;
// Add stamp to particular page
pdfDocument.Pages[1].AddStamp(pageStamp);
dataDir = @"C:\pdftest\ PDFPageStamp_out.pdf";
// Save output document
pdfDocument.Save(dataDir);