C#/XAML winRT apps export PDF - c#

How can I generate a pdf in winRT apps? I'm using iTextSharp to generate pdfs in windows store apps, but winRT does not have filestream, filemode or filedirectory. help
Here is my code:
iTextSharp.text.pdf.PdfWriter writer =
iTextSharp.text.pdf.PdfWriter.GetInstance(
doc, new System.IO.FileStream(System.IO.Directory.GetCurrentDirectory() +
"\\ScienceReport.pdf", System.IO.FileMode.Create
)
);

I can generate a PDF file in winrt
string path = #"ExportPDF.pdf";
var storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
// Create empty PDF file.
var file = await storageFolder.CreateFileAsync(path, CreationCollisionOption.ReplaceExisting);
if (file != null)
{
await FileIO.WriteTextAsync(file, string.Empty);
}
// Open to PDF file for read/write.
StorageFile sampleFile = await storageFolder.GetFileAsync(path);
var stream = await sampleFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
// Create an instance of the document class which represents the PDF document itself.
Document document = new Document(PageSize.A4, 25, 25, 30, 30);
// Create an instance to the PDF file by creating an instance of the PDF
// Writer class using the document and the filestrem in the constructor.
PdfWriter writer = PdfWriter.GetInstance(document, stream.AsStream());
// Add meta information to the document
document.AddAuthor("Jigs");
document.AddCreator("Sample application");
document.AddKeywords("PDF App");
document.AddSubject("Document subject - Describing the steps creating a PDF document");
document.AddTitle("The document title - PDF creation");
// Open the document to enable you to write to the document
document.Open();
// Add a simple and wellknown phrase to the document in a flow layout manner
document.Add(new Paragraph("Hello!"));
// Close the document
document.Close();
// Close the writer instance
writer.Close();

Related

Generate PDF Xamarin.Forms NotImplementedException

I am trying to generate a PDF from a Xamarin.Forms application.
In the IOS simulator, I get System.NotImplementedException on document.Add function
Code:
PdfDocument pdf = new PdfDocument(new PdfWriter(filenamepdf));
Document document = new Document(pdf);
String line = "Hello! Welcome to iTextPdf";
document.Add(new Paragraph(line));
document.Close();
How should IText be used on Xamarin?
IText version: 7.1.13
You forgot to open the doc
PdfDocument pdf = new PdfDocument(new PdfWriter(filenamepdf));
Document document = new Document(pdf);
document.Open();
......
document.Close();

How to store Generated pdf on deployed website with Itext

I am trying to write to a pdf and send it in an email.I am able to implement this on my local machine. The problem is when I deploy to azure I am not sure where to store the pdf . I have seen one question regarding this
and tried this solution from stackoverflow -
Does iText (any version) work on Windows Azure websites?.
var path = Server.MapPath("test.pdf");
FileInfo dest = new FileInfo(path);
var writer = new PdfWriter(dest);
var pdf = new PdfDocument(writer);
var document = new Document(pdf);
document.Add(new Paragraph("hello world"));
document.Close();
I get an error
Could not find a part of the path
'D:\home\site\wwwroot\Email\test.pdf'.
Try to create the Pdf in memory and stream the content to the asp.net output stream.
Document document = new Document(PageSize.A4);
MemoryStream ms = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, ms);
document.Open();
document.NewPage();
...
...
document.Close();
Response.Clear();
Response.ContentType = "application/pdf";
byte[] pdfBytes = ms.ToArray();
Response.AppendHeader("Content-Length", pdfBytes.Length.ToString());
Response.OutputStream.Write(pdfBytes, 0, (int)pdfBytes.Length);
I suppose your issue is related with the file path.
If I use the path like Server.MapPath("Azure_Example1.pdf"), I also get the same error as you.
I suggest you could try to use the relative path like Server.MapPath("~/Azure_Example1.pdf"). The '~/' points to the project root directory.
You could also set a break point to check the value of path by using remote debugging.
I have created a simple demo, it works fine on my side. You could refer to.
Install the iTextSharp 5.5.13 nuget package in Manage Nuget Packages.
Use the following code:
var path = Server.MapPath("~/Azure_Example1.pdf");
FileInfo dest = new FileInfo(path);
FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();
doc.Add(new Paragraph("Hello World")); //change content in pdf
doc.Close();
Finally, you could see the pdf file has been stored in root project directory.

