iTextSharp stops after 2 pages - c#

I'm trying to convert HTML to PDF with iTextSharp.
Here's my code:
Document doc = new Document(PageSize.A4);
StringReader reader = new StringReader(responseHtml);
FileStream pdfStream = new FileStream("C:\\temp\\foo.pdf", FileMode.OpenOrCreate);
PdfWriter writer = PdfWriter.GetInstance(doc, pdfStream);
doc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, reader);
doc.Close();
The issue is that the PDF file stops after 2 pages.
The rest of the content just doesn't make it to the PDF file.
When I change the PageSize to A1, I get the whole content, 'cause it fits on two A1 pages.
How do I get it to create more than two pages?

Related

Itext PDF Image

I am trying to add a signature to the 2nd page of a PDF Form. The PDF was created outside of IText, but it has fields that were populated using IText in C#/MVC. That part works fine.
The issue is getting the signature image to the 2nd page. I can get the image onto the 1st page and position it, but cannot get it to the 2nd page.
I have tried to using the var PdfDocument class' GetPage(x) method without any luck. I have also tried to use GetLastPage(). No luck.
I tried the above approach before flattening the fields and after but no luck.
PdfReader reader = new PdfReader(fileSource);
System.IO.MemoryStream m = new System.IO.MemoryStream();
PdfWriter writer = new PdfWriter(m);
PdfDocument pdf = new PdfDocument(reader, writer);
Document document = new Document(pdf);
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);
String imageFile = "C:/Temp/signature.png";
ImageData data = ImageDataFactory.Create(imageFile);
Image img = new Image(data);
img.SetFixedPosition(50, 50);
pdf.GetPage(2);
document.Add(img);
form.FlattenFields();
document.Close();
pdf.Close();
pdfBytes = m.ToArray();
return new FileContentResult(pdfBytes, "application/pdf");
Any help would be appreciated.

iText 7 and C# writing a PDF file from MemoryStream?

I have a PDF document (using iText 7/C# 4.01) that I am creating in a MemoryStream and at the end, I want to write it out to a file. Part of the reason I am creating it in a memory stream is that I want to stamp a header table and footers on it at the end and was hoping to avoid writing it to a file then reading the file back in, stamping, then writing out a new file (as the examples I keep finding on iText website seem to do). However, I seem to be having some sort of chicken/egg scenario in the below code. It seems that you have to Close() the document in order for iText to fully form it. However, if I Close() it, then I get an ObjectDisposedException when trying to write it (simplified example below). I have to be missing something simple here, right? Thanks
MemoryStream baos = new MemoryStream();
PdfWriter writer = new PdfWriter(baos);
PdfDocument pdfDocument = new PdfDocument(writer.SetSmartMode(true));
//writer.SetCloseStream(true);
//pdfDocument.SetCloseWriter(true);
//pdfDocument.SetCloseReader(true);
//pdfDocument.SetFlushUnusedObjects(true);
Document d = new Document(pdfDocument, iText.Kernel.Geom.PageSize.LETTER);
d.Add(new Paragraph("Hello world!"));
//d.Close();
FileStream file = new FileStream("C:\test.pdf",
FileMode.Create, FileAccess.Write);
baos. WriteTo(file);
file.Close();
//baos.Close();
//d.Close();
Try this
I dont have IDE for test, but i think this work
MemoryStream baos = new MemoryStream();
PdfWriter writer = new PdfWriter(baos);
PdfDocument pdfDocument = new PdfDocument(writer.SetSmartMode(true));
Document d = new Document(pdfDocument, iText.Kernel.Geom.PageSize.LETTER);
d.Add(new Paragraph("Hello world!"));
d.Close();
byte[] byte1 = baos.ToArray();
File(byte1, "application/pdf", "C:\\iTextTester\\test.pdf");

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.

CSV to PDF file in c#

I am trying to convert a comma separated string(CSV) file to pdf file in C# using the iTextSharp library. I would like to know if there is any other free or more efficient tool/way for this type of conversion.
You can use iTextSharp
Here's an example:
Document pdfDoc = new Document();
string path = #"c:\file.pdf";
PdfWriter writer = PdfWriter.GetInstance( pdfDoc, new FileStream(path, FileMode.OpenOrCreate));
pdfDoc.Open();
pdfDoc.Add(new Paragraph("your data"));
pdfDoc.Close();

Categories