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
Related
I am trying to create an fillable pdf form using iText7 (in C# .Net framework) by passing an HTML using the itext7.pdfhtml library. iText is successful in generating most of the fillable fields from the html except the dropdown.
The dropdown field appears to be read-only and chooses the first value as the default. I cannot click on it for selection. Please see the attached image from the pdf.
PdfWriter pdfWriter = new PdfWriter(dest);
ConverterProperties converterProperties = new ConverterProperties();
iText.Kernel.Pdf.PdfDocument pdfDocument = new iText.Kernel.Pdf.PdfDocument(pdfWriter);
converterProperties.SetCreateAcroForm(true);
//For setting the PAGE SIZE
pdfDocument.SetDefaultPageSize(new PageSize(PageSize.A3));
iText.Forms.PdfAcroForm form = iText.Forms.PdfAcroForm.GetAcroForm(pdfDocument, true);
Document document = HtmlConverter.ConvertToDocument(htmlString, pdfDocument, converterProperties);
document.Close();
Ok so I have gone ahead and done all my steps. My problem is now the export to pdf. I have gotten it to work using a screenshot and iTextSharp. However, my problem is that what if for some reason that screen is not the main screen when the it takes a screenshot it'll take a screenshot of something else and save that as the report. Is there a way to save directly the form or even go one step further and save certain parts of the form. I found this doing my search but I don't know what my next step would be:
public void SaveToPdf()
{
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream("E:/a.pdf", FileMode.Create));
document.Open();
Paragraph p = new Paragraph();
document.Add(p);
document.Add("DONT KNOW WHAT THE SYNTAX FOR CHART WOULD BE")
document.Close();
}
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.
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
Using asp.net with C# Codebehind and iTextSharp library.
I have a pdf form that I created in LiveCycle, that has text fields and a barcode (code 3 of 9). I use this template to create packing slips. When I run my code, I pull values out of the database and plug them into the text boxes and change the number value for the barcode. In order for the values to show up on the completed pdf, I have to flatten the pdf. It seems that when the pdf is flattened, I lose the barcode image. All that shows is the number that I set.
Does anyone have any Idea how to retain the barcode image when I flatten my pdf?
Here is a snippet of my code.
PdfReader pdfReader = new PdfReader(_pdfFullFilename);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(pdfTemplate, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
//...
foreach (string fieldKey in pdfFormFields.Fields.Keys)
{
if (fieldKey.Contains("BarCode[0]"))
pdfFormFields.SetField(fieldKey, _productNumber);
}
//...
pdfStamper.FormFlattening = true;
pdfStamper.Close();
pdfReader.Close();
Any Help would be much appreciated. Let me know if I need to expound on anything.