PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Apr 23, 2024 6:14 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed Aug 10, 2016 12:15 am 
Offline

Joined: Tue Aug 09, 2016 10:55 pm
Posts: 3
Hey everyone!

I am currently working on an ASP.NET 4.5.2 C# web application. This application will take some data from the page and print a receipt that is 3.15 inches (80 mm) wide and the appropriate length (among other things). Right now, I am using MigraDoc 1.32.3885.0 and I do have it working in the sense that it prints, but it prints a very long receipt, much much longer than it needs to be (especially considering my test is a single line: "Hello World").

Does anyone have any idea how to make it so the receipt is a reasonable length? Do I need to use PDFsharp instead of MigraDoc considering my size requirements?

You can find the relevant code below:

Code:
        private void print()
        {
            Document document = CreateDocument();
            document.UseCmykColor = true;
            const bool unicode = false;

            const PdfFontEmbedding embedding = PdfFontEmbedding.Always;

            PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(unicode, embedding);

            pdfRenderer.Document = document;

            pdfRenderer.RenderDocument();

            MemoryStream stream = new MemoryStream();
            pdfRenderer.PdfDocument.Save(stream, false);
            Response.Clear();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-length", stream.Length.ToString());
            Response.BinaryWrite(stream.ToArray());
            Response.Flush();
            stream.Close();
            Response.End();

        }

        private Document CreateDocument()
        {
            Document document = new Document();

            MigraDoc.DocumentObjectModel.Unit width, height;

            PageSetup.GetPageSize(PageFormat.A6, out width, out height);


            Section section = document.AddSection();
            section.PageSetup.PageHeight = height;
            section.PageSetup.PageWidth = width;
           


            Paragraph paragraph = section.AddParagraph();

            paragraph.Format.Font.Color = MigraDoc.DocumentObjectModel.Colors.Black; //Same as System.Drawing.Color

            paragraph.AddFormattedText("Hello, World!", TextFormat.Bold);

            return document;
        }


Thanks in advance!

Jimmy


Top
 Profile  
Reply with quote  
PostPosted: Wed Aug 10, 2016 9:36 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
Hi!
jimmyl wrote:
Does anyone have any idea how to make it so the receipt is a reasonable length?
Currently you set a page size of 105 mm × 148 mm.
Open the PDF with Adobe Reader and you can check that the page size is correct.

DIN A7 is 74 mm × 105 mm and would be closer to the 80 mm width you mention in your post.
You can set custom sizes as needed, e.g. width 80 mm and height 50 mm or less.

How do you print the PDF file?
With your A6 setting, the paper strip should be 128 mm or 113 mm or so, depending on the scaling applied while printing.
How long is the paper strip you get?

Do not forget to adjust the page margins. Margins of 25 mm (I think that's the default) are oversized for 80 mm paper width.
But that will be visible from the PDF file before printing.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Aug 10, 2016 4:51 pm 
Offline

Joined: Tue Aug 09, 2016 10:55 pm
Posts: 3
I did check by saving the PDF and it all does seem fine (after implementing your suggestions for a custom size and tweaking/elimination of the margins). I used the values of 80mm and 50mm and when I check the saved PDF I do get the correct values in inches. (However, during the running of the actual application I cannot save the PDF to disk.) When I print it, I don't know how long it is, because I stop the printer long before it finishes (once I see that it is longer than I intend it to be). However, when I tried it just now it was much longer than 50mm before I stopped it, and "Hello World" hadn't even been printed yet. Here is the new code I changed (same CreateDocument() function otherwise):

Code:
private Document CreateDocument()
        {
            Document document = new Document();

            MigraDoc.DocumentObjectModel.Unit width, height;

            width = MigraDoc.DocumentObjectModel.Unit.FromMillimeter(80);
            height = MigraDoc.DocumentObjectModel.Unit.FromMillimeter(50);


            Section section = document.AddSection();
            section.PageSetup.PageHeight = height;
            section.PageSetup.PageWidth = width;
            section.PageSetup.PageHeight = height;
            section.PageSetup.PageWidth = width;
            section.PageSetup.LeftMargin = 0;
            section.PageSetup.RightMargin = 0;
            section.PageSetup.TopMargin = 0;

           
            Paragraph paragraph = section.AddParagraph();

            paragraph.Format.Font.Color = MigraDoc.DocumentObjectModel.Colors.Black; //Same as System.Drawing.Color

            paragraph.AddFormattedText("Hello, World!", TextFormat.Bold);

            return document;
        }


I print the PDF file from this code. This causes it to be displayed as a preview, then the user needs to click the print icon, then they need to accept the proper printer. I must admit I find how this works to be annoying (considering that the user needs to lick a print button first, this means they need to click 3 times just to print anything), but at this point I am just trying to get anything printed.

Code:
            MemoryStream stream = new MemoryStream();
            pdfRenderer.PdfDocument.Save(stream, false);

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


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 11, 2016 8:39 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
jimmyl wrote:
I print the PDF file from this code.
That code does not print, that code sends the PDF to the browser. I think IE uses Adobe Reader while Chrome and Firefox have their own PDF viewers.

Does printing work with Adobe Reader? Directly or from an IE window?
Does printing work with Chrome?
Does printing work with Firefox?
Does printing work with Edge under Windows 10?

PDF is "portable" and all viewers should give the same printout. But Adobe Reader has several scaling options that will lead to different printouts. I do not use those other viewers for printing and have no experience with their settings.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 11, 2016 4:03 pm 
Offline

Joined: Tue Aug 09, 2016 10:55 pm
Posts: 3
Quote:
Does printing work with Adobe Reader? Directly or from an IE window?

No, I get the super long receipts regardless of whether I print from IE's Adobe Reader or in my Adobe Reader.

I am not particularly concerned about the other browsers as this application is only intended to run in IE. (I know that is considered poor practice, but we are developing this application for specific people, and so we can have such a constraint, and it comes as a direct order from my boss anyway). It is an interesting idea to try it out to see what happens. Once I have done that I will report here what happened. Edit: Google Chrome did the same thing. My work computer does not have Firefox, and it does not seem like it is easy to test it on Microsoft Edge in Visual Studio 2013. Note that I am using Windows 10 regardless.

In any case, it doesn't bother me if we have to change my code for the PDF to print on this thermal printer properly. Do you know of a way to get it to work?


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

All times are UTC


Who is online

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