Attaching PDF without saving document using SyncFusion C#.net - c#

Can someone help me or give me a link on how to attach a PDF to email using syncfusion html to pdf converter without saving the document?
here is my code:
//Convert URL to PDF
PdfDocument doc = htmlConverter.Convert(render_to_html(Panel_preview_attachment), baseUrl);
doc.Save(path + "Attachments\\" + file_name); //I need to remove this
string empPdf = path + "Attachments\\" + file_name;
LinkedResource linkedresource = new LinkedResource(empPdf, "application/pdf");
linkedresource.ContentId = "empPdf";
linkedresource.ContentType.Name = file_name;
htmlView.LinkedResources.Add(linkedresource);

We can convert HTML to PDF and send the PDF document in a mail without saving it to a filesystem. We can save the PDF document to the memory stream and we can send it to a mail. Please refer below link for more information,
HTML to PDF: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/webkit
Email output PDF: https://www.syncfusion.com/kb/6064/how-to-create-pdf-dynamically-and-email-as-attachment
Note : I work for Syncfusion.

Related

SelectPDF ConvertUrl Non-English Characters Error

In my demo project I'm using Selectpdf tool to convert html pages to pdf documents. These html pages are stored locally. So I'm using ConvertUrl function for conversion. Here is the inline code
`
string url = AppDomain.CurrentDomain.BaseDirectory + "HTML" + "\\OrderName_" + DateTime.Now.ToString("yyyy'-'MM'-'dd'_'HH'-'mm_") + MockOrderNo + ".html";
HtmlToPdf converter = new HtmlToPdf();
PdfDocument doc = converter.ConvertUrl(htmlurl);
`
Then I save the pdf document, using doc.Save(). Here is the pdf document result
Now as you can see there is a problem displaying Turkish characters like "İ,ı,ş,ğ...". How can I resolve this using SelectPdf? If solving this with SelectPdf is not possible, what are the other prefable pdf conversion tools that does not have this kind of problem?
Also for my requirements I don't use ConvertHtmlString function. I need to store html pages in a folder, convert these html pages to pdf and store those pdf documents in an another folder.
Thanks for your help
I just changed the encoding of html file to windows-1252. This solved the problem

Google Drive API / GUI exports invalid PDF - not readable by iText7

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.

Open the pdf without saving it ,during url to pdf conversion using ironpdf

I am using ironpdf for converting url to pdf. Conversion is successful, but I have to save the document at a location. Is it possible to just display as a memory stream or byte array?
string authority = HttpContext.Current.Request.Url.Authority;
string val = "http://"+authority + "/Report/PrintReport.aspx?ID=" + vciInfoReportid;
string fileName = "url.pdf";
string fileloc = HttpContext.Current.Server.MapPath("~/"+fileName);
Renderer.RenderUrlAsPdf(val).SaveAs(fileloc);
I found the solution. There is a inbuilt method for it
//Conversion of the ASPX file into PDF
MemoryStream rend=Renderer.RenderUrlAsPdf(val).Stream;
HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");
// let the browser know how to open the PDF document, attachment or inline, and the file name
HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("{0}; filename=Print.pdf; size={1}",
"attachment", rend.Length.ToString()));
// write the PDF buffer to HTTP response
HttpContext.Current.Response.BinaryWrite(rend.ToArray());
// call End() method of HTTP response to stop ASP.NET page processing
HttpContext.Current.Response.End();
More information can be found in this ASPX to PDF Tutorial
Try Google Docs :
Open your pdf in google viewer without download.
http://docs.google.com/viewer?url=http://"+authority + "/Report/PrintReport.aspx?ID=" + vciInfoReportid

Attach PDF document to Email C# Windows Application - Email does not send out

I am trying to attach PDF document to MailMessage Send method in Windows Form Application. I am using below code to attach document to email.
mail.Attachments.Add(new Attachment(#"c:\Files\churchapplication.pdf"));
Email is not generated and no exception is raised either so i am unable to troubleshoot the problem. Same code works if I attach text file instead of pdf. So what could be the issue? Any ideas? PDF file size 291 KB. Is there some restrictions with attaching PDF documents in .NET?
Thanks.
You can try this multi-step approach.
//First create FileContent
FileContentResult fileContent = File(fileName, "application/pdf", "file.pdf");
MemoryStream ms = new MemoryStream(fileContent.FileContents);
// Create an in-memory System.IO.Stream
ContentType ct = new ContentType(fileContent.ContentType);
Attachment a = new Attachment(ms, ct);
sender.SendMail("email", "email", "subject", "Body", a);

Cannot read text from pdf by ITextSharp in C#

I have two pdf files. On Sercurity tab both files have set Security Method: No Security and Document Assembly: Not Allowed and page Extraction: Not Allowed. Other items are allowed.
I using standart ITextSharp method to retrieve text from pdf:
PdfReader pdfReader = new PdfReader(fileName);
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy(); //LocationTextExtractionStrategy();
string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
text.Append(currentText);
From first file i can get currentText wihtout any problem from second file I cannot retrieve text, currentText is empty. I was trying with LocationTextExtractionStrategy, but result is the same. I opened this file in SodaPDF and convert it to txt file but this file is empty too (while frist file is converted to txt without any problems).
It is possible to read text from this file from C# or with any other application? If I buy Adobe Reader I will convert this file to txt ?
What is difference between these two files ?
Thanks
There may be a lot of pdf which actually are images. You cannot extracttext from imaged pdf as Bruno Lowagie said. you need to go for third party OCR for this.
you ca use Adobe Acrobat to convert the pdf to editable format like word, html..
I work as Social Media Developer at Aspose. I would suggest you to download and try Aspose.Pdf for .NET to convert PDF to Text file. In case your file contains images and you need to extract the text from those images, you can use Aspose.Pdf to convert Pdf file to images and then perform OCR using Aspose.OCR for .NET.
Following is the sample code to convert PDf to Text using Aspose.Pdf for .NET
//open document
Document pdfDocument = new Document("input.pdf");
//create TextAbsorber object to extract text
TextAbsorber textAbsorber = new TextAbsorber();
//accept the absorber for all the pages
pdfDocument.Pages.Accept(textAbsorber);
//get the extracted text
string extractedText = textAbsorber.Text;
// create a writer and open the file
TextWriter tw = new StreamWriter("extracted-text.txt");
// write a line of text to the file
tw.WriteLine(extractedText);
// close the stream
tw.Close();
Please download a free trial and try it.

Categories

Resources