Issue to PDF file

Peter_1985 2,736 Reputation points
2020-10-23T07:28:47.507+00:00

Good day,
Is there one better example to create PDF file? I used code below and PDF file then is empty (cannot be opened).
PdfWriter pdfWriter = new PdfWriter("f:\l0\A0.pdf");
PdfDocument pdfD0 = new PdfDocument(pdfWriter);
Document D0 = new Document(pdfD0);

Developer technologies | Windows Forms
Developer technologies | Windows Presentation Foundation
{count} votes

9 additional answers

Sort by: Most helpful
  1. David 151 Reputation points
    2021-03-08T02:17:19.797+00:00

    Spire.PDF for .NET is a good alternative when it comes to creating or processing PDF document from C# code.

    https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/How-to-Draw-Text-in-PDF-with-Different-Styles.html

    0 comments No comments

  2. Mo Sharnouby 1 Reputation point
    2021-08-06T13:17:21.117+00:00

    Hi Jackson, this code will help you set the page size and add margins

    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    Rectangle one = new Rectangle(70,140);
    Rectangle two = new Rectangle(700,400);
    document.setPageSize(one);
    document.setMargins(2, 2, 2, 2);
    document.open();
    Paragraph p = new Paragraph("YourTextHere");
    document.add(p);
    document.setPageSize(two);
    document.setMargins(20, 20, 20, 20);
    document.newPage();
    document.add(p);
    document.close();
    

    I see you have more than one issue using iText7, what version are you using? and if it's not working properly you can try a different library, such as ironPDF or PDFFlow, I use PDFFlow and this simple code to create a PDF file with setting margins to 50 and the orientation to portrait:

       //Create a document builder:
       DocumentBuilder.New() 
       //Add a section and section content:
       .AddSection()
               .SetMargins(50)
               .SetOrientation(PageOrientation.Portrait)
               .AddParagraph("Lorem ipsum dolor sit amet")
       //Build a file:
       .ToDocument().Build("Result.pdf");
    

    if you need to add more fields, tables, images, or complicated PDF designs you can find a lot of examples here , or contact me, I will be happy to help you. Good luck!

    0 comments No comments

  3. Peter_1985 2,736 Reputation points
    2021-08-09T09:30:46.37+00:00

    Hi,
    Thanks a lot. Is IronPDF free to use? How to enable it in VS 2017?

    0 comments No comments

  4. Mo Sharnouby 1 Reputation point
    2021-09-20T17:28:29.08+00:00

    hi again Jackson1990-7147 , ironPDF is free for only 30 days trial and I didn't try I before,

    I use PDFFlow for almost a year now and it's completely free for non-commercial use, and you can find a lot of examples here, where you can download and do few edits to save time coding from scratch.

    this is how to enable it in VisualStudio:

    You need to add the Gehtsoft.PDFFlowlib library to your project. You can download it as a NuGet package from NuGet Gallery | Gehtsoft.PDFFlowLib.

    You can also install it from the Visual Studio NuGet Package Manager:

    1. In Solution Explorer, right-click either References or a project and select Manage NuGet Packages.
    2. Go to the Browse tab and search for the Gehtsoft.PDFFlowLib package using the search box on the upper left.
    3. Select the package from the list and click Install.

    now you are ready :) tell me if you need more help

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.