Delete PDF bookmarks using itextsharp

I use the itextsharp library to remove unwanted bookmarks from PDF files.
I developed the following code:
private static void RemovePDFbookmarks(string filein, string fileout)
{
PdfReader pdfReader = new PdfReader(filein);
Document document = new Document();
PdfCopy copy = new PdfCopy(document, new FileStream(fileout, FileMode.Create));
document.Open();
copy.AddDocument(pdfReader);
document.Close();
pdfReader.Close();
copy.Close();
}
This method creates a copy of the original file. During the following process, I need to delete the original file and rename the new file back to the original file's name.
How can I remove the bookmarks in the original PDF without the copy-delete-rename detour?

Printing InnerHtml to Printer with formating intact in vb.net/C#

In vb.net I need to print the contents showing in a browser control to printer.
I have used Gecko web-browser control in winform application and there is no direct way to print the page's contents.
Either way to print direct using InnerHtml or converting that html to pdf and then printing the pdf document.
currently I am using a third party library `ItextSharpe' but it gives errors.
public byte[] GetPDF(string pHTML) {
byte[] bPDF = null;
MemoryStream ms = new MemoryStream();
TextReader txtReader = new StringReader(pHTML);
// 1: create object of a itextsharp document class
Document doc = new Document(PageSize.A4, 25, 25, 25, 25);
// 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file
PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);
// 3: we create a worker parse the document
HTMLWorker htmlWorker = new HTMLWorker(doc);
// 4: we open document and start the worker on the document
doc.Open();
htmlWorker.StartDocument();
// 5: parse the html into the document
htmlWorker.Parse(txtReader);
// 6: close the document and the worker
htmlWorker.EndDocument();
htmlWorker.Close();
doc.Close();
bPDF = ms.ToArray();
return bPDF;
}
Byte[] bytes;
bytes = GetPDF(browse.Document.Body.InnerHtml);
var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");
System.IO.File.WriteAllBytes(testFile, bytes);
but throws errors while parsing.
Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'.
I have seen different examples over web but this is totally different, the examples or duplicate answer is about to export panels or grids but this is dynamic HTML and i need to convert it to PDF or print directly the client area.

C# ItextSharp integrate Quicktime movie into a PDF

I was wondering if it is possible to integrate a Quicktime movie into a PDF with iTextSharp or other PDF tools. I have been able integrate images without a problem. Thanks.
Is it this you are looking for?
Edit
In short
First you have to create a movie annotation by using the CreateScreen method of the PdfAnnotation class
You can create an movie annotation using this media types .aiff, .au, .avi, .mid, .mov, .mp4, .mp4, .mpeg, .smil, .swf
Then you need to add the movie annotation to the PDF by using the AddAnnotation method of the PdfWriter
/create a document object
var doc = new Document();
//output file path
String outfile = "d:/pdfdoc.pdf";
//get PdfWriter object
PdfWriter writer=PdfWriter.GetInstance(doc, new FileStream(outfile, FileMode.Create));
//open the document for writing
doc.Open();
//Create an instance of PdfFileSpecification
PdfFileSpecification fs = PdfFileSpecification.FileEmbedded(writer, "d:/bailey.mpg", "bailey.mpg", null);
//create and add a movie annotation to PDF document
writer.AddAnnotation(PdfAnnotation.CreateScreen(writer, new Rectangle(200f, 700f, 400f, 800f), "Bailey", fs,"video/mpeg", true));
//close the document
doc.Close();
//view the result pdf file
System.Diagnostics.Process.Start(outfile);
Note
I'm not the owner of this content, it's only a brief summary of the tutorial from the website worldbestlearningcenter.com (link above)

Categories