I am trying to use HTMLWorker using the following:
public static string toWorks(string s)
{
string fontpath = System.Web.HttpContext.Current.Server.MapPath("~/Content/");
BaseFont bf = BaseFont.CreateFont(fontpath + "ARIALUNI.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
var f = new Font(bf, 10, Font.NORMAL);
// var p = new Paragraph { Alignment = Element.ALIGN_LEFT, Font = f };
var styles = new StyleSheet();
styles.LoadTagStyle(HtmlTags.SPAN, HtmlTags.FONTSIZE, "10");
styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);
using (var sr = new StringReader(s))
{
List<IElement> list = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, styles);
// var elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, styles);
foreach (var e in list)
{
list.Add(e);
}
return list.ToString();
}
return null;
}
It converts:
src="/Content/UserFiles/635380078478327671/Images/test.png
To:
C:\Content\UserFiles\635380078478327671\Images\test.png
Any suggestion.
Please compare the following two examples:
HtmlMovies1
HtmlMovies2
If you use the first example to render an HTML file with images, you probably won't succeed. The second example introduces an ImageProvider implementation.
In the getImage() method of the ImageProvider interface, you get information about the path to an image. It is up to you to interpret this path. For instance: if the path is /Content/UserFiles/635380078478327671/Images/test.png, you can create an Image object by loading the bytes from that path, possibly after applying some minor changes to the path.
If you don't create an ImageProvider class, iText will do a single guess to find the path. In your case, that guess is wrong.
You can find the C# equivalent of the examples here: http://tinyurl.com/itextsharpIIA2C09
Related
I'm trying to extract text from PDF using the following method:
public static string GetRectangleText(string pdfPath, int pageId, float[] rectangleDimensions)
{
using (PdfDocument pdfDoc = new PdfDocument(new PdfReader(pdfPath)))
{
var page = pdfDoc.GetPage(pageId);
iText.Kernel.Geom.Rectangle rect = new iText.Kernel.Geom.Rectangle(rectangleDimensions[0], rectangleDimensions[1], rectangleDimensions[2], rectangleDimensions[3]);
var filter = new IEventFilter[1];
filter[0] = new TextRegionEventFilter(rect);
var filteredTextEventListener = new FilteredTextEventListener(new LocationTextExtractionStrategy(), filter);
var result = PdfTextExtractor.GetTextFromPage(page, filteredTextEventListener);
return result;
}
}
While it works fine for most documents, several PDFs which would seem to have their encoding broken, return strings like ǪȃǷǻȁǭǵǶǬdzȇǹǺǸǶǰǺǭdzȄǹǺǪǨ ,668(')25&216758&7,21 what should in fact be ВЫПУЩЕНО ДЛЯ СТРОИТЕЛЬСТВА / ISSUED FOR CONSTRUCTION
I wonder if some kind of specific LocationTextExtractionStrategy would help?
I'd like to create pdf with barcode using Itex7 library.
There is a lot of examples using older version of Itex, or Java, but I can't find solution for Itex7.
(generally new lib has no implementation of createImageWithBarcode method)
My solution could look like as:
string outputPdfFile = #"c:\DEV\pdfFromScratchWithBarCode.pdf";
using (iText.Kernel.Pdf.PdfWriter writer = new iText.Kernel.Pdf.PdfWriter(outputPdfFile))
{
using (iText.Kernel.Pdf.PdfDocument pdf = new iText.Kernel.Pdf.PdfDocument(writer))
{
iText.Layout.Document doc = new iText.Layout.Document(pdf);
doc.Add(new iText.Layout.Element.Paragraph("Title"));
iText.Barcodes.BarcodeInter25 bar = new iText.Barcodes.BarcodeInter25(pdf);
bar.SetCode("00600123456");
//HOW TO ADD barcode TO PDF ??
// ...
}
}
There is similar answer but for older version:
iText for .NET barcode
Thanks for advices.
I found the solution (create pdf, add barcode {type: Code 25 – Non-interleaved 2 of 5} and set valid postion)
using (iText.Kernel.Pdf.PdfWriter writer = new iText.Kernel.Pdf.PdfWriter(outputPdfFile))
{
using (iText.Kernel.Pdf.PdfDocument pdf = new iText.Kernel.Pdf.PdfDocument(writer))
{
iText.Layout.Document doc = new iText.Layout.Document(pdf);
doc.Add(new iText.Layout.Element.Paragraph("Title"));
//barcode
iText.Barcodes.BarcodeInter25 bar = new iText.Barcodes.BarcodeInter25(pdf);
bar.SetCode("0600123456");
iText.Kernel.Pdf.Canvas.PdfCanvas canvas = new iText.Kernel.Pdf.Canvas.PdfCanvas(pdf.GetFirstPage());
//bar.PlaceBarcode(canvas, iText.Kernel.Colors.ColorConstants.BLUE, iText.Kernel.Colors.ColorConstants.GREEN);
iText.Kernel.Pdf.Xobject.PdfFormXObject barcodeFormXObject = bar.CreateFormXObject(iText.Kernel.Colors.ColorConstants.BLACK, iText.Kernel.Colors.ColorConstants.BLACK, pdf);
float scale = 1;
float x = 450;
float y = 700;
canvas.AddXObject(barcodeFormXObject, scale, 0, 0, scale, x, y);
}
}
You can create an image from a PdfFormXObject by doing this:
var barcodeImg = new Image(bar.CreateFormXObject(pdf));
Here is your code including changes that does the trick:
string outputPdfFile = #"c:\DEV\pdfFromScratchWithBarCode.pdf";
using (var writer = new iText.Kernel.Pdf.PdfWriter(outputPdfFile))
{
using (var pdf = new iText.Kernel.Pdf.PdfDocument(writer))
{
var doc = new Document(pdf);
doc.Add(new Paragraph("Title"));
var bar = new BarcodeInter25(pdf);
bar.SetCode("000600123456");
//Here's how to add barcode to PDF with IText7
var barcodeImg = new Image(bar.CreateFormXObject(pdf));
doc.Add(barcodeImg);
}
}
I send the following string
string text = <img alt=\"\" src=\"http://localhost:6666/content/userfiles/admin/images/q4.png\" /><br/>
to:
public static Paragraph CreateSimpleHtmlParagraph (String text)
{
string fontpath = System.Web.HttpContext.Current.Server.MapPath("~/Content/");
BaseFont bf = BaseFont.CreateFont(fontpath + "ARIALUNI.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
var f = new Font(bf, 10, Font.NORMAL);
var p = new Paragraph
{
Alignment = Element.ALIGN_LEFT,
Font = f
};
var styles = new StyleSheet();
styles.LoadTagStyle(HtmlTags.SPAN, HtmlTags.FONTSIZE, "10");
styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);
using (var sr = new StringReader(text))
{
var elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, styles);
foreach (var e in elements)
{
p.Add(e);
}
}
return p;
}
using:
document.Add(CreateSimpleHtmlParagraph("<span style='font-size:10;'>" + "<b><u>" +
"Notes" + "</u></b>" + ": " + "<br/><br/>" + text + "</span>"));
to generate PDF using itextsharp, It works very well except the image is too large! Is there a way to check if the string includes width and height and if not add the to scale the image?
As Bruno said, please upgrade to XMLWorker.
What you need to do is implement the no longer supported IHTMLTagProcessor interface for the HTML tag that you are interested in. You are interested in the img tag so you'll want to just use basically what iText is already doing but with your own logic. Unfortunately their class is private so you can't just subclass it but you can see its contents here. You'll basically end up with a class like this:
public class MyImageTagProcessor : IHTMLTagProcessor {
void IHTMLTagProcessor.EndElement(HTMLWorker worker, string tag) {
//No used
}
void IHTMLTagProcessor.StartElement(HTMLWorker worker, string tag, IDictionary<string, string> attrs) {
if (!attrs.ContainsKey(HtmlTags.WIDTH)) {
//Do something special here
attrs.Add(HtmlTags.WIDTH, "400px");
}
if (!attrs.ContainsKey(HtmlTags.HEIGHT)) {
//Do something special here
attrs.Add(HtmlTags.HEIGHT, "400px");
}
worker.UpdateChain(tag, attrs);
worker.ProcessImage(worker.CreateImage(attrs), attrs);
worker.UpdateChain(tag);
}
}
Then in your code create a Dictionary holding the tag that you are targeting and an instance of that class:
var processors = new Dictionary<string, IHTMLTagProcessor>();
processors.Add(HtmlTags.IMG, new MyImageTagProcessor());
Finally, change your parsing call to use one of the overloads. We don't need to fourth parameter (providers) so we're passing null to that.
var elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, styles, processors, null);
I'm parsing an HTML with some images inside this.
This images are stored as embedded resource, not in the filesystem.
as I know, i need to set a custom image provider in HtmlPipelineContext, and this provider need to retrieve the image path or the itextsharp image.
The question is, somebody know which method of Abstract Image Provider i need to implement? and how?
this is my code:
var list = new List<string> { text };
byte[] renderedBuffer;
using (var outputMemoryStream = new MemoryStream())
{
using (
var pdfDocument = new Document(PageSize.A4, 30, 30, 30, 30))
{
var pdfWriter = PdfWriter.GetInstance(pdfDocument, outputMemoryStream);
pdfWriter.CloseStream = false;
pdfDocument.Open();
HtmlPipelineContext htmlContext = new HtmlPipelineContext(new CssAppliersImpl());
htmlContext.SetImageProvider(new MyImageProvider());
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
CssResolverPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDocument, pdfWriter)));
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser p = new XMLParser(worker);
foreach (var htmlText in list)
{
using (var htmlViewReader = new StringReader(htmlText))
{
p.Parse(htmlViewReader);
}
}
}
renderedBuffer = new byte[outputMemoryStream.Position];
outputMemoryStream.Position = 0;
outputMemoryStream.Read(renderedBuffer, 0, renderedBuffer.Length);
}
Thanks in advance.
Using a custom Image Provider it doesn't seem to be supported. The only thing it really supports is changing root paths.
However, here's one solution to the problem:
Create a new html tag, called <resimg src="{resource name}"/>, and write a custom tag processor for it.
Here's the implementation:
/// <summary>
/// Our custom HTML Tag to add an IElement.
/// </summary>
public class ResourceImageHtmlTagProcessor : AbstractTagProcessor
{
public override IList<IElement> End(IWorkerContext ctx, Tag tag, IList<IElement> currentContent)
{
var src = tag.Attributes["src"];
var bitmap = (Bitmap)Resources.ResourceManager.GetObject(src);
if (bitmap == null)
throw new RuntimeWorkerException("No resource with the name: " + src);
var converter = new ImageConverter();
var image = Image.GetInstance((byte[])converter.ConvertTo(bitmap, typeof(byte[])));
HtmlPipelineContext htmlPipelineContext = this.GetHtmlPipelineContext(ctx);
return new List<IElement>(1)
{
this.GetCssAppliers().Apply(
new Chunk((Image)this.GetCssAppliers().Apply(image, tag, htmlPipelineContext), 0f, 0f, true),
tag,
htmlPipelineContext)
};
}
}
To configure your new processor replace the line where you specify the TagFactory with the following:
var tagProcessorFactory = Tags.GetHtmlTagProcessorFactory();
tagProcessorFactory.AddProcessor(new ResourceImageHtmlTagProcessor(), new[] { "resimg" });
htmlContext.SetTagFactory(tagProcessorFactory);
Have piece of code like below:
var workStream = new MemoryStream();
var doc = new Document(PageSize.LETTER, 10, 10, 42, 35);
PdfWriter.GetInstance(doc, workStream).CloseStream = false;
doc.Open();
var builder = new StringBuilder();
builder.Append("MY LONG HTML TEXT");
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(builder.ToString()), null);
foreach (var htmlElement in parsedHtmlElements)
doc.Add(htmlElement);
doc.Close();
byte[] byteInfo = workStream.ToArray();
workStream.Write(byteInfo, 0, byteInfo.Length);
workStream.Position = 0;
return new FileStreamResult(workStream, "application/pdf")
And have one problem-how make that pdf justified? Is any method or something which quickly do that?
Ah, I get it, you mean "justified" instead of "adjusted". I updated your question. It's actually pretty easy. Basically it depends on the type of content that you're adding and whether that content supports this concept in the first place. Assuming that you have basic paragraphs you can set the Alignment property on them before adding them in your main loop:
foreach (var htmlElement in parsedHtmlElements){
//If the current element is a paragraph
if (htmlElement is Paragraph){
//Set its alignment
((Paragraph)htmlElement).Alignment = Element.ALIGN_JUSTIFIED;
}
doc.Add(htmlElement);
}
There's two types of justification, Element.ALIGN_JUSTIFIED and Element.ALIGN_JUSTIFIED_ALL. The second is the same as the first except that it also justifies the last line of text which you may or may not want to do.