PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 3:20 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Split page spread
PostPosted: Thu Sep 14, 2017 7:24 am 
Offline

Joined: Thu Sep 14, 2017 6:56 am
Posts: 5
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Split page spread
PostPosted: Thu Sep 14, 2017 8:19 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: Split page spread
PostPosted: Thu Sep 14, 2017 9:36 am 
Offline

Joined: Thu Sep 14, 2017 6:56 am
Posts: 5
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Split page spread
PostPosted: Thu Sep 14, 2017 9:59 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: Split page spread
PostPosted: Thu Sep 14, 2017 10:54 am 
Offline

Joined: Thu Sep 14, 2017 6:56 am
Posts: 5
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Split page spread
PostPosted: Thu Sep 14, 2017 11:38 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: Split page spread
PostPosted: Fri Sep 15, 2017 3:39 am 
Offline

Joined: Thu Sep 14, 2017 6:56 am
Posts: 5
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 138 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group