Welcome Guest Search | Active Topics | Sign In | Register

Multiple pages with page size A4 using AcmRender Options
K. Zimny
Posted: Wednesday, July 12, 2017 9:57:15 AM
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.
eo_support
Posted: Wednesday, July 12, 2017 11:28:47 AM
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!
K. Zimny
Posted: Wednesday, July 12, 2017 12:18:15 PM
Rank: Member
Groups: Member

Joined: 8/4/2014
Posts: 11
A list of what? There is no List of nothing in c#......
eo_support
Posted: Wednesday, July 12, 2017 1:58:43 PM
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
K. Zimny
Posted: Wednesday, July 12, 2017 2:44:14 PM
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;
            }
        }
eo_support
Posted: Wednesday, July 12, 2017 2:52:08 PM
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
K. Zimny
Posted: Wednesday, July 12, 2017 3:02:53 PM
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?
eo_support
Posted: Wednesday, July 12, 2017 5:39:46 PM
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!
K. Zimny
Posted: Thursday, July 13, 2017 4:12:11 AM
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!
eo_support
Posted: Thursday, July 13, 2017 8:05:17 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,225
Great. Glad that you got it working!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.