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.