Generate PDF Xamarin.Forms NotImplementedException - c#

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

Related

ITextSharp pdf resize and data alignment

I am using ITextSharp to convert HTML to PDF but i want the PDF to be generated of size 5cm width. I used the following code
var pgSize = new iTextSharp.text.Rectangle(2.05f, 2.05f);
Document doc = new Document(pgSize);
but it is just resizing the pdf and my data disappeared in the pdf or get hide.
How can i align the data in the center in PDF or resize the pdf? Here is my code
public void ConvertHTMLToPDF(string HTMLCode)
{
try
{
System.IO.StringWriter stringWrite = new StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
StringReader reader = new StringReader(HTMLCode);
var pgSize = new iTextSharp.text.Rectangle(2.05f, 2.05f);
Document doc = new Document(pgSize);
HTMLWorker parser = new HTMLWorker(doc);
PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("~") + "/App_Data/HTMLToPDF.pdf",
FileMode.Create));
doc.Open();
foreach (IElement element in HTMLWorker.ParseToList(
new StringReader(HTMLCode), null))
{
doc.Add(element);
}
doc.Close();
Response.End();
}
catch (Exception ex)
{
}
}
You are creating a PDF that measures 0.0723 cm by 0.0723 cm. That is much too small to add any content. If you want to create a PDF of 5 cm by 5 cm, you need to create your document like this:
var pgSize = new iTextSharp.text.Rectangle(141.732f, 141.732f);
Document doc = new Document(pgSize);
As for the alignment, that should be defined in the HTML, but you are using an old version of iText and you are using the deprecated HTMLWorker.
You should upgrade to iText 7 and pdfHTML as described here: Converting HTML to PDF using iText
Also: the size of the page can be defined in the #page-rule of the CSS. See Huge white space after header in PDF using Flying Saucer
Why would you make it difficult for yourself by using an old iText version, when the new version allows you to do this:
#page {
size: 5cm 5cm;
}

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?

C#/XAML winRT apps export PDF

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

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

iTextSharp stops after 2 pages

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?

Categories