none
Handle PDF file RRS feed

  • Question

  • Hi,
    In addition to this, is there any other details to the way to create PDF file using C Sharp?


    Many Thanks & Best Regards, Jackson Chen

    Friday, September 25, 2020 1:22 PM

Answers

  • NuGet is how all .NET apps get third party dependencies. It is baked into the build infrastructure and is used for just about every project you have. You cannot even build a web app without it. Other languages refer to this as a package manager.

    As I mentioned before, to use NuGet do the following.

    1. In solution explorer select the solution node

    2. Right click and select the option to Manage NuGet

    3. In the Package Manager dialog go to the Browse tab

    4. Search for the package you want (e.g. iText)

    5. It will list all the relevant packages. Find the one that is the official package.

    6. On the right side select the project or projects that need access to this functionality and then click the Install button.

    7. The package is downloaded and a reference added.

    8. You can now use the library directly in your code by following the instructions on the libraries page (generally just adding a using statement).

    Here's the official steps.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Jackson_1990 Monday, October 5, 2020 8:03 AM
    Wednesday, September 30, 2020 1:08 PM
    Moderator

All replies

  • .NET does not provide support for PDFs. To use PDFs you'll need to use a third party library. iTextSharp/iText7 is free (or you can get a commercial license) and very common. There are other libraries as well. Google for some PDF libraries for C# and then evaluate them for your specific application needs.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, September 25, 2020 6:06 PM
    Moderator
  • Apart from PdfSharp, you can have a check on Spire.PDF library, it provides both free and commercial editions.

    Create PDF:

    //Create a Pdf document
    PdfDocument document = new PdfDocument();
    
    //Set margins
    PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
    PdfMargins margins = new PdfMargins();
    margins.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
    margins.Bottom = margins.Top;
    margins.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
    margins.Right = margins.Left;
    
    //Add a page
    PdfPageBase page = document.Pages.Add(PdfPageSize.A4, margins);
    
    //Create a PdfTrueTypeFont
    PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 11f), true);
    PdfPen pen = new PdfPen(Color.Black);
    
    //Draw text to the page
    string text = "My first PDF document";
    page.Canvas.DrawString(text, font,pen,100,50);
    
    //Save the result document
    document.SaveToFile("CreatePDF.pdf");



    • Edited by Jamesbn Sunday, September 27, 2020 2:14 AM
    • Marked as answer by Jackson_1990 Sunday, September 27, 2020 2:38 AM
    • Unmarked as answer by Jackson_1990 Sunday, September 27, 2020 3:20 AM
    Sunday, September 27, 2020 2:06 AM
  • .NET does not provide support for PDFs. To use PDFs you'll need to use a third party library. iTextSharp/iText7 is free (or you can get a commercial license) and very common. There are other libraries as well. Google for some PDF libraries for C# and then evaluate them for your specific application needs.

    Michael Taylor http://www.michaeltaylorp3.net

    Hi CoolDadTx,
    I tried iText7. How to directly refer to way of that, in our VS 2015 project?


    Many Thanks & Best Regards, Jackson Chen

    Monday, September 28, 2020 3:58 AM
  • "How to directly refer to way of that, in our VS 2015 project?"

    I assume you are asking how to use the library in your code. iText7, like almost every other library you'll want to use, can be referenced using NuGet. In VS right click the project in Solution Explorer (or the solution if you want to use it in multiple projects). Select `Manage NuGet` (or equivalent in VS 2015). When the NuGet Package Manager appears go to the Browse tab and type in iText7. The official package should be one of the first ones but ensure it comes from iText. Then on the right side click the install button and the dependencies will be set up in your code. You can then refer to their documentation on getting started. 

    Note that the library is automatically downloaded to your project either during build or some time after the solution loads. This is handled by MSBuild automatically. However you're using a really old version of VS. In earlier versions (not sure which) you sometimes had to enable the restore on build option. Go to Tools \ Options in the menu and then look for the NuGet options. Ensure the options are set to download missing packages. This should be sufficient. Then rebuild.

    If for some reason you are still getting errors then you might need to enable project restore on each project. Again, it has been a long time since I've had to work with VS 2015 so I don't remember how integrated it was compared to current versions.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, September 28, 2020 1:20 PM
    Moderator
  • Thanks CoolDadTx.
    Is there any iText 7 DLL file, to be used, in VS 2015/2017?

    Many Thanks & Best Regards, Jackson Chen

    Tuesday, September 29, 2020 7:40 AM
  • On Windows 10, you can use  "Microsoft Print to PDF" virtual printer to generate .pdf files
    • Edited by Castorix31 Tuesday, September 29, 2020 8:24 AM
    Tuesday, September 29, 2020 8:22 AM
  • Hi Jackson,
    About this probelm, Justin has provide a good answer in this thread with iText and C# code example you can refer to.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 29, 2020 8:28 AM
  • Hi Daniel,

    In VS 2017, I cannot work properly vs this PDFSharp-Master project. Can you help?




    Many Thanks & Best Regards, Jackson Chen

    Tuesday, September 29, 2020 10:16 AM
  • The version of VS doesn't matter for NuGet packages. It has been supported for years. Are you having a particular issue with using NuGet inside of VS? If so then please post what issues you are having.

    There shouldn't be any problems. The only thing you may run into is that most NuGet packages require newer versions of the framework. If you're running a v2 app then you could run into issues but 4.5.1 or higher should be fine for everything else.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, September 29, 2020 1:12 PM
    Moderator
  • Hi,
    Sorry, what is NuGet command to enable the way (or do proper setup) to create PDF file?

    Many Thanks & Best Regards, Jackson Chen


    • Edited by Jackson_1990 Wednesday, September 30, 2020 7:25 AM
    Wednesday, September 30, 2020 7:16 AM

  • Hi Jackson_1990,
    About PDFSharp, you need to dowload PDFsharp Assemblies in first.
    Then add pdfsharp reference in C# Project via following steps:
    In Solution Explorer, right-click the project name ->Add-> Reference->Browser-> choose the pdfsharp assembly file->Select all files and click OK
    Finally, you can start programming to create a New PDF document.
    Here is a code example you can refer to.

     private void button1_Click(object sender, EventArgs e)
            {
                PdfDocument pdf = new PdfDocument();
                pdf.Info.Title = "My First PDF";
                PdfPage pdfPage = pdf.AddPage();
                XGraphics graph = XGraphics.FromPdfPage(pdfPage);
                XFont font = new XFont("Verdana", 20, XFontStyle.Bold);
                graph.DrawString("This is my first PDF document", font, XBrushes.Black, new XRect(0, 0, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.Center);
                string pdfFilename = "firstpage.pdf";
                pdf.Save(pdfFilename);
            }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 30, 2020 7:37 AM
  • Hi,
    Which file is PDFSharp assembly?

    Many Thanks & Best Regards, Jackson Chen

    Wednesday, September 30, 2020 8:05 AM
  • NuGet is how all .NET apps get third party dependencies. It is baked into the build infrastructure and is used for just about every project you have. You cannot even build a web app without it. Other languages refer to this as a package manager.

    As I mentioned before, to use NuGet do the following.

    1. In solution explorer select the solution node

    2. Right click and select the option to Manage NuGet

    3. In the Package Manager dialog go to the Browse tab

    4. Search for the package you want (e.g. iText)

    5. It will list all the relevant packages. Find the one that is the official package.

    6. On the right side select the project or projects that need access to this functionality and then click the Install button.

    7. The package is downloaded and a reference added.

    8. You can now use the library directly in your code by following the instructions on the libraries page (generally just adding a using statement).

    Here's the official steps.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Jackson_1990 Monday, October 5, 2020 8:03 AM
    Wednesday, September 30, 2020 1:08 PM
    Moderator
  • Hi,
    Can there be one clear help to resolve issue like

    The type or namespace name 'iTextSharp' could not be found (are you missing a using directive or an assembly reference?)



    due to this line?
    using iTextSharp.text;



    Many Thanks & Best Regards, Jackson Chen

    Friday, October 16, 2020 2:05 AM
  • The error actually clarifies what is wrong. You have added a using statement for iTextSharp and the compiler cannot find it because you are probably missing an assembly reference. 

    Did you follow the steps posted to include the assembly using NuGet? If you didn't then it won't work. If you did then verify that your project has a reference to the iTextSharp assembly under its References node in Solution Explorer. Note that you may need to build your code at least once before it'll show up.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, October 16, 2020 2:32 AM
    Moderator
  • Yes, I did relevant steps for that and you can see proper reference below.

    But I now have issue to the line below.
    using iTextSharp.text;



    Many Thanks & Best Regards, Jackson Chen

    Friday, October 16, 2020 2:48 AM
  • iTextSharp isn't a namespace in iText7. It seems like you're trying to use some code sample that uses iTextSharp, which is deprecated and no longer supported, with iText7.

    iText7 uses the `iText` namespace. If you're looking for the PDF functionality then you'll need to add a using for iText.Kernel.Pdf then you'll have access to the PDF types. But for more help with using this library you'll need to ask for help in their forums. When looking for code samples note that any that refer to iTextSharp will need to be converted to the newer iText7 framework instead.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, October 16, 2020 3:07 AM
    Moderator