I want to preview a PDF file.
I used iTextSharp in C# to generate the PDF file.
How can I make a function to preview PDF file before print it.
I didn't find anything on the preview with iTextSharp
I did find an answer of my question and I want to shared it with all.
So, I have make a personnal viewer with Windows.Data.Pdf library.
I would just open the By saving it the opening it then having a check to see if it is open and if it isn't open anymore deleting it.
Document doc = new Document(iTextSharp.text.PageSize.A4, 10, 10, 20, 10);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("submitted.pdf", FileMode.Create));
doc.Open();
// do your generating code here
doc.Close();
System.Diagnostics.Process.Start("filepath\\submitted.pdf");
System.IO.File.Delete("filepath\\submitted.pdf");
Related
My windows form contains a textbox in which we need to enter html tags,One button to generate PDF.
And we need to load the textbox content into XML Reader and process each element of XML recursively then we need to generate a PDF file.
The PDF file must contain the data i.e;
for example if I entered tag in the text box in the pdf file it must display a table.
I am very new to Windows forms and XML also can any one help me to complete this task
You would need to use a library to create PDF files. iTextSharp is a common library which can help. Take a look at this library and samples, you would be able to create PDF files easily from your application
https://sourceforge.net/projects/itextsharp/
iText is a PDF library that allows you to CREATE, ADAPT, INSPECT and MAINTAIN documents in the Portable Document Format (PDF):
iTextSharp is the .NET port.
Got Answer with this simple code
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\MySamplePDF.pdf", FileMode.Create));
document.Open();
iTextSharp.text.html.simpleparser.HTMLWorker hw =
new iTextSharp.text.html.simpleparser.HTMLWorker(document);
hw.Parse(new StringReader(htmlText));
document.Close();
but my problem is the path I want to select the path dynamically.Can any one help me how to set the path dynamically in the above code.
PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\MySamplePDF.pdf", FileMode.Create));
Changed this code by using a savefile dialog box
SaveFileDialog svg = new SaveFileDialog();
svg.ShowDialog();
PdfWriter.GetInstance(document, new FileStream(svg.FileName + ".pdf", FileMode.Create));
I have used itextsharp for PDF documents and now I need to create and add a text to a Word document(I'm using OpenXml SDK) so I would like to know what classes and objects are used here to add a paragraph or to set the alingment and indentation or to set the basefont and size of font. For example, this is my code for creating PDF using iTextSharp and now I want to translate it to create Word:
Document document = new Document(iTextSharp.text.PageSize.A4, 40, 40, 50, 50);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfFile.FileName, FileMode.Create));
document.Open();
document.NewPage();
Paragraph title = new Paragraph("aaa",titleFont);
title.Alignment = Element.ALIGN_CENTER;
document.Add(title);
document.Add(Chunk.NEWLINE);
Paragraph p1 = new Paragraph("",Font11);
p1.IndentationLeft = 20f;
document.Add(p1);
The best way to discover the things you ask about is to download the Open XML SDK Productivity Tool from Microsoft's site. Create a small, sample document in Word (as a user), save it, close it, then open it in the Productivity Tool. That can show you both the underlying XML as well as standard code for generating the document. That way you can see what objects are used and how they're put together.
This is have I done it with DocX.dll:
var document = DocX.Create(wordFile.FileName);
Novacode.Paragraph title = document.InsertParagraph("AAA", false, titleFormat);
title.Alignment = Alignment.center;
document.InsertParagraph(Environment.NewLine);
document.Save();
Process.Start("WINWORD.exe", wordFile.FileName);
I'm using iTextSharp to update A PDF's file properties:
FileStream fs = File.Open(#"C:\Developer\C#Projects\BylawSearch\0001.pdf", FileMode.Open);
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
document.AddSubject("Blah");
document.AddTitle("Blah blah");
document.AddKeywords("Blah blah blah");
document.Close();
I'm getting a "The document has no pages." error from iTextSharp. Any help appreciated.
You haven't added any information to put on a page ... !!
document.Add(new Paragraph("Hello World!"));
... for example.
Your title etc are part of the document properties (rather than something that's "printed" to the pdf).
Check out this introductory example, that seems to cover what you're after.
In my case, I had added a paragraph, but specified a font that was null.
document.Add(new Paragraph("Hello World!", nullFont));
Either make sure the font is valid, or else don't use the Paragraph constructor with the Font argument.
(This does not apply to the poster's scenario, but may be helpful to someone else.)
I had the same issue with Xamarin, .NET. For me the error message was misleading, because it happened when i tried to create fonts from local files.
Project settings > Android Options > Additional supported encodings.
Set this to West and it solved my problem.
I am using iTextSharp to generate PDF out of HTML. I can save the PDF file ok, but I want to handle the PDF for the OS to open it, without having to save it to disk first.
How can I do that? I am doing this from within a WPF application.
Here's my code so far:
MemoryStream memoryStream = new MemoryStream();
TextReader reader = new StringReader(tb.Text);
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
PdfWriter pdfWriter = PdfWriter.GetInstance(document, memoryStream);
HTMLWorker worker = new HTMLWorker(document);
document.Open();
worker.StartDocument();
worker.Parse(reader);
worker.EndDocument();
worker.Close();
pdfWriter.CloseStream = false;
document.Close();
How can I "materialize" memoryStream.ToArray() into a .pdf file (in memory) and send it to Windows?
"Send to Windows" doesn't mean anything. Only a process knows how to deal with a PDF document. Like Adobe Acrobat. A process has no use for what you store in memory, it can't get to it. It needs a file. That's a non-issue in Windows, when you write a file you write to memory first. The file system cache. The difference between the disk and memory is very small in Windows, an important design feature of the operating system.
hi
how do we rotate PDF using itext library.
Thanks
If you writing to a new PDF document, the following line will create a new A4 page rotated (into landscape)
Document doc = new Document(PageSize.A4.Rotate());
This is a very simple example:
Document pdf = new Document(PageSize.A4);
PdfWriter.GetInstance(pdf, new FileStream("file.pdf", System.IO.FileMode.Create));
pdf.Open();
pdf.Add(new Paragraph("This is a pdf document!"));
pdf.Close();
Edit: My mistake, I read "how to create pdf ...". That is what the example above does. I am sorry.