I am developing an Application in Xamarin for Android. I have already generated HTML file using StringBuilder. Now I have a HTML file in my External storage and the same template is required for PDF. So when I try to convert HTML to PDF using iTextSharp using XML Worker & PDFSharp libraries, I am getting build errors due to missing System.Drawing.dll. Then I found from Xamarin forums & Stackoverflow links that it is not supported for Xamarin.Android.
Can anyone please tell me other alternative about how to create template for PDF or any other working nuget package for Xamarin.Android which will convert html file to pdf.
NOTE: I am able to generate PDF but not able to convert HTML to PDF.
It would be of great help!. Thanks a ton!.
Use Nuget package Xam.iTextSharpLGPL
Below is the sample code
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using Android.Graphics;
string path = Android.OS.Environment.ExternalStorageDirectory.Path;
string pdfPath = System.IO.Path.Combine(path, "samplee.pdf");
System.IO.FileStream fs = new FileStream(pdfPath, FileMode.Create);
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
HTMLWorker worker = new HTMLWorker(document);
document.Open();
StringBuilder html = new StringBuilder();
html.Append("<? xml version='1.0' encoding='utf-8' ?><html><head><title></title></head>");
html.Append("<CENTER>Simple Sample html</H1>");
html.Append("<H4>By User1</H4>");
html.Append("<H2>Demonstrating a few HTML features</H2>");
html.Append("</CENTER>");
html.Append("<p>HTML doesn't normally use line breaks for ordinary text. A white space of any size is treated as a single space. This is because the author of the page has no way of knowing the size of the reader's screen, or what size type they will have their browser set for.");
html.Append("</p></body</html>");
TextReader reader = new StringReader(html.ToString());
worker.StartDocument();
worker.Parse(reader);
worker.EndDocument();
worker.Close();
document.Close();
writer.Close();
fs.Close();
Related
As per research I found the way to convert my base64String of a word document to PNG.
By Using OpenXml ( it does not support .doc .. it only support docX )
Tried to convert the base64String to pdf by using iTextSharp ( but failed )
byte[] fileContent = Convert.FromBase64String(base64String);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
document.Open();
document.Add(Image.GetInstance(fileContent));
document.Close();
*My word document contains image/tables and text.
After decoding the base64 string to obtain the Word Document file, to access the file's contents for conversion to an image, you could look into using a library with the necessary components to handle and load the DOC format.
For example, the RasterCodecs class from the LEADTOOLS SDK (Which is what I am familiar with since I work for the vendor), uses the file filters that comes with that library for loading and saving a DOC file as PNG. This would look something like this:
byte[] fileContent = Convert.FromBase64String(base64String);
MemoryStream DocStream = new MemoryStream(fileContent);
using (RasterCodecs codecs = new RasterCodecs())
{
RasterImage image = codecs.Load(DocStream);
codecs.Save(image, new FileStream(filePath, FileMode.Create), RasterImageFormat.Png, 0);
}
DocStream.Close();
You can also consider using Microsoft's Office.Interop.Word for loading the DOC file, though you would have to implement conversion to PNG after this step.
I have a Windows Form App which automatically generates a PDF using itext. I was using the following code previously to generate the PDF:
namespace ConsoleApp2 {class Program{static void Main(string[] args){
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream("C:/Users/bra/800C.pdf", FileMode.Create));
document.Open();
document.Add(Chunk.NEWLINE);
Paragraph p17 = new Paragraph("Use formulations of Penzien (2000) assuming full slip condition. ");
document.Add(p17);
document.Close();
Console.ReadLine();
Is there a way that my code could be modified so that the PDF can be opened automatically when the program is run? (Rather than being saved on the C drive)? I have seen some examples with memoryStream online but couldn't wrap my head around how this works.
Just stamping some text into a pdf and itextsharp creats a corrupted file. When tried to read the pdf it throws error as follows
An exception of type 'iTextSharp.text.exceptions.InvalidPdfException'
Additional information: The document has no page root (meaning: it's an invalid PDF).
Following code is used to edit the pdf and stamp text content
using (PdfReader pdfReader = new PdfReader(System.IO.File.ReadAllBytes(pdfPath)))
using (Stream pdfStream = new FileStream(pdfPath, FileMode.Open, FileAccess.ReadWrite))
{
PdfReaderContentParser parserReason = new PdfReaderContentParser(pdfReader);
PdfStamper pdfStamper = new PdfStamper(pdfReader, pdfStream);
PdfContentByte pdfContentByte = pdfStamper.GetOverContent(pdfReader.NumberOfPages);
BaseFont baseFont = BaseFont.CreateFont(BaseFont.COURIER_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
pdfContentByte.SetColorFill(BaseColor.BLACK);
pdfContentByte.SetFontAndSize(baseFont, 12);
pdfContentByte.BeginText();
TextMarginFinder finderReason = parserReason.ProcessContent(pdfReader.NumberOfPages, new iTextSharp.text.pdf.parser.TextMarginFinder());
pdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Some text : " + annotation, finderReason.GetLlx(), finderReason.GetLly() - 20f, 0);
pdfContentByte.EndText();
pdfStamper.Close();
}
The pdf files are created with apache fop 1.1 and itextsharp is used to edit the file.The issue is not happening with all pdf but only with some files.
You can find the PDF which creates the issue here
The issue is that you are opening the file stream like this:
using (Stream pdfStream = new FileStream(pdfPath, FileMode.Open, FileAccess.ReadWrite))
FileMode.Open leaves the old content in place, writing to it merely overwrites it. In particular, if the new document is shorter than the original one, an old tail piece of the original document remains. As the PDF cross references are at its end, this results in the old cross references being applied to the new document. This obviously does not match.
If your use FileMode.Create instead, this issue does not happen.
By the way, your code completely fails for the sample file you provided because that sample file has no text on the final page. Thus, finderReason determines no margins rectangle, your access to finderReason.GetLlx() tries to access a null rectangle member, and consequentially it fails. You should add some appropriate checks.
I want to make my PDF document protected by not allowing fill in and copy from it. I am using iTextSharp for this. I have following code:
PdfReader reader = new PdfReader(document, System.Text.Encoding.UTF8.GetBytes(PASSWORD));
using (MemoryStream ms = new MemoryStream())
{
using (PdfStamper stamper = new PdfStamper(reader, ms))
{
stamper.SetEncryption(
null,
Encoding.ASCII.GetBytes(PASSWORD),
PdfWriter.ALLOW_PRINTING,
PdfWriter.ENCRYPTION_AES_128);
}
}
reader.Close();
When the document is generated I use that code to encrypt the document. But later when I open the document in Adobe Reader (tested on 9 and 11) and check the 'File > Properties > Security' their are no restrictions applied on fill in and copy of the document and their status is Allowed.
Is there any issue in that code?
According to the ITextSharp documentation for PdfStamper, the second parameter to this method is an output stream representing the destination for the encrypted PDF document data. The code you show in the question simply disposes the MemoryStream after you setup the encryption so any changes this code could apply to your PDF document will never be saved to disk or otherwise be available outside your application.
I am working in asp.net with C# website. I want to convert a HTML DIV which contains various html elements like divs,label, tables and images with css styles(background color, cssClass etc) and I want its whole content to be converted into PDF using iTextSharp DLL but here I am facing a issue that css is not getting applied.Can any one help me by providing any example or code snippet.
Install 2 NuGet packages iTextSharp and itextsharp.xmlworker and use the following code:
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
byte[] pdf; // result will be here
var cssText = File.ReadAllText(MapPath("~/css/test.css"));
var html = File.ReadAllText(MapPath("~/css/test.html"));
using (var memoryStream = new MemoryStream())
{
var document = new Document(PageSize.A4, 50, 50, 60, 60);
var writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
using (var cssMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(cssText)))
{
using (var htmlMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)))
{
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, htmlMemoryStream, cssMemoryStream);
}
}
document.Close();
pdf = memoryStream.ToArray();
}
Check out Pechkin, a C# wrapper for wkhtmltopdf.
Specifically at this point in time (considering a pending pull request) I'd check out this fork that addresses a couple of bugs (particularly helpful in IIS based on my experience).
If you don't go with the fork / get other stability issues you may want to look at having some kind of "render queue" (e.g. in a database) and have a background process (e.g. Windows service) periodically run over the queue and render then store the binary content somewhere (either in database as well, or on file system). This depends entirely on your use-case though.
Alternatively the similar solution #DaveDev has comment linked to.