how to rotate pdf using itextsharp library - c#

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.

Related

Generating PDF in windows forms using XML reader

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));

Creating and editing text in Word document in C#

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);

How to open an existing PDF file with Migradoc PDF library

I am trying to use the Migradoc library from PDFSharp (http://www.pdfsharp.net/) to print pdf files. So far I have found that Migradoc does support printing through its MigraDoc.Rendering.Printing.MigraDocPrintDocument class. However, I have not found a way to actually open an existing PDF file with MigraDoc.
I did find a way to open an existing PDF file using PDFSharp, but I cannot successfully convert a PDFSharp.Pdf.PdfDocument into a MigraDoc.DocumentObjectModel.Document object. So far I have not found the MigraDoc and PDFSharp documentation to be very helpful.
Does anyone have any experience using these libraries to work with existing PDF files?
I wrote the following code with help from this sample, but the result when my input PDF is 2 pages is an output PDF with 2 blank pages.
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
...
public void PrintPDF(string filePath, string outFilePath)
{
var document = new Document();
var docRenderer = new DocumentRenderer(document);
docRenderer.PrepareDocument();
var inPdfDoc = PdfReader.Open(filePath, PdfDocumentOpenMode.Modify);
for (var i = 0; i < inPdfDoc.PageCount; i++)
{
document.AddSection();
docRenderer.PrepareDocument();
var page = inPdfDoc.Pages[i];
var gfx = XGraphics.FromPdfPage(page);
docRenderer.RenderPage(gfx, i+1);
}
var renderer = new PdfDocumentRenderer();
renderer.Document = document;
renderer.RenderDocument();
renderer.PdfDocument.Save(outFilePath);
}
Your code modifies the inPdfDoc in memory without saving the changes. Complicated code without any visual effect.
MigraDoc cannot open PDF files, MigraDoc cannot print PDF files, PDFsharp cannot print PDF files.
http://www.pdfsharp.net/wiki/PDFsharpFAQ.ashx

Preview PDF before save it

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");

Print BING Map Location in the PDF programmatically

I am using Silverlight BING Map Api to show the location (by providing the address) on my website..
And I want to show this Map location in the PDF file programmatically, I tried alot using 'wkhtmltopdf' but all in-vain, It shows empty space instead of BING MAP...
Please guide me in this case, I am open in using any other open source PDF generation tool.
Thanks
Xtremist
If you want insert image to PDF use iText library. It is very easy to start using it:
string pdfFilename = #"c:\temp\test.pdf";
string imageFilename = #"C:\map.jpg";
// Create PDF writer, document and image to put
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageFilename);
Document doc = new Document();
PdfWriter pdfW = PdfWriter.GetInstance(doc, new FileStream(pdfFilename, FileMode.Create));
// Open created PDF and insert image to it
doc.Open();
doc.Add(image);
doc.Close();
Or do you want to save Bing map as image?
I don't think that there's an easy way to do it through the Silverlight SDK. Instead, I think Microsoft prefers that you use the Bing Maps SOAP Services. Specifically you'll want to look at the Imagery Service where you can send a ImageryMetadataRequest with location/zoom info set in the ImageryMetadataOptions. Some basic sample code can be found here.
Once you get the images you can pretty easily add them to a PDF.

Categories