|
Rank: Member Groups: Member
Joined: 8/4/2014 Posts: 11
|
How to generate one pdf file with several pages containing Acm-Elements on each page with page size A4. The page size need to be A4 (!!!!!!!! not A4 Letter !!!!!!!!!!!!). I can generate several documents with A4 pages and merge them to one pdf file as follows:
Code: C#
private MemoryStream Test()
{
int pages = 10;
PdfDocument[] docs = new PdfDocument[pages];
for (int i = 0; i < pages; i++)
{
docs[i] = new PdfDocument();
AcmRender render = new AcmRender(docs[i]);
render.SetDefPageSize(new SizeF(8.26f, 11.69f));
AcmText text = new AcmText("Doc " + i);
render.Render(text);
}
PdfDocument result = PdfDocument.Merge(docs);
using (MemoryStream memory = new MemoryStream())
{
result.Save(memory);
return memory;
}
}
But how to generate only one document with SizeF(8.26f, 11.69f) and add several pages in for loop? As soon as I add a new page using doc.Pages.Add() the documents becomes A4 Letter size.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,225
|
Hi, You would just reply on SetDefPageSize and use a single render. For example:
Code: C#
//Create one AcmRender object for a single PdfDocument
PdfDocument doc = new PdfDocument();
AcmRender render = new AcmRender(doc);
render.SetDefPageSize(new SizeF(8.26f, 11.69f));
//Builds contents list for all pages
List<AcmContent> contents = new List<AcmContent>();
for (int i = 0; i < pages; i++)
{
contents.Add(new AcmText("page " + i));
contents.Add(new AcmPageBreak());
}
//Render them all
render.Render(contents.ToArray());
Note that you would use AcmPageBreak object to force page breaks. Thanks!
|
|
Rank: Member Groups: Member
Joined: 8/4/2014 Posts: 11
|
A list of what? There is no List of nothing in c#......
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,225
|
Hi,
Sorry this is a bug of the syntax coloring code. The original code is List<AcmContent> contents = nw List<AcmContent>().
In any case, the code is for you to understand the idea, not for you to copy literally as most likely you will need to adapt the code to fit your specific need. The basic idea the code is trying to demonstrate here is that you use a single AcmRender to render all pages.
Thanks
|
|
Rank: Member Groups: Member
Joined: 8/4/2014 Posts: 11
|
Thank you. Could you please explain why are the elements each on a separate page? They should render on the same page. And why is the page break on a separate page? You can download the document here
Code: C#
private MemoryStream Test()
{
PdfDocument doc = new PdfDocument();
AcmRender render = new AcmRender(doc);
render.SetDefPageSize(new SizeF(8.26f, 11.69f));
List< AcmContent > contents = new List< AcmContent >();
float height = 2.65f;
float width = 7.86f;
for (int p = 0, x = 1; p < 20; p++, x++)
{
AcmBlock acmBlock1 = new AcmBlock();
acmBlock1.Style.Width = width;
acmBlock1.Style.Height = height;
acmBlock1.Style.Border = new AcmBorder(Color.Red, 0.001f);
AcmBlock acmBlock2 = new AcmBlock();
acmBlock2.Style.Width = width;
acmBlock2.Style.Height = height;
acmBlock2.Style.Border = new AcmBorder(Color.Green, 0.001f);
contents.Add(acmBlock1);
contents.Add(acmBlock2);
contents.Add(new AcmPageBreak());
}
render.Render(contents.ToArray());
using (MemoryStream memory = new MemoryStream())
{
doc.Save(memory);
return memory;
}
}
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,225
|
They are on separate pages because the code explicitly added an AcmPageBreak inside the loop. We put this there to show you how to create the output on separate page if that's what you wanted, if that's not what you wanted, just remove the contents.Add(new AcmPageBreak()).
Thanks
|
|
Rank: Member Groups: Member
Joined: 8/4/2014 Posts: 11
|
When I delete the contents.Add(new AcmPageBreak()); line, the document looks as follows. May I ask you how to place the acmBlock1 and acmBlock2 on the same page? Why they are still on a separate page?
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,225
|
Hi,
This is because your block width (7.86f) is greater than the available page width (8.26f - 1 inch default left margin - 1 inch default right margin = 6.26f). When this happens, the block is automatically pushed to the next page in the hope that the next page can have more space. If you reduce your block width to be less than 6.26f it should fit into the same page.
Thanks!
|
|
Rank: Member Groups: Member
Joined: 8/4/2014 Posts: 11
|
The AcmBlock was too width. Reducing the width solved the issue. Thank you very much!
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,225
|
Great. Glad that you got it working!
|
|