We are using itextsharp library in one of our project for creating pdf from html.
Everything works fine, but PDF does not create exact replica of html text.
Like for example if html is like :-
<font size="3"><font face="Courier New, Courier, monospace">Plesae <strong>enter
your</strong> text in below editor and click <font size="4">Generate button to view pdf
from html to publish add in india</font></font>
and below code is being used to generate PDF then the font size are not properly taken by itext
StringReader strReader = new StringReader(content);
arrList = HTMLWorker.parseToList(strReader, null);
Paragraph para = new Paragraph();
for (int k = 0; k < arrList.size(); ++k) {
para.add((com.lowagie.text.Element)arrList.get(k));
}
We have made changes in library for mapping font size like if font size 3 is given then take 12 but still exact replica is not being created, may be for Courier 3 we need to map 13, 14 what i really looking forward is, if there is any formula for setting font size accrodingly. The Html being generated from CkEditor.
You need to use LoadTagStyle to handle it.
EX.
StyleSheet styles = new StyleSheet();
styles.LoadTagStyle(HtmlTags.P, HtmlTags.FONTSIZE, "16");
arrList = HTMLWorker.parseToList(strReader, style);
And add tag to wrap your whole thing
Related
I am trying to use PDFsharp and MigraDoc to create a PDF report file from many other files.
Basically the workflow is as follows:
The user uploads 2 or more documents to concatenate
The user selects what pages they want to include in the TOC
The documents are concatenated together and a table of contents is generated
Now I am able to easily concatenate the documents and add bookmarks/outlines in PDFsharp. Where I have run into problems is when using MigraDoc to create the TOC referencing the PDFsharp created bookmarks/outlines as the targets of the hyperlinks.
Code to add bookmark/outline in PDFsharp (successfully adds the bookmarks):
// Current document is a PdfDocument and Title is the name of the bookmark
CurrentDocument.Outlines.Add(title, page, true, PdfOutlineStyle.Bold);
MigraDoc code to create the TOC page and render it to the current PDFsharp document:
// Create blank page
PdfPage page = (!hasTitlePage)
? AddPage(null, 0, TOC_BOOKMARK_TITLE) // Add to start
: AddPage(null, 1, TOC_BOOKMARK_TITLE); // Add after title page
// Get Graphics obj
XGraphics gfx = XGraphics.FromPdfPage(page);
gfx.MUH = PdfFontEncoding.Unicode;
// Create MigraDoc document + Setup styles
Document document = new Document();
DefineStyles(document);
// Add header
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph("Table of Contents");
paragraph.Format.Font.Size = 14;
paragraph.Format.Font.Bold = true;
paragraph.Format.SpaceAfter = 24;
paragraph.Format.OutlineLevel = OutlineLevel.Level1;
// Add links - these are the PdfSharp outlines/bookmarks added previously when concatinating the pages
foreach (var bookmark in CurrentDocument.Outlines)
{
paragraph = section.AddParagraph();
paragraph.Style = "TOC";
Hyperlink hyperlink = paragraph.AddHyperlink(bookmark.Title);
hyperlink.AddText($"{bookmark.Title}\t");
hyperlink.AddPageRefField(bookmark.Title);
}
// Render document
DocumentRenderer docRenderer = new DocumentRenderer(document);
docRenderer.PrepareDocument();
docRenderer.RenderPage(gfx, 1);
return page;
Note - the MigraDoc code adds the TOC page but it doesn't recognize the PDFsharp bookmarks.
Screenshot of the TOC:
The TOC page is added at the start (although the TOC bookmark is listed at the end) but I cannot link to PDFsharp bookmarks
I am certain that the issue is with my understanding of MigraDoc and/or PDFsharp.
First prize would be to only use PDFsharp to add the TOC. I would appreciate any help in being pointed in the right direction.
MigraDoc is a world of its own - it uses PDFsharp to create PDF files, but cannot access internals of PDF files creates with PDFsharp (like Bookmarks in your case).
One approach: create the TOC with PDFsharp (as you already suggested).
Other approach: use MigraDoc to add pages from existing PDF files to a MigraDoc document to create the final PDF (you can add pages from PDF files like you add images).
With the second approach you can use the formatting capabilities of MigraDoc for the TOC and MigraDoc will insert the correct page numbers, provided you add MigraDoc Bookmarks for every image (imported PDF page).
With the first approach, you will have to do the formatting and insert the page numbers yourself, but you'll have more control over the final PDF.
Which is the "best" approach? Depends a bit on the extent of formatting you need for your TOC. With the second approach the TOC can have two or more pages and MigraDoc will take care of that automatically and entries in the TOC can have two or more lines and MigraDoc will also take care of that automatically. But I think a hack will be needed to add the Outlines (e.g. draw very small white text with the outline text somewhere on the page).
Update: For the PDFsharp only approach, you will add links with code like this:
PdfRectangle prect = new PdfRectangle(gfx.Transformer.WorldToDefaultPage(rect));
page.AddDocumentLink(prect, 1);
The second parameter to AddDocumentLink is the target page.
I am devoloping a wpf c# program that manages a school. From time to time the user has to print certifications for a single student. For instance a certification that this student is learning in our institution, or a certification with the amount of his stipend.
On these reports there is:
fixed text
dynamic information about the student like his name and so on
For this task I built rdls and it serves the purpose, but the users requested faster speed.
My question is: are rdls (working off a report server) the right method for this task? or is there a different faster option?
I use a FlowDocument for simple printing functionality such as this. You add XAML elements to the document just as you would when programmatically creating XAML in a window. Use the same layout controls (grids, stack panels, etc) to arrange all the other controls (text paragraphs, images, etc), and when the FlowDocument is printed it will be "flowed" into the page(s) based on various factors such as the paper size selected in the printer dialog.
Disclaimer: this was copy/pasted in a rush but it should give you an idea of how it works!
// Show the print dialog
var dlg = new PrintDialog();
if (!dlg.ShowDialog().GetValueOrDefault())
{
// User cancelled
return;
}
// Create and initialise the FlowDocument
_doc = new FlowDocument();
_doc.FontFamily = new FontFamily("Arial");
_doc.FontSize = 14;
// Add a paragraph of text
var para = new Paragraph(new Run("My paragraph....."))
{
FontSize = 14,
Foreground = new SolidColorBrush(Colors.Black),
Margin = new Thickness(0,0,0,12)
};
_doc.Blocks.Add(para);
// Add an image
var para = new Paragraph();
var img = new Image
{
Source = bitmapSource,
HorizontalAlignment = HorizontalAlignment.Center,
Margin = new Thickness(0,0,0,12)
};
para.Inlines.Add(img);
_doc.Blocks.Add(para);
// Print
var documentPaginator = ((IDocumentPaginatorSource)_doc).DocumentPaginator;
dlg.PrintDocument(documentPaginator, "My print job");
In my application I have all this wrapped in a class (as I have a few places where I need printing functionality). The class creates and initialises the FlowDocument in its constructor, and provides various methods such as "AddParagraph()", "AddImage()", with different overloads for specifying margins, fonts, font sizes, etc.
The document paginator bit at the end is a simplified version of my implementation, but it may be sufficient for your needs. (I've created a custom document paginator that provides the ability to set a header and footer on each page).
HI I'm building an app that export information to PDF using iTextSharp.
It has a header, a footer and the body is a table. That could have several pages.
Everything is okey excepts when the table has few columns, like 2.
If the table has 5 or 6 columns, the PDF works ok.
When I have just 2 or 3 columns the Adobe Reader gives me the following error "There is an error on this page, Acrobat could not show it properly. Contact the person who create this PDF" (I translated the message from spanish, so it could be different to the original)
If I open the same file using Chrome it works ok.
This is how i add a cell:
PdfPTable t = new PdfPTable(ColumnValid);
t.WidthPercentage = 98;
PdfPCell celda = new PdfPCell(new Phrase(Convert.ToString(cell.Value), font));
celda.BorderWidth = 0;
t.AddCell(celda);
Edit:
Demo PDF
I have two documents, I need to copy a picture from one document to the other. I can't use altChunks because I need to do further editing on the file.
I tried taking a clone of the sdtBlock that the image is in and appending that to the other document.
Like..
sdtBlock = document2.Decendants<StdBlock>.First().Clone(); //Block with image and text etc...
WordprocessingDocument oDoc = WordprocessingDocument.Open(document1, true);
Body body = oDoc.MainDocumentPart.Document.Body;
body.InsertAfter(sdtBlock, body.Elements<Paragraph>().First()); //insert block into new doc
That works for everything except the image. The image appears as either a red X or the document shows as corrupt. If you take the image out this method works fine.
Looking on msdn I think its because I need to create a relationship for that image?
Can anyone show me how this is possible?
Thanks!
Yes, you need to copy the image part across, and add a rel pointing to it. You need to make sure the relId in the paragraph matches the relId of the rel you added.
Have a look at DocumentBuilder in http://powertools.codeplex.com/ for how to do this.
i'm italian and i'm sorry for my english.
I'm trying to use itextSharp to convert a txt file into pdf file.
this is my code:
String l = file.ReadLine() + "\r\n";
while (l != null)
{
iTextSharp.text.Font contentFont = iTextSharp.text.FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL);
//Chunk line = new Chunk(l, contentFont);
Paragraph p2 = new Paragraph(l,contentFont);
oDoc.Add(p2);
oDoc.Add(Chunk.NEWLINE);
l = file.ReadLine();
}
oDoc.Close();
The text page is a multi page file, so I would like to reproduce the same on the pdf file.
When i read "Page 2" on my txt file, I need to create a new page in the pdf file.
I would like to put all the text before page 2 in only one pdf page.
How can i fit all the text in only one pdf page?
Thank's so much and sorry for my english
Well, if you reach the end of a text page before the end of a PDF page, you can just call oDoc.newPage(). The alternative isn't so simple.
The only Easy Way To Do It would be to create a text field on each page, with the multiline flag set. You then set the font size to zero and the field will automatically pick a font size that will size the font to fit the available space (within some reasonable limits).
You could also use a ColumnText and call go(true). This will "simulate" layout allowing you to make adjustments to the actual font size prior to actually drawing the text to a content stream.