Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Nov 23, 2021 4:41 pm

I would like to copy the content from a single layer on a page.

The destination for this content may be a layer in another document, or a different page within the same document.

Is this possible currently?


If it isn't could it be added? I'd like it to work similar to when you create a page template (possibly using the layer name as a parameter in that method)?

emaginationstore
 
Posts: 15
Joined: Mon Sep 25, 2017 2:02 pm

Wed Nov 24, 2021 10:18 am

Hello,

Thank you for your inquiry.
According to your description, I need to confirm with you that you just need to copy the content of the layer to a new page (new file or the same file)? You don't need to keep the layer object of the PDF, right?

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1648
Joined: Wed Apr 07, 2021 2:50 am

Wed Nov 24, 2021 11:00 am

Hi,

As an example, I currently have one PDF with 2 layers and another PDF with 3 layers.

I need to merge the content of these PDFs into a new, while keeping the content on their own layer.

My idea was to copy the content of each layer in the source PDFs and paste into a new PDF, creating a new layer in the destination PDF with the same name as the source layer.

I have found a work around since creating the original post. I am opening a copy of the source PDF and deleting all but one layer, copying that content into the destination PDF on a new layer which has the same layer name as the source layer. I'm then closing the source PDF, re-opening it and repeating the process for the next layer, until I have done all layers, then moving onto the next source PDF and doing the same. This is working but it seems a bit hacky to have to open the source document multiple times.

Thanks

emaginationstore
 
Posts: 15
Joined: Mon Sep 25, 2017 2:02 pm

Thu Nov 25, 2021 6:09 am

Hello,

Thank you for your feedback.
According to your description, you want to merge multiple PDF files with layers. Actually, our Spire.PDF supports merging multiple PDF files and the result file retains all layers of the source file (including the layer name). Please refer to the sample code below. If there is any question, please feel free to write back.
Code: Select all
 FileStream stream1 = File.OpenRead("input1.pdf");
 FileStream stream2 = File.OpenRead("input2.pdf");
 //Pdf document streams
 Stream[] streams = new Stream[] { stream1, stream2 };
 PdfDocumentBase doc = PdfDocument.MergeFiles(streams);
 doc.Save("result.pdf", FileFormat.PDF);

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1648
Joined: Wed Apr 07, 2021 2:50 am

Tue Nov 30, 2021 8:24 am

Hello,

Hope you are doing well!
How is your issue going? Did the code we provided work for you? Any feedback will be greatly appreciated.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1648
Joined: Wed Apr 07, 2021 2:50 am

Wed Jan 19, 2022 11:54 am

Hello,

As a work around to this, I am enumerating the layers on the original document and for each layer I am creating a temporary document which is a copy of the original, deleting all the layers (and content on those layers) and then creating a template from the temporary document to draw into the new document.

Hope this makes sense.

Code: Select all
var newDoc = new PdfDocument();
            using (var ms = new MemoryStream())
            {
                doc.SaveToStream(ms);
                for (var i = 0; i < doc.Pages.Count; i++)
                {
                    var newPage = newDoc.Pages.Add(new SizeF(width, height), new PdfMargins(margin));
                    foreach (PdfLayer layer in doc.Layers)
                    {
                        var newLayer = doc.Layers[layer.Name] ?? doc.Layers.AddLayer(layer.Name, layer.Visibility);
                        var newCanvas = newLayer.CreateGraphics(newPage.Canvas);
                        var tempdoc = new PdfDocument();
                        tempdoc.LoadFromStream(ms);
                        var toRemove = new List<PdfLayer>();
                        foreach (PdfLayer pdfLayer in tempdoc.Layers)
                        {
                            if (pdfLayer.Name != layer.Name)
                                toRemove.Add(pdfLayer);
                        }

                        foreach (var t in toRemove)
                        {
                            tempdoc.Layers.RemoveLayer(t, true);
                        }
                        var template = tempdoc.Pages[i].CreateTemplate();
                        newCanvas.DrawTemplate(template,new PointF(0,0), new SizeF(width, height));
                        tempdoc.Close();
                    }
                }
            }
            return newDoc;


Thanks

emaginationstore
 
Posts: 15
Joined: Mon Sep 25, 2017 2:02 pm

Thu Jan 20, 2022 11:07 am

Hello,

Thanks for your feedback.
Sorry, I tested your code, but the program reported an error. I will investigate the cause as soon as possible and give you feedback.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1648
Joined: Wed Apr 07, 2021 2:50 am

Fri Jan 21, 2022 6:10 am

Hello,

Thanks for your patience.
Please use the following modified code to achieve your needs. If there is any question, please feel free to write back.
Code: Select all
using (var ms = new MemoryStream())
{
    //Converting the source document to memory stream staging
    doc.SaveToStream(ms);
    for (var i = 0; i < doc.Pages.Count; i++)
    {
        width = doc.Pages[i].ActualSize.Width;
        height = doc.Pages[i].ActualSize.Height;
        //Add a new page to the new document with the same size as the original document page.
        var newPage = newDoc.Pages.Add(doc.Pages[i].ActualSize, new PdfMargins(margin));
        //Traverse source document layers
        foreach (PdfLayer layer in doc.Layers)
        {
            //Determine whether the new document already contains a layer with the corresponding name, and if so, use it. If not, add one.
            var newLayer = newDoc.Layers[layer.Name] ?? newDoc.Layers.AddLayer(layer.Name, layer.Visibility);
            //Get the canvas of the corresponding layer on the new page
            var newCanvas = newLayer.CreateGraphics(newPage.Canvas);
            //Create a temporary document object
            var tempdoc = new PdfDocument();
            //Load source document data
            tempdoc.LoadFromStream(ms);
            //Collection of layers to delete
            var toRemove = new List<PdfLayer>();
            //Traverse the layers in the temporary document and add all layers except the target layer to the to-be-deleted list
            foreach (PdfLayer pdfLayer in tempdoc.Layers)
            {
                if (pdfLayer.Name != layer.Name)
                    toRemove.Add(pdfLayer);
            }
            //Traverse the to-be-deleted list and delete the layers one by one
            foreach (var t in toRemove)
            {
                tempdoc.Layers.RemoveLayer(t, true);
            }
            //At this point, there is only one layer left, so create a template for the entire page directly, and then draw it to the new page
            var template = tempdoc.Pages[i].CreateTemplate();
            newCanvas.DrawTemplate(template, new PointF(0, 0), new SizeF(width, height));
        }
    }
}
newDoc.SaveToFile("result.pdf");

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1648
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.PDF