export to pdf using itextsharp - itext

I have a .aspx page in which I have a panel and some controls and one gridview. I am trying to take the print of that panel, but the downloaded pdf is empty. I don't know why, could someone explain me?
Here's the code I was trying
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.pnlprint.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

Try changing your:
Response.Write(pdfDoc);
to:
Response.BinaryWrite(pdfDoc.ToArray());

Related

How to convert PowerPoint (.ppt) to PDF in Java

Only last slide is getting convert means last slide overlapping every slides. Can anyone suggest how to combine it in one PDF?
I have tried with different approach but they are first creating image and then PDF.
FileInputStream inputStream = new FileInputStream(in);
SlideShow ppt = new SlideShow(inputStream);
inputStream.close();
Dimension pgsize = ppt.getPageSize();
Document document = new Document();
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(out));
document.setPageSize(new Rectangle(
(float)pgsize.getWidth(), (float)pgsize.getHeight()));
document.open();
PdfGraphics2D graphics= null;
for (int i = 0; i < ppt.getSlides().length; i++) {
Slide slide = ppt.getSlides()[i];
graphics = (PdfGraphics2D) pdfWriter.getDirectContent()
.createGraphics((float)pgsize.getWidth(), (float)pgsize.getHeight());
slide.draw(graphics);
}
graphics.dispose();
document.close();

itextsharp remove field position

I have this code to get field positions
PdfReader reader = new PdfReader(ssPdf);
var output = new MemoryStream();
var stamper = new PdfStamper(reader, output);
IList<AcroFields.FieldPosition> positions =stamper.AcroFields.GetFieldPositions(ssName);
and this to remove one position rect
if (positions.count>0)
positions.RemoveAt(1);
But then i dont have any method to set the field positions list to the stamper.
If i call the GetFieldPositions again it will return all the fields and not without the one i deleted
IList<AcroFields.FieldPosition> positions2 = stamper.AcroFields.GetFieldPositions(ssName);
stamper.Close();
reader.Close();

iText - Add text to existing landscape document

In my project, I am opening existing PDFs and add text on it at specific positions. This worked well since today where I got a landscape file. This is my code:
outputStream = new FileOutputStream(file + "_out.pdf");
PdfReader reader = new PdfReader(file);
PdfStamper stamper = new PdfStamper(reader, outputStream);
stamper.setRotateContents(true);
PdfContentByte canvas = stamper.getOverContent(1);
ColumnText.showTextAligned(canvas,
Element.ALIGN_LEFT, new Phrase("Text"), xPosition, yPosition, 0);
stamper.close();
reader.close();
outputStream.close();
When I opened the newly created file, the content is shown in portrait mode.
How do I have to change the code in order to get the file in landscape mode, together with the stamped text?
The file in question: http://www.share-online.biz/dl/RO1IPFVM0JX

Chapter-2 example on countrychunks

I am trying to run Countrychunks example from the Chapter 2. The example works but the line: document.Add(Chunk.NEWLINE); does not produce the new line and the loop overwrites the first line. I am posting my code here in case I am doing anything wrong:
public void createCountryChunks(String fileName)
{
iTextSharp.text.Font font;
Document document = new iTextSharp.text.Document();
//PdfWriter.GetInstance(document, new FileStream(fileName)).setInitialLeading(16);
PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
document.Open();
font = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 6, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.WHITE);
foreach (var p in myProducts)
{
// add a country to the document as a Chunk
document.Add(new Chunk(p.pr_name));
document.Add(new Chunk(" "));
Chunk id = new Chunk(p.pr_URN.ToString(), font);
// with a background color
id.SetBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f);
// and a text rise
id.SetTextRise(6);
document.Add(id);
document.Add(Chunk.NEWLINE);
}
document.Close();
}
As you can see the example is a bit different because of the data but the rest is almost the same as the original Java example.
Any suggestions please?
The setInitialLeading call that you weren't able to bring over and was commented out was actually very important. Adding that back in will solve your problems. I really don't like to add properties directly onto my constructed objects so I'm going to do it in two lines:
var w = PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
w.InitialLeading = 16;

iTextSharp ColumnText.SetSimpleColumn Addtext causes Error in Adobe Reader X 10.1.5

The code below illustrates a problem I have with iTextSharp. Everything works perfectly. The pdf file is created and appears correct on the screen. When I print the pdf from Adobe Reader X, it looks exactly right but Adobe reports "An error exists on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document to correct the problem."
Unfortunately, the file has to be attached to an email and sent to clients. The error message is not a good look and I want to fix it. It happens in all versions of Reader that I have tried, including 10.1.15 installed today.
I have iTextSharp 5.3.4.0 under Windows 7 Pro SP1
private void writeTestDoc()
{
string fname = "test.pdf";
float textWidth = 500;
float leftMgn = 60;
float rubricTop = 720;
float leftPad = 5;
float topPad = 12;
float leading = 0;
BaseFont baseCalibri = BaseFont.CreateFont("c:/windows/fonts/calibri.ttf", BaseFont.WINANSI, true);
Font plainFont = new Font(baseCalibri, 11, Font.NORMAL);
Document document = new Document();
try
{
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fname, FileMode.Create));
document.Open();
PdfContentByte cb = writer.DirectContent;
cb.BeginText();
ColumnText ct = new ColumnText(cb);
float boxTop = rubricTop;
ct.SetSimpleColumn(leftMgn + leftPad, boxTop - topPad, leftMgn + textWidth, boxTop, leading, Element.ALIGN_CENTER);
ct.AddText(new Phrase("A test message", plainFont));
ct.Go();
cb.EndText();
document.Close();
}
catch (Exception ex)
{
writeFile("ERROR in writeTestDoc " + ex.Message);
}
}
Remove cb.BeginText(); and cb.EndText();. It's illegal for BT/ET text objects to be nested. Report the place where you've found the documentation that told you to use BeginText()/EndText in combination with ColumnText, so that we can ask the author to correct it from his or her documentation.