Rank: Newbie Groups: Member
Joined: 6/8/2012 Posts: 6
|
I'm trying to combine pages from one pdf document into another (one has portrait pages and the other has landscape). I'm struggling to figure out how to insert a page into the portrait PDFDocument using the Pages.Insert method. I cannot use the Merge method because the pages must be in a specific order.
Here's what I'm trying:
// Create the Portrait PDF Document PdfDocument portraitPdfDoc = new PdfDocument(); HtmlToPdf.Options.PageSize = new SizeF(8.5f, 11f); // Portrait Letter HtmlToPdf.Options.OutputArea = new RectangleF(0.5f, 0.5f, 7.5f, 10f); HtmlToPdf.ConvertHtml(sPortraitHtml, portraitPdfDoc);
// Create the Landscape PDF Document PdfDocument landscapePdfDoc = new PdfDocument(); HtmlToPdf.Options.PageSize = new SizeF(11f, 8.5f); // Landscape Letter HtmlToPdf.Options.OutputArea = new RectangleF(0.5f, 0.5f, 10f, 7.5f); HtmlToPdf.ConvertHtml(sLandscapeHtml, landscapePdfDoc);
// Insert the Landscape page into the Portrait document portraitPdfDoc.Pages.Insert(1, landscapePdfDoc.Pages[0]);
The Pages.Insert cases and exception stating "The object already has an owner. Please use a clone of the object instead." How can I make a clone of a PdfPage without creating a third PdfDocument? It doesn't look like a PdfPage object has a .Clone() method.
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,195
|
Hi, You can not use Insert. The only way to get a PdfPage from one PdfDocument to another PdfDocument is to merge two PdfDocument objects into one: http://www.essentialobjects.com/doc/4/eo.pdf.pdfdocument.merge_overloads.aspxAs such if you want to take a single PdfPage from an existing PdfDocument and put it into another PdfDocument, you have to call Split to Split the first PdfDocument first: http://www.essentialobjects.com/doc/4/eo.pdf.pdfdocument.split.aspxFor example, if you have PdfDocument A with 5 pages and PdfDocument B with 4 pages, but you wish to have A.Pages[3] inserted as the second page of B, then you would need to do:
Code: C#
//This returns three PdfDocument:
// docAPieces[0] -- page 1, 2, 3
// docAPieces[1] -- page 4
// docAPieces[2] -- page 5
PdfDocument[] docAPieces = docA.Split(3, 4);
//This returns two PdfDocument
// docBPieces[0] -- page 1
// docBPieces[1] -- page 2, 3, 4
PdfDocument[] docBPieces = docB.Split(1);
//Merge them all together
PdfDocument result = PdfDocument.Merge(
docBPieces[0], //B's page 1
docAPieces[1], //A's page 4
docBPieces[2]); //B's page 2, 3, 4
Hope this helps. Please feel free to let us know if you still have any questions. Thanks!
|
Rank: Newbie Groups: Member
Joined: 6/8/2012 Posts: 6
|
That worked. Thanks!
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,195
|
Great! Glad to hear that it works for you. Please feel free to let us know if there is anything else.
Thanks!
|