iTextSharp XMLWorkerHelper Font Size - itext

I'm need to set the font size for the iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml() method.
Or set the document default font side. but I can't seem to find the methods in Document object :/
using (var stringReader = new StringReader(tcTopic.Body))
{
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(
pdfWriter, doc, stringReader
);
}

Setting the font-size in the HTML content that is being parsed through solved my issue.

Related

How to generate PDF in Hebrew? currently the PDF is generated empty

I'm using iTextSharp 5.5.13 and when i try to generate the PDF with Hebrew it comes out empty.
this is my code: I'm i doing something wrong?
public byte[] GenerateIvhunPdf(FinalIvhunSolution ivhun)
{
byte[] pdfBytes;
using (var mem = new MemoryStream())
{
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.GetInstance(document, mem);
writer.PageEvent = new MyHeaderNFooter();
document.Open();
var font = new
Font(BaseFont.CreateFont("C:\\Downloads\\fonts\\Rubik-Light.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED), 14);
Paragraph p = new Paragraph("פסקת פתיחה")
{
Alignment = Element.ALIGN_RIGHT
};
PdfPTable table = new PdfPTable(2)
{
RunDirection = PdfWriter.RUN_DIRECTION_RTL
};
PdfPCell cell = new PdfPCell(new Phrase("מזהה", font));
cell.BackgroundColor = BaseColor.BLACK;
table.AddCell(cell);
document.Add(p);
document.Add(table);
document.Close();
pdfBytes = mem.ToArray();
}
return pdfBytes;
}
The PDF comes out blank
I changed a few details of your code, and now I get this:
My changes:
Embedding the font
As I don't have Rubik installed on my system, I have to embed the font into the PDF to have a chance to see anything. Thus, I replaced BaseFont.NOT_EMBEDDED by BaseFont.EMBEDDED when creating the var font:
var font = new Font(BaseFont.CreateFont("Rubik-Light.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 14);
Making the Paragraph kind of work
You create the Paragraph p without specifying a font. Thus, a default font with default encoding is used. The default encoding is WinAnsiEncoding which is Latin1-like, so no Hebrew characters can be represented. I added your Rubik font instance to the Paragraph p creation:
Paragraph p = new Paragraph("פסקת פתיחה", font)
{
Alignment = Element.ALIGN_RIGHT
};
Et voilà, the writing appears.
iText developers often have communicated that in iText 5.x and earlier right-to-left scripts are only supported properly in certain contexts, e.g. in tables, but not in others like paragraphs immediately added to the document. As your Paragraph p is added immediately to the Document document, its letters appear in the wrong order in the output.
Making the PdfPTable work
You defined the PdfPTable table to have two columns (new PdfPTable(2)) but then you added only one cell. Thus, table contains not even a single complete row. iText, therefore, draws nothing when it is added to the document.
I changed the definition of table to have a single column only:
PdfPTable table = new PdfPTable(1)
{
RunDirection = PdfWriter.RUN_DIRECTION_RTL
};
Furthermore, I commented out the line setting the cell background to black because usually it is difficult to read black on black:
PdfPCell cell = new PdfPCell(new Phrase("מזהה", font));
//cell.BackgroundColor = BaseColor.BLACK;
table.AddCell(cell);
And again the writing appears.
Properly downloading the font
Another possible obstacle is that when downloading the font from the URL you gave — https://fonts.google.com/selection?selection.family=Rubik — one can see in the customization tab of the selection drawer that by default only Latin characters are included in the download, in particular not Hebrew ones:
I tested with a font file I downloaded with all language options enabled:

Embedding font using itext 5 for PDF/UA compliance

We are currently building a proof of concept to generate PDF/UA compliant PDF from from a CSS and html (xhtml) file using xslt. We are able tag the PDF and add the appropriate metadata information.
The last major issue we are unable to solve is embedding a standard PDF font zapfdinbats, which our accessibility assessment tool complains about - using PAC 2.0 along with adobe DC built in checker.
As you can see from the image below the other fonts we are using seems automatically get embedded using the xmlworker from our CSS.
I have also tried finding the font as indicated and found one, however, it doesn't seem to be the correct one.
Here is a sample of our code
private static ReturnValue CreateFromHtml(string html)
{
ReturnValue Result = new ReturnValue();
var stream = new MemoryStream();
using (var doc = new Document(PageSize.LETTER))
{
using (var ms = new MemoryStream())
{
using (var writer = PdfWriter.GetInstance(doc, ms))
{
writer.CloseStream = false;
writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_7);
//TAGGED PDFVERSION_1_7
//Make document tagged
writer.SetTagged();
//===============
//PDF/UA
//Set document metadata
writer.ViewerPreferences = PdfWriter.DisplayDocTitle;
doc.AddLanguage("en-US");
doc.AddTitle("document title");
writer.CreateXmpMetadata();
doc.Open();
var embedfont = HttpContext.Current.Server.MapPath("~/scripts/ZapfDingbats.ttf");
var fontProv = new XMLWorkerFontProvider();
fontProv.DefaultEncoding = "UTF-8";
fontProv.Register(embedfont);
//Testing zapfDingbats font
Font font = FontFactory.GetFont(embedfont, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Paragraph p1 = new Paragraph("Testing of Fonts", font);
doc.Add(p1);
//end font processing
var tagProcessors = (DefaultTagProcessorFactory)Tags.GetHtmlTagProcessorFactory();
tagProcessors.RemoveProcessor(HTML.Tag.IMG);
tagProcessors.AddProcessor(HTML.Tag.IMG, new CustomImageTagProcessor());
var cssFiles = new CssFilesImpl();
cssFiles.Add(XMLWorkerHelper.GetInstance().GetDefaultCSS());
var cssResolver = new StyleAttrCSSResolver(cssFiles);
var charset = Encoding.UTF8;
var context = new HtmlPipelineContext(new CssAppliersImpl(new XMLWorkerFontProvider()));
context.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(tagProcessors);
var htmlPipeline = new HtmlPipeline(context, new PdfWriterPipeline(doc, writer));
var cssPipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
var worker = new XMLWorker(cssPipeline, true);
var xmlParser = new XMLParser(true, worker, charset);
using (var sr = new StringReader(html))
{
xmlParser.Parse(sr);
doc.Close();
ms.Position = 0;
ms.CopyTo(stream);
stream.Position = 0;
}
}
}
}
// get bytes from stream
Result.Data = stream.ToArray();
// success
Result.Success = true;
return Result;
}
Maybe there is something in the CSS we need to do (our CSS is quite large f
iText only ships with the Adobe Font Metrics (AFM) file of Zapfdingbats. This means that you can't embed that font unless you provide the corresponding PostScript Font Binary (PFB) file. This PFB file can't be shipped with iText because iText doesn't have a license to do so.
The first step to solve this, is to:
purchase a Zapfdingbats license so that you get the PFB (If I recall correctly, it's a font owned by Adobe), or
use an alternative font when you want to insert special characters (check boxes, phone symbols,...) into your text (e.g. purchase a license for the AdobePiStd font that was used as a substitution font and use that font instead of Zapfdingbats).
In your case, you provided a font ZapfDingbats.ttf which you register with the XMLWorkerFontProvider. When you register this font, it can be recognized through an alias. If ZapfDingbats.ttf isn't picked up by XML Worker, there is probably a mismatch between the name of the font used in the PDF and the alias that was used when ZapfDingbats.ttf was registered.
What is the font name used for ZapfDingbats in the CSS? You should register ZapfDingbats using that name as alias.

How to move text written in Type 3 Font from one pdf to other pdf?

I have a pdf which include text written in Type 3 Font.
I want to get some text from it and write it into other pdf in exactly same shape.
I am using itext. Please give me a tip.
edit: I attached my code.
DocumentFont f = renderInfo.getFont();
String str = renderInfo.getText();
x = renderInfo.getBaseline().getStartPoint().get(Vector.I1);
In this code, I want to write str into x value position.
In Type 3 Font, is it work?
You can copy parts of one page to a new one using code like this:
InputStream resourceStream = getClass().getResourceAsStream("from.pdf");
PdfReader reader = new PdfReader(new FileOutputStream("from.pdf"));
Rectangle pagesize = reader.getPageSizeWithRotation(1);
Document document = new Document(pagesize);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("areaOfFrom.pdf"));
document.open();
PdfContentByte content = writer.getDirectContent();
PdfImportedPage page = writer.getImportedPage(reader, 1);
content.saveState();
content.rectangle(0, 350, 360, 475);
content.clip();
content.newPath();
content.addTemplate(page, 0, 0);
content.restoreState();
document.close();
reader.close();
This turns your
into
Unfortunately, though, that hidden content is merely... hidden... but it is still there. You can especially mark the lines with that hidden text and try to copy&paste them.
If you want to completely remove that hidden text (or start out by merely copying the desired text), you have to inspect the content of the imported page and filter it. I'm afraid iText does not yet explicitly support something like that. It can be done using the iText lowlevel API but it is quite some work.

Choosing a FontProviderImp in iTextSharp

I am using XMLWorker for parsing html. I have been having some issues with the fonts I define in the styles. For example, something simple as this:
<span style="font-family: Garamond">Foo Garamond</span>
wasn't not working.
I was using this as my css applier:
CssAppliers ca = new CssAppliersImpl();
In order to check if it was a problem with the encoding of the html, or any other issue..., I did my own implementation of the IFontProvider:
class MyFontProvider : IFontProvider
{
public bool IsRegistered(string fontname)
{
return false;
}
public Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color)
{
var font = FontFactory.GetFont(fontname, encoding, embedded, size, style, color);
return new Font(font);
}
}
Then, this:
CssAppliers ca = new CssAppliersImpl(new MyFontProvider());
Great!!!, that works fine!!!, also if I passed to the constructor this:
CssAppliers ca = new CssAppliersImpl(new XMLWorkerFontProvider());
also works.
So, it is obvious that the default implementation of the font provider is not working. I defined it as this:
CssAppliers ca = new CssAppliersImpl();
or
CssAppliers ca = new CssAppliersImpl(new FontFactoryImp());
, and neither worked.
My questions are:
What possible explanation has this?
Differences between XMLWorkerFontProvider and FontFactoryImp implementations
FontFactoryImp is part of iText/iTextSharp core. It provides access to the required core 14 fonts and allows you to optionally register additional font files and give them nice names. But unless you manually register a font file it will just return the "default" font.
XMLWorkerFontProvider is part of XMLWorker and is actually a subclass of FontFactoryImp. The biggest difference between the two is that if you use the empty constructor on XMLWorkerFontProvider it actually calls base.RegisterDirectories() which registeres every font in the system font folder. Depending on the number of fonts you have this could be expensive which is why the default version doesn't do it.

Why do my PDF fields stay blank in Adobe Reader if I do not flatten the PDF document?

I use iTextSharp to populate fields in a PDF. I would like the PDF to stay editable when the user opens it (i.e. so I can't flatten the PDF).
The problem is that when I stamp the PDF and view the values in Adobe Reader the fields remain blank.
If I stamp the PDF, and flatten it, and then view the values then the fields do in fact have values.
How can I populate fields and keep the PDF editable?
Here's my method as an ASP.NET MVC action:
public ActionResult GetPdfTWOWithSurname()
{
PdfHelpers _pdfHelper = new PdfHelpers(Server, Url, "~/Content/newForm.pdf");
var outStream = new MemoryStream();
var pdfReader = new PdfReader(_pdfHelper.GetPdfBytes());
var pdfStamper = new PdfStamper(pdfReader, outStream, '\0', true); //The 'true' value is important. Otherwise the document loses some dynamic features.
pdfStamper.Writer.CloseStream = false;
pdfStamper.FormFlattening = false; //Allow editing after close.
var fields = pdfStamper.AcroFields;
//Fill in text fields...
fields.SetField("topmostSubform[0].Page2[0].InvestorDetails_Surname[0]", "This is a surname");
pdfStamper.Close();
//Get outStream bytes and return...
outStream.Seek(0, SeekOrigin.Begin);
byte[] outBytes = new byte[outStream.Length];
outStream.Read(outBytes, 0, (int)outStream.Length);
return File(outBytes, "application/pdf", "ThePdfFileTWO.pdf");
}
Note that this PDF is reader enabled, and I assume it stays that way even after the save since Adobe reader shows the "Extended" toolbox when I open the file. And the fields remain editable.
Also note that the PDF remains editable, and the fields have values... in FoxitPDF reader.
Please HELP
I have that same issue and it only happens if the PDF was created with Adobe Acrobat with Extended Features. Only option I've found is to re-create the form so that pop-up box has no reason to pop-up. GL
http://what-when-how.com/itext-5/preserving-the-usage-rights-of-reader-enabled-forms-itext-5/
set FormFalttering to False and set RemoveUsageRights() (depends from the iText version)