Need help with Pdf Creation

RaphaelSchindlerRaphaelSchindler USMember ✭✭✭

Hi,
I try to create a Pdf file with itextsharp and save it onto the device. I made an example programm that saved it to my pc and it worked, but when i try to save this to my android device i get "Win32 IO returned 25" exception. What am i doing wrong?

           //Calculated items for pdf
           var itemTuple = new List<Tuple<int, double>>();
            int tempAge = calc.Age;
            for (int i = 0; i < calculatedItems.Length; i++)
            {
                  itemTuple.Add(new Tuple<int, double>(tempAge, calculatedItems[i]));
                  tempAge++;
            }

            var path = ae.GetExternalStoragePublicDirectory(ae.DirectoryDownloads).Path;

            var document = new Document();

            PdfWriter.GetInstance(document, new FileStream(path + "/Vermoegensliste" +  DateTime.Now.ToString() + ".pdf", FileMode.Create));

            BaseFont bfTimes = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, false);

            Font red = new Font(bfTimes, 12, Font.NORMAL, BaseColor.RED);
            Font green = new Font(bfTimes, 12, Font.NORMAL, BaseColor.GREEN);
            Font headFont = new Font(bfTimes, 20, Font.BOLD, BaseColor.BLACK);

            document.Open();

            Paragraph header = new Paragraph("Ausgerechnete Werte: ", headFont);
            PdfPTable table = new PdfPTable(6);
            PdfPCell headerCell = new PdfPCell(new Phrase(header));
            headerCell.Colspan = 6;
            headerCell.HorizontalAlignment = 1;

            float[] colWidths = { 30, 65, 30, 65, 30, 65 };
            table.SetWidths(colWidths);
            table.WidthPercentage = 100.0f;

            table.AddCell(headerCell);

            foreach (var item in itemTuple)
            {
                table.AddCell(new PdfPCell(new Phrase("Alter: " + item.Item1.ToString() + " Jahre.")));
                if (item.Item2.ToString().Contains('-'))
                {
                    table.AddCell(new PdfPCell(new Phrase("Vermögen: " + item.Item2.ToString() + "€", red)));
                }
                else
                {
                    table.AddCell(new PdfPCell(new Phrase("Vermögen: " + item.Item2.ToString() + "€", green)));
                }
            }

            document.Add(table);

            document.Close();

            Toast.MakeText(this, "Pdf wurde erstellt", ToastLength.Short);

