0

how to printpreview and print SharpPDFLabel in vb.net?

As per the link with the documentation below : https://github.com/finalcut/SharpPDFLabel/tree/master

but there is an error in the code below Response.AddHeader.

is there something wrong with my code?

Thanks

Private personCollection As List(Of personCollection)
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ' Create the required label
        Dim labelDefinition = New SharpPDFLabel.Labels.A4Labels.Avery.L5160()

        ' Create a CustomLabelCreator, passing the required label
        Dim customLabelCreator = New SharpPDFLabel.CustomLabelCreator(labelDefinition)

        '//Add content to the labels
        '    ... 
        '    some IEnumerable collection was created...
        '...

        For Each person In personCollection
            ' you can specify LEFT, RIGHT, or CENTER for the horizontal alignment during Label construction
            Dim label As New Label(Enums.Alignment.LEFT)
            label.AddText(person.fullname, "Verdana", 12, embedFont:=True)
            label.AddText(person.address1, "Verdana", 12, embedFont:=True)
            label.AddText(person.address2, "Verdana", 12, embedFont:=True)
            label.AddText(person.city & ", " & person.stateCode & " " & person.zipcode, "Verdana", 12, embedFont:=True)
            customLabelCreator.AddLabel(label)
        Next person
        'Create the PDF as a stream
        Dim pdfStream = customLabelCreator.CreatePDF()

        'Do something with it!
'I have an error in the code below
        Response.AddHeader("Content-Disposition", "attachment; filename=address_labels.pdf")
        Return New FileStreamResult(pdfStream, "application/pdf")


    End Sub
End Class
Public Class personCollection
    Public fullname As String
    Public address1 As String
    Public address2 As String
    Public city As String
    Public stateCode As String
    Public zipcode As String
End Class
roy
  • 693
  • 2
  • 11
  • 3
    *"there is an error"*. What error? Please spend some time in the Help Center to learn how to ask a question properly. – jmcilhinney Aug 09 '23 at 03:07
  • It looks like you have copied code that was written for ASP.NET into a WinForms app. What exactly do you think `Response` is in that context? – jmcilhinney Aug 09 '23 at 03:08
  • In that example, the contents of the stream is presumably being written to the result of a controller action in an ASP.NET app. That is obviously of no use to you in a WinForms app. It's up to you to decide what is useful to you in your context. You have a stream containing PDF data so you can do with it what you can do with any stream, e.g. write it to a file. – jmcilhinney Aug 09 '23 at 03:11
  • @jmcilhinney , `"there is an error". What error? Please spend some time in the Help Center to learn how to ask a question properly` `in this code ` Response.AddHeader("Content-Disposition", "attachment; filename=address_labels.pdf") Return New FileStreamResult(pdfStream, "application/pdf")` – roy Aug 09 '23 at 03:21
  • @jmcilhinney , `What exactly do you think Response is in that context?`I think those variables can be generated to PDF or print files – roy Aug 09 '23 at 03:23
  • @jmcilhinney , `You have a stream containing PDF data so you can do with it what you can do with any stream, e.g. write it to a file. ` for this please guide me – roy Aug 09 '23 at 03:24
  • For what? I told you that it is up to you to decide what you want to do with it. Have you decided? You've given us no indication that you have. If you have then the first step is to research how to do that for yourself. Only if you can't work out how to do it should you be asking us and then you need to explain what you have done and where you are stuck. – jmcilhinney Aug 09 '23 at 03:26
  • @jmcilhinney , `For what? I told you that it is up to you to decide what you want to do with it. Have you decided? You've given us no indication that you have. If you have then the first step is to research how to do that for yourself. Only if you can't work out how to do it should you be asking us and then you need to explain what you have done and where you are stuck.` I keep running in winform and my want is first of printpreview and print – roy Aug 09 '23 at 03:29
  • You're creating a pdf document. There really isn't a print preview, other than opening it in a pdf viewer such as adobe acrobat or some web browser. What you are asking to do here really makes no sense. Either way though, generating the file stream as you are now is a complete different set of problems from doing something with it afterwards – Hursey Aug 09 '23 at 03:37
  • There's nothing in WinForms specifically for printing PDFs, which you would know if you had done the research I directed you to. You could use a third-party control but then it's up to you to choose and research such a control. You could use a `WebView2` from Microsoft, which is basically an Edge browser control, so it could display a PDF and it may be able to print one too, but then you would need to research the `WebView2` for yourself too, as it's not a standard part of WinForms. Apart from that, you'd have to use some other app, as suggested above. – jmcilhinney Aug 09 '23 at 03:43
  • @jmcilhinney , You mean I have to use webview2 – roy Aug 09 '23 at 03:48
  • Is there a reason you've gone away from using standard printing? Assuming you never found a resolution to this [question](https://stackoverflow.com/questions/76849642/why-do-i-print-my-barcode-does-not-match-the-output-result-with-the-size-of-the) – Hursey Aug 09 '23 at 03:52
  • @Hursey , I have replied in other post I am again looking for a solution – roy Aug 09 '23 at 04:00
  • If I meant that you have to use `WebView2` then that's what I would have said. I meant what I actually said. That's why I said it. The read the words and then you'll know what I meant. – jmcilhinney Aug 09 '23 at 04:11
  • @jmcilhinney, please guide me for use `webwiew2` – roy Aug 09 '23 at 04:14
  • 1
    You. Need. To. Do. Your. Own. Research. First. You don't know that you can't do it yourself if you haven't even tried. That is the last I'll say on this matter until I see an effort from you. I know you don't want to have to do it for yourself because it would be easier to just let someone else do it for you but that's not what this site is about. If you didn't know about `WebView2` before then you couldn't research it. That's fine. Now you do, so now you can. – jmcilhinney Aug 09 '23 at 04:18
  • IMO, unless you are actually needing a PDF, forget this whole topic. You are just trying to find a solution here for a problem that you shouldn't have. Figure out how to manage printing properly and you don't need to worry about hacky solutions for printing, even then there are far better 3rd party tools for reporting. – Hursey Aug 09 '23 at 05:23

0 Answers0