PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Split page spread
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3662
Page 1 of 1

Author:  wj89 [ Thu Sep 14, 2017 7:24 am ]
Post subject:  Split page spread

Hello, I am generating a new pdf document using Pdfsharp and Migradoc based on a template which is an A4 spread (two A5 pages side by side merged to a single page) I have two final issues to solve.

I need to be able to split the page in two and then rejoin it with the sides swapped around and I need to generate a large text version by splitting the document to two pages and scaling each side up to A4.

Any help would be greatly appreciated,

Regards,
Will

Author:  Thomas Hoevel [ Thu Sep 14, 2017 8:19 am ]
Post subject:  Re: Split page spread

Hi!

I would use MigraDoc to create A5 pages. You can then use RenderPage to render two A5 pages onto one A4 page - and at the same time re-arrange the pages to get a printout in booklet style.

You can use the same pattern to scale A5 pages onto A4 pages.

See this sample (full source code in the download package):
http://www.pdfsharp.net/wiki/MixMigraDo ... ample.ashx
It draws 9 pages on one, pages scaled down to fit.

Author:  wj89 [ Thu Sep 14, 2017 9:36 am ]
Post subject:  Re: Split page spread

Thanks for your quick reply and all your work on the library.

I have to start off with the A4 spread template as a base as it already has a lot of manually formatted elements inserted, is there some way to copy half the page to a new page? I've been playing with CropBox's but so far have only got to the point where I can save the document as one of the A5 pages

I think I'm close with:
PdfPage pageOneCopy = (PdfPage)pageOne.Clone();
PdfPage pageTwoCopy =(PdfPage) pageOne.Clone();
pageOneCopy.CropBox = new PdfRectangle(new XPoint(0, 0),new XSize(pageOne.Width / 2, pageOne.Height));
pageTwoCopy.CropBox = new PdfRectangle(new XPoint(pageOne.Width / 2, 0),new XSize(pageOne.Width / 2, pageOne.Height));

PdfDocument outputDoc1 = new PdfDocument();
outputDoc1.AddPage(pageOneCopy);
outputDoc1.AddPage(pageTwoCopy);
but this doesn't save properly and only the first page is visible.

Author:  Thomas Hoevel [ Thu Sep 14, 2017 9:59 am ]
Post subject:  Re: Split page spread

You can copy the entire page, scale it as needed, and use white rectangles to hide parts that should not be visible (e.g. the other A5 page).
The white rectangles are a hack, but maybe you don't need that.

You can use DrawImage to draw existing PDF pages onto new PDF pages as shown here:
http://www.pdfsharp.net/wiki/TwoPagesOnOne-sample.ashx
DrawImage has overloads where you supply the destination size.

Author:  wj89 [ Thu Sep 14, 2017 10:54 am ]
Post subject:  Re: Split page spread

Thanks,
I'm struggling with copying the page.
Code:
pageOneCopy.CropBox = new PdfRectangle(new XPoint(0, 0),new XSize(pageOne.Width / 2, pageOne.Height));
pageTwoCopy.CropBox = new PdfRectangle(new XPoint(pageOne.Width / 2, 0), new XSize(pageOne.Width / 2, pageOne.Height));
PdfDocument outputDoc1 = new PdfDocument();
outputDoc1.PageLayout = PdfPageLayout.SinglePage;
outputDoc1.AddPage(pageOneCopy);
PdfDocument outputDoc2 = new PdfDocument();
outputDoc2.AddPage(pageTwoCopy);
string tempName = Guid.NewGuid().ToString();
outputDoc1.Save(outputLoc+tempName + "-1.pdf");
outputDoc2.Save(outputLoc+tempName + "-2.pdf");

PdfDocument finalOutputDoc = new PdfDocument();
finalOutputDoc.PageLayout = PdfPageLayout.SinglePage;
               
PdfPage page = finalOutputDoc.AddPage();
page.Orientation = PdfSharp.PageOrientation.Landscape;
string newPageOne = outputLoc+tempName + "-2.pdf";
string newPageTwo = outputLoc+tempName + "-1.pdf";
XGraphics gfx;
double width = page.Width;
double height = page.Height;

gfx = XGraphics.FromPdfPage(page);
DrawImage(gfx,newPageOne , 0,0, width / 2, height);
DrawImage(gfx, newPageTwo , width / 2, 0, width / 2, height);



the two outputted files appear to be the individual pages, however when the final output document is displayed it has the full spread squished onto the page twice.

Author:  Thomas Hoevel [ Thu Sep 14, 2017 11:38 am ]
Post subject:  Re: Split page spread

wj89 wrote:
the two outputted files appear to be the individual pages, however when the final output document is displayed it has the full spread squished onto the page twice.
Maybe the crop box does not work and you see something that should be clipped out.

I hope you use PDFsharp 1.50.

If you think there is a bug in PDFsharp 1.50, please use the Issue Submission Template:
http://www.pdfsharp.net/wiki/IssueSubmissions.ashx

Author:  wj89 [ Fri Sep 15, 2017 3:39 am ]
Post subject:  Re: Split page spread

Thanks for your help Thomas, got it working.

Here's my code for anyone else who needs to do this: (template is my pdf document which has been modified in prior code)
To swap side of spread:
Code:
                MemoryStream stream = new MemoryStream();
                template.Save(stream, false);

                XPdfForm form = XPdfForm.FromStream(stream);

                XRect pageRect = new XRect(0, 0, pageOne.Width, pageOne.Height);
                template.Pages.Remove(pageOne);
                PdfPage newPage = template.AddPage();
                newPage.Orientation = PdfSharp.PageOrientation.Landscape;
                newPage.Size = PdfSharp.PageSize.A4;

                XGraphics canvasOneN = XGraphics.FromPdfPage(newPage);
                canvasOneN.DrawImage(form, -(pageOne.Width / 2), 0, pageOne.Width, pageOne.Height);
                canvasOneN.DrawImage(form, pageOne.Width / 2, 0, pageOne.Width, pageOne.Height);
                template.Save(outputPath);

To turn A4 spread into two A4 pages:
Code:
                MemoryStream stream = new MemoryStream();
                template.Save(stream, false);
                template.Pages.Remove(pageOne);

                XPdfForm form = XPdfForm.FromStream(stream);
                PdfPage largePageOne = template.AddPage();
                largePageOne.Orientation = PdfSharp.PageOrientation.Portrait;
                largePageOne.Size = PdfSharp.PageSize.A4;
                PdfPage largePageTwo = template.AddPage();
                largePageTwo.Orientation = PdfSharp.PageOrientation.Portrait;
                largePageTwo.Size = PdfSharp.PageSize.A4;
                XGraphics largeCanvasOne = XGraphics.FromPdfPage(largePageOne);
                XGraphics largeCanvasTwo = XGraphics.FromPdfPage(largePageTwo);
                largeCanvasOne.DrawImage(form, -(largePageOne.Width), 0, largePageTwo.Width * 2, largePageTwo.Height);
                largeCanvasTwo.DrawImage(form, 0, 0, largePageTwo.Width * 2, largePageTwo.Height);
                template.Save(outputPath);


My only issue now is a null reference exception when I try to save after setting the PdfSecuritySettings

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/