Forum Xamarin.Forms

Convert docX to PDF free

jwartofskyjwartofsky Member ✭✭
edited February 2018 in Xamarin.Forms

I'm trying to figure out how to convert a docX file that was downloaded as a byte stream and written to a file into a PDF so that I can open it using the PDFViewPage. I'd rather not pay for any packages. I tried Sautinsoft pdfmetamorphosis, but couldn't get that to work. All of their examples were for various Windows versions, and when attempting to compile I would get this error: System.IO.FileNotFoundException: Could not load assembly 'PdfMetamorphosis, Version=6.8.4.10, Culture=neutral, PublicKeyToken=f77041e5caa5fc2f'. Perhaps it doesn't exist in the Mono for Android profile? File name: 'PdfMetamorphosis.dll' Is this package meant for mobile?

Here's my code:

    async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        Publication p = (Publication)e.SelectedItem;
        Debug.WriteLine(p);
        if (p.folderID.Equals("-1"))
        {
            using (Stream respStream = await post(p.docNum))
            {
                string ext = p.appextension.ToLower();
                byte[] buffer = new byte[respStream.Length];
                respStream.Read(buffer, 0, buffer.Length);
                string path = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "/downloadedFile." + ext;
                File.WriteAllBytes(path, buffer);
                switch (ext)
                {
                    case "pdf":
                        await Navigation.PushAsync(new PDFViewPage(path));
                        break;
                    case "docx":

                        //Convert docX at "path" to PDF here, then call "await Navigation.PushAsync(new PDFViewPage(path));"

                        //SautinSoft.PdfMetamorphosis converter = new SautinSoft.PdfMetamorphosis();
                        //string pdfPath = Path.ChangeExtension(path, ".pdf");
                        //converter.DocxToPdfConvertFile(path, pdfPath);
                        //await Navigation.PushAsync(new PDFViewPage(pdfPath));

                        await DisplayAlert("Error", ".docx files are not supported at this time", "Return");
                        break;
                    default:
                        Debug.WriteLine("wasn't pdf");
                        Debug.WriteLine("was a ." + ext);
                        break;
                }


            }
        }
        else
        {
            await Navigation.PushAsync(new PublicationsPage(p.folderID));
        }
    }

    private async Task<Stream> post(string id)
    {
        Dictionary<string, string> dir = new Dictionary<string, string>();
        dir.Add("LoginID", App.user.login_id);
        dir.Add("docID", id);
        var jsonReq = JsonConvert.SerializeObject(dir);
        Debug.WriteLine("req: " + (String)jsonReq);
        var content = new StringContent(jsonReq, Encoding.UTF8, "application/json");
        var response = await client.PostAsync(url, content);
        var responseString = await response.Content.ReadAsStreamAsync();
        return responseString;
    }
}

Best Answer

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I tried Sautinsoft pdfmetamorphosis,but couldn't get that to work.

    That's pretty vague. Did you get exceptions? The nuget didn't install? You just didn't understand it?

  • jwartofskyjwartofsky Member ✭✭

    @ClintStLaurent said:

    I tried Sautinsoft pdfmetamorphosis,but couldn't get that to work.

    That's pretty vague. Did you get exceptions? The nuget didn't install? You just didn't understand it?

    Updated the initial post

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited February 2018

    After a quick google this one says its free
    https://github.com/xceedsoftware/DocX

    UPDATE: Oops. They have a free version - but it doesn't do the word->PDF conversion
    But since you want DocX not word Doc - it might work.

  • jwartofskyjwartofsky Member ✭✭

    @ClintStLaurent said:
    After a quick google this one says its free
    https://github.com/xceedsoftware/DocX

    Tried that one too. When I try to compile I get this:

    Severity Code Description Project File Line Suppression State
    Error NU1202 Package DocX 1.1.0 is not compatible with xamarinios10 (Xamarin.iOS,Version=v1.0) / win. Package DocX 1.1.0 supports: net (.NETFramework,Version=v0.0)

    Severity Code Description Project File Line Suppression State
    Error NU1202 Package DocX 1.1.0 is not compatible with monoandroid80 (MonoAndroid,Version=v8.0). Package DocX 1.1.0 supports: net (.NETFramework,Version=v0.0)

    Is .NetFramework v0.0 even a thing?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Here's a shot of me trying to add it.

    It doesn't seem to like UWP, but otherwise works for Android and iOS

    My test app compiled and deployed fine with this in.

    Can you give it a go without UWP support?

  • jwartofskyjwartofsky Member ✭✭

    I don't have UWP enabled. How do I get it to display all of those options? Here's what mine looks like:

  • GiampaoloGabbaGiampaoloGabba USMember ✭✭✭

    You can try with the syncfusion controls: https://help.syncfusion.com/file-formats/pdf/working-with-document-conversions
    I already used their EssentialPDF component in a xamarin forms project and works very well.

    They offer a community license (free) for individual developers and small businesses.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @jwartofsky said:
    I don't have UWP enabled. How do I get it to display all of those options? Here's what mine looks like:

    A buddy had to show me how you were getting that display because I've never seen it before.

    What you want to do is...
    In Solution Manager right-click at the top level of the solution - the solution itself, not a sub project within the solution.
    The pick "Manage NuGet packages for solution"

  • jwartofskyjwartofsky Member ✭✭
    edited February 2018

    Tried it with opening that window, it's basically the same thing. I don't have UWP enabled, but I'm still getting the same errors about it not being compatible with Xamarin.iOS. Tested as well on a new blank app and got the same thing. I'm gonna try Syncfusion and see if that works better.

    Update: Trying SyncFusion on a new, blank, Xamarin.Forms project, getting same kind of error again. Anyone know what's wrong? Here's a screenshot:

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Hmmm.... You can try jumping to another package... but... If you are now saying several packages are throwing these errors... one of which someone else can install and compile... It makes me wonder if there is an issue with your Visual Studio installation or the solution itself. Its just hard to believe that all of these packages you've tried are broken.

  • jwartofskyjwartofsky Member ✭✭
    edited February 2018

    I agree. Tried it on new blank Xamarin.Forms apps and get the same error. I've looked up the error and couldn't find anything. Any idea on how to fix? Is there some setting I need to change?

  • Hi @jwartofsky ,

    It seems that you are referring incorrect NuGet packages to perform Word to PDF conversion in Xamarin platforms. Please refer this article to know about Word to PDF conversion in Xamarin platforms.

    Note: I work for Syncfusion.

    P.S: There is a similar discussion going on here by you.

    Regards,
    Balasubramanian

  • SergeFloydSergeFloyd Member ✭✭

    Do you need to generate docX to PDF ??

    please to check ZetPDF.com library

Sign In or Register to comment.