Posts

  • PeterDavisPeterDavis USMember ✭✭✭
    edited May 2014

    Where are you getting the exception? What's the value of "path"?

  • RaphaelSchindlerRaphaelSchindler USMember ✭✭✭
    edited May 2014

    At this codeline
    PdfWriter.GetInstance(document, new FileStream(path + "/Vermoegensliste" + DateTime.Now.ToString() + ".pdf", FileMode.Create));

    I should say that i have added the WRITE_EXTERNAL_STORAGE permission in the manifest.

  • PeterDavisPeterDavis USMember ✭✭✭

    What's the value of "path"?

  • PeterDavisPeterDavis USMember ✭✭✭

    Also, try replacing your FileStream with a MemoryStream and generate the PDF file in the MemoryStream and then write that out to a file.

  • RaphaelSchindlerRaphaelSchindler USMember ✭✭✭

    /storage/sdcard0/Download
    That's where i want it to go.

  • RaphaelSchindlerRaphaelSchindler USMember ✭✭✭
    edited May 2014

    I can't convert the iTextSharp.text.Document to something the Memorystream could work with. Does the framework somewhere has a familiar method like the System.IO.FileStream i am using here? I couldn't find one.

  • PeterDavisPeterDavis USMember ✭✭✭
    edited May 2014

    I'm confused. Why can't you do something like this?

    using (MemoryStream ms = new MemoryStream())
    {
        PdfWriter.GetInstance(document, ms);
        ...
        ...
        document.Close();
        File.WriteAllBytes(myFilePath, ms.ToArray());
    }
    
  • RaphaelSchindlerRaphaelSchindler USMember ✭✭✭

    I changed the code to this:
    ` var path = ae.GetExternalStoragePublicDirectory(ae.DirectoryDownloads).Path;
    var output = path + "/Vermoegensliste" + DateTime.Now.ToString() + ".pdf";

                using (MemoryStream stream = new MemoryStream())
                {
                    var document = new Document();
                    PdfWriter.GetInstance(document, stream);
    
                    BaseFont bfTimes = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, false);
    
                    Font red = new Font(bfTimes, 12, Font.NORMAL, BaseColor.RED);
                    Font green = new Font(bfTimes, 12, Font.NORMAL, BaseColor.GREEN);
                    Font headFont = new Font(bfTimes, 20, Font.BOLD, BaseColor.BLACK);
    
                    document.Open();
    
                    Paragraph header = new Paragraph("Ausgerechnete Werte: ", headFont);
                    PdfPTable table = new PdfPTable(6);
                    PdfPCell headerCell = new PdfPCell(new Phrase(header));
                    headerCell.Colspan = 6;
                    headerCell.HorizontalAlignment = 1;
    
                    float[] colWidths = { 30, 65, 30, 65, 30, 65 };
                    table.SetWidths(colWidths);
                    table.WidthPercentage = 100.0f;
    
                    table.AddCell(headerCell);
    
                    foreach (var item in itemTuple)
                    {
                        table.AddCell(new PdfPCell(new Phrase("Alter: " + item.Item1.ToString() + " Jahre.")));
                        if (item.Item2.ToString().Contains('-'))
                        {
                            table.AddCell(new PdfPCell(new Phrase("Vermögen: " + item.Item2.ToString() + "€", red)));
                        }
                        else
                        {
                            table.AddCell(new PdfPCell(new Phrase("Vermögen: " + item.Item2.ToString() + "€", green)));
                        }
                    }
    
                    document.Add(table);
    
                    document.Close();
    
                    File.WriteAllBytes(output, stream.ToArray());
                }`
    

    I still get the same exception :(

  • PeterDavisPeterDavis USMember ✭✭✭

    Except now you're either getting it in the PdfWriter.GetInstance(document, stream); call (meaning you have a itextSharp issue) or you're having it in File.WriteAllBytes(output, stream.ToArray()); (meaning that your path is probably invalid).

    So which is it?

    See how we just took the first step to diagnosing your problem? We broke it down to find the source.

  • PeterDavisPeterDavis USMember ✭✭✭
    edited May 2014

    Doh! I see what the problem is: DateTime.Now.ToString()

    Vermoegensliste5/30/2014 9:13:49 AM.pdf is not a valid filename.

  • RaphaelSchindlerRaphaelSchindler USMember ✭✭✭
    edited May 2014

    What really? Ohhhhhhhhh, man, oh wow I'm so stupid^^ I thought because the filename actually is a string i could do this. I've done that before O.o Thank you man^^ I would have searched serveral more hours :D That's the first thing I'm gonna change on monday, then i will update you.

  • PeterDavisPeterDavis USMember ✭✭✭

    You wouldn't be able to use DateTime.ToString() (without parameters) in a filename on any operating system that I'm aware of. I don't know of any cultures that are compatible with file systems. Either the date is going to have a / (most cultures) or the time is going to have a colon (all cultures, I think), or both and I don't think any file systems support colons in filenames (colon is the only character that's now allowed in Mac filenames).

    But you can do something like DateTime.Now.ToString("yyyyMMddTHHmmss")

  • RaphaelSchindlerRaphaelSchindler USMember ✭✭✭

    Yes you were completely right. It was the DateTime.ToString() without parameters. I looked at a previous project of mine where i also had to export pdfs and i had parameters there. I knew i did this before but i forgot that i need parameters... This is akward because it's basic stuff. Thank you again :D

  • if you have a sample working example of this, could you send it to me.

  • BatzeeBatzee LKMember ✭✭

    I was trying to use iTextSharp but I am getting this, Error
    Exception while loading assemblies: System.IO.FileNotFoundException: Could not load assembly 'System.Drawing'

    Can you explain how you overcame this issue? Thanks

  • Diorrini11Diorrini11 USMember

    @Batzee said:
    I was trying to use iTextSharp but I am getting this, Error
    Exception while loading assemblies: System.IO.FileNotFoundException: Could not load assembly 'System.Drawing'

    Can you explain how you overcame this issue? Thanks

    Just add the System.Drawing Reference.

Sign In or Register to comment.