Search code examples
c#.netpdfpdf-generationpdflib

How to generate new PDF using PDFLib 9 in C#?


I am trying to create a new, blank PDF document using PDFLib 9 in my .Net project. I've looked at some of the tutorials and documentation but could not get it working.

Here is the code I have in a unit test:

        public void Test()
    {
        try
        {
            var outfile = "newPDF.pdf";
            const string docOption = "searchpath={C:\\Users\\me\\Desktop\\Test_Pdfs}";
            var p = new PDFlib();
            p.set_option(docOption);
            p.set_option("errorpolicy=return");

            var x = p.begin_document(outfile, "");
            if (x != -1)
            {
                p.begin_page_ext(595.0, 842.0, "topdown");
                p.end_page_ext("");
                p.end_document("");
            }
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.Message);
            throw;
        }
    }

I don't get any errors in the catch, and the test does not fail.

However, when I enable CLR exceptions I get a number of CommunicationAbortedException's and InvalidOperationExceptions that basically talk about how some connection was closed.

This all happens after the last bracket. Also, the PDF is simply not created.

Any insight is really appreciated!


Solution

  • looks like something wrong with searchpath. I commented out your docOption and PDF is created. this should give you some clue.