PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Apr 28, 2024 5:34 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Tue Nov 26, 2013 9:22 pm 
Offline

Joined: Tue Nov 26, 2013 7:51 pm
Posts: 5
I am using MigraDoc 1.32.3885 to generate a pdf and no matter what I try, the page height is never set to what I want.

It is a very simple pdf generation function. The pdf gets generated but in the print dialog under Acrobat reader the actual page size is about 6 cm and it tries to shrink everything to that page size.

Thanks.

Code:
       public void GeneratePdf()
        {
            Document doc = PdfPrinting.CreateDocument("My Print Document", "Receipt", "MySelf");

            PageSetup pagesetup = doc.DefaultPageSetup.Clone();
            pagesetup.LeftMargin = Unit.FromCentimeter(LeftMargin);
            pagesetup.RightMargin = Unit.FromCentimeter(RightMargin);
            pagesetup.PageWidth = Unit.FromCentimeter(PageWidth);
            pagesetup.TopMargin = Unit.FromCentimeter(TopMargin);
            pagesetup.BottomMargin = Unit.FromCentimeter(BottomMargin);


            Section sec = doc.AddSection();
            sec.PageSetup = pagesetup;

            TextFormat TextDecoration = TextFormat.NotBold | TextFormat.NotItalic | TextFormat.NoUnderline;

            ParagraphFormat Format = new ParagraphFormat();
            Format.Alignment = ParagraphAlignment.Left;
            Format.Font.Name = FontName;
            Format.Font.Size = FontSize;

            Paragraph para = null;
            string text = "";

            // PrintLinesList = an ArrayList of strings
            for (int i = 0; i < PrintLinesList.Count; i++)
            {
                txt = (string)PrintLinesList[i];

                para = new Paragraph();
                para.Format = Format.Clone();
                para.AddFormattedText(Text, TextDecoration);

                sec.Add(para);
            }
            double height = 2.0 + ((double)PrintLinesList.Count) / 3.0 + (PrintLinesList.Count % 3 > 0 ? 1.0 : 0.0);
            // ************************* set the page height estimating about three lines per cm using the selected font and size
            pagesetup.PageHeight = Unit.FromCentimeter(height);

            PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
            renderer.Document = doc;
            renderer.RenderDocument();

            MemoryStream stream = new MemoryStream();
            renderer.PdfDocument.Save(stream, false);

            Response.Clear();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-length", stream.Length.ToString());
            Response.BinaryWrite(stream.GetBuffer());
            Response.Flush();
            stream.Close();
            Response.End();
        }


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 27, 2013 8:44 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
You should add TopMargin and BottomMargin to your page height.

That code snippet is far from SSCCE and we don't know how many test strings you used.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 27, 2013 3:04 pm 
Offline

Joined: Tue Nov 26, 2013 7:51 pm
Posts: 5
Thanks for the suggestion.

These margins were set at the top:

pagesetup.TopMargin = Unit.FromCentimeter(TopMargin);
pagesetup.BottomMargin = Unit.FromCentimeter(BottomMargin);

I will consider doing a win form app for the SSCCE.

The number of string could variable but in this case is 50. I am basically generating a list of items on the pdf to be printed on a receipt printer (Epson TM-T88V) and the height of the page needs to be nonstandard to whatever the number of lines in the list. The printer uses paper rolls and should cut the paper after all the items have been printed.

Thanks again.


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 27, 2013 3:58 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
kiskeya wrote:
These margins were set at the top
Yes. But page height includes these margins, but not in your code.
If you need 3 cm top margin and 3 cm bottom margin and 8 cm for text lines, then you will need a page height of 14 cm. But your code would set 8 cm only because you don't add the margins.

This could make a big difference already.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 27, 2013 4:14 pm 
Offline

Joined: Tue Nov 26, 2013 7:51 pm
Posts: 5
Thomas Hoevel wrote:
kiskeya wrote:
These margins were set at the top
Yes. But page height includes these margins, but not in your code.
If you need 3 cm top margin and 3 cm bottom margin and 8 cm for text lines, then you will need a page height of 14 cm. But your code would set 8 cm only because you don't add the margins.

This could make a big difference already.


I see what you mean but I did take that into account in the height calculation ( 1 cm top and 1 cm bottom = 2 which I added):

double height = 2.0 + ((double)PrintLinesList.Count) / 3.0 + (PrintLinesList.Count % 3 > 0 ? 1.0 : 0.0);

But you are right, it should have been:

double height = TopMargin + BottomMargin + ((double)PrintLinesList.Count) / 3.0 + (PrintLinesList.Count % 3 > 0 ? 1.0 : 0.0);

Thanks.


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 27, 2013 6:39 pm 
Offline

Joined: Tue Nov 26, 2013 7:51 pm
Posts: 5
I have attached the SSCCE but the MigraDoc 1.32.3885 dlls are not included to keep the size to under 256kb.

I also included a capture of the Adobe reader display of the generated pdf (pdfgenerator-1.png) and captures of the Adobe reader's print dialog (pdfgenerator-2.png).

In the size options the "fill option" try to print everything on one page about 9.42cm the page size calculated and set by the program was 20cm.

The "actual size" option displays and prints only the middle half portion of the pdf.

Thanks.


Attachments:
pdfgenerator-2.png
pdfgenerator-2.png [ 78.3 KiB | Viewed 8674 times ]
pdfgenerator-1.png
pdfgenerator-1.png [ 67.19 KiB | Viewed 8674 times ]
MigraDocSSCCE.zip [23.54 KiB]
Downloaded 396 times
Top
 Profile  
Reply with quote  
PostPosted: Thu Nov 28, 2013 9:18 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
I tried custom page size (roll feed (green ellipse) is not supported by my printer; ignore the red ellipse):
Attachment:
image1.png
image1.png [ 59.11 KiB | Viewed 8670 times ]


The Properties button took me to the dialog shown on the first picture (but Page Setup may be helpful for printers with roll feeds):
Attachment:
image2.png
image2.png [ 52.48 KiB | Viewed 8670 times ]


The PDF appears to be correct, so the problem is finding the correct settings for Adobe Reader to print it.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Nov 28, 2013 3:12 pm 
Offline

Joined: Tue Nov 26, 2013 7:51 pm
Posts: 5
Thomas,

Thanks a lot for your help.

I'll play with the printing settings.

I consider this thread closed.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 385 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