I have been working on getting a PDF Viewer working in Net MAUI. In Xamarin, I displayed a PDF in Webview. Not a problem. In MAUI, you don't do that. I installed SpirePDF(free) and have it loading the PDF from file. When adding it to VerticalStackLayout (which will be the Content for a ScrollView), it fails on conversion to IView. Any ideas or suggestions?
VerticalStackLayout vsl = new VerticalStackLayout(); PdfDocument viewPdf = new PdfDocument(); Assembly.GetExecutingAssembly().GetType().GetTypeInfo().Assembly.GetManifestResourceStream("CommanderGrady.Resources.Images.adventure218.pdf"); viewPdf.LoadFromFile(#"C:\Users\leuol\source\repos\CommanderGrady\CommanderGrady\Resources\Images\adventure218.pdf"); vsl.Add((IView)viewPdf); return vsl; }
I have done a sample and the pdf can show in the browser. So you can try the following code.
string fullpath = xxx; // provide the file path
await Launcher.OpenAsync(new OpenFileRequest
{
File = new ReadOnlyFile(fullpath)
});
Related
I'm trying to use IronOCR to create text-searchable versions of scanned PDF documents. The outputted file is displayed properly (and is text-selectable) in pretty much every viewer, except for Chrome's built-in PDF viewer.
Here's my code for converting the files:
byte[] origPdfBytes = Properties.Resources.Non_text_searchable;
using (MemoryStream pdfStream = new MemoryStream(origPdfBytes))
{
var ocr = new IronTesseract();
using (OcrInput input = new OcrInput())
{
input.AddPdf(pdfStream);
OcrResult ocrResult = ocr.Read(input);
ocrResult.SaveAsSearchablePdf("C:\\temp\\OCRTest\\output.pdf");
}
}
Here is a sample file that I've converted using IronOCR: https://drive.google.com/file/d/1_uhmZKJN_TFStApfeeieI8LezAPjp1mj/view
If you download and view this file in pretty much any viewer other than Chrome, the text is properly selectable. However, in Chrome, the cursor does appear to be selecting text, but it does not display properly.
I've Chrome's built-in PDF viewer for years, and I've never run into an issue like this. I'm not sure if this is an issue with IronOCR's output formatting, or if it's just a problem with Chrome. Any ideas?
I'm trying to export multiple Google Docs files via Google Drive API into Pdf and merge them into one using iText7 but it throws exception iText.IO.Exceptions.IOException: 'PDF header not found.' because of the weird PDF format from Google export.
Google Disk generated PDF content (read with notepad) is not valid PDF.
File content starts like this 倥䙄ㄭ㐮┊ㄊ instead of something like %PDF-1.4
The uploaded PDF file is readable from Google Disk without any problem and it is readable even if I export the Stream directly to the disk. File content is exactly the same when I download file manually through Google Docs GUI.
Here is my code to export files via API:
var mimeType = "application/pdf";
var file = GetFile(sourceFile);
var pdfRequest = _driveService.Files.Export(sourceFile, mimeType);
var stream = pdfRequest.ExecuteAsStream();
Then I'm uploading PDF back into Google Drive via it's API
var newFile = new Google.Apis.Drive.v3.Data.File();
newFile.MimeType = mimeType;
newFile.Parents = new List<string>() { targetFolder };
var createRequest = _driveService.Files.Create(newFile, stream, mimeType);
createRequest.SupportsAllDrives = true;
var createResult = createRequest.Upload();
Weirdly enough the format of exported PDF is ok when I use
var text = pdfRequest.Execute(); instead of pdfRequest.ExecuteAsStream (it starts with %PDF-1.7).
But Execute() returns string instead of Stream.
Is there any way to get standard PDF format from Google Disk API or convert it in any possible way?
The problem was in the iText7 itself. It considered PDF as invalid but it probably just does not support PDFs in iso8859_2 encoding.
I tried to use PDFSharp instead and everything went smoothly.
I've used ExecuteAsStream() from Google Disk API to get PDF Stream with no problems at all so it wasnt at fault.
Thanks for all your tips.
I am migrating an application from asp.net MVC to Aspnet Core v2.1.
I have the following code to generate a Word Document and when I hover over the document I can see the properties such as Paragraphs, Images, Footer etc all set; but when I save I always get a 5Kb document with no content. It should return the document as a stream to download by the browser, but I have also included an explicit .SaveAs("C:\blah") which produces same file.
var fileStream = new MemoryStream();
DocX document = DocX.Create(fileStream, DocumentTypes.Document);
Paragraph p1 = document.InsertParagraph();
p1.Append("Hello world");
document.SaveAs(fileStream);
fileStream.Position = 0;
fileStream.Seek(0, SeekOrigin.Begin);
document.SaveAs("C:\\Code\\MyFile.docx");
var fsr = new FileStreamResult(fileStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
fsr.FileDownloadName = "MyFile.docx";
return fsr;
I don't get any errors either in the application or in my Event Viewer.
Any ideas?
Looks like the DocX library by Xceed is not yet supporting .Net Core. DocX is not supported in .net core applications.
You can try using EPPlus instead.
I'm new to stack overflow, C# and onenote interop com api. I'm trying to display a pdf file in onenote using C# and the onenote com/interop api (I'd rather not use the REST API).
I am able to display a link to a pdf file using the tag < InsertedFile pathSource="[myfilepath]" preferredName = "[myPreferredName]"> in conjunction with the UpdatePageContent function in the interop API, but this doesn't display the PDF.
I have been able to get my program to display an image in onenote using the following code to create the image tag
private XElement createImageTag(Image image)
{
string OneNoteNamespace = "http://schemas.microsoft.com/office/onenote/2013/onenote";
var img = new XElement(XName.Get("Image", OneNoteNamespace));
var data = new XElement(XName.Get("Data", OneNoteNamespace));
data.Value = this.toBase64(image);
img.Add(data);
return img;
}
private string toBase64(Image image)
{
using (var memoryStream = new MemoryStream())
{
image.Save(memoryStream, ImageFormat.Png);
var binary = memoryStream.ToArray();
return Convert.ToBase64String(binary);
}
}
I tried altering this for a pdf instead of am image by converting a pdf to a byte array then converting it to base64 and assigning the result as data.Value in the createImageTag function but it did not result in a displayed pdf either (presumably because onenote was expecting an image and not a pdf). I'd like to avoid using third party libraries or extensions to convert a pdf to an image if possible, and haven't found any other ways to convert a pdf to an image.
I used ONOMSpy to look for any other onenote/xml tags that might help me display a pdf in onenote, but didn't see others besides the Image and InsertedFile tags that looked like they were close to doing what I wanted.
so if you could help me either :
1) find an easy way to convert a pdf to an image using C# or
2) show me how to tell onenote to display the PDF
I'd really appreciate it. Thanks!
I'm having a go with document viewer and XPS atm as I haven't tried it before. So I have a simple piece of code loading an XPS document and displaying it in the document viewer, however the document doesn't appear. The document viewer loads and a quick step through in debug mode tells me the information is there,it just won't show.
dvDoc = new DocumentViewer();
string fileName = null;
string appPath = System.IO.Path.GetDirectoryName(Assembly.GetAssembly(typeof(DocumentWindow)).CodeBase);
if (type == "About")
fileName = appPath + #"\Documents\About.xps";
fileName = fileName.Remove(0, 6);
XpsDocument doc = new XpsDocument(fileName, FileAccess.Read);
dvDoc.Document = doc.GetFixedDocumentSequence();
All literature I can find tells me to do it this way yet it doesn't seem to work for me. I'm aware that document viewer doesn't like URI's, hence the filename.remove line.
Any suggestions as to what I'm missing.
Cheers,
SumGuy
You've probably already figured this out by now since it's been almost a month.
It doesn't look like your document viewer is part of your xaml file. It looks like you are creating a new DocumentViewer object, but never adding it to the xaml file.
Instead of
dvDoc = new DocumentViewer();
Declare it in your xaml file:
<DocumentViewer Name="dvDoc" />