So I'm using PdfContentByte to draw a simple line in an itextsharp pdf document, but when using it i get an error that says "An error exists on this page. Acrobat may not display the page correctly". Does anyone have a solution to this? The error usually pops up after I have selected to print the document.
Here is my code:
`cb.BeginText();
cb.SetLineWidth(1.0f);
cb.MoveTo(37.0f, doc.PageSize.Height - 105.0f);
cb.LineTo(doc.PageSize.Width - 37.0f, doc.PageSize.Height - 105.0f);
cb.Stroke();
cb.EndText();`
thanks in advance
You may not have anything but text operators between a BeginText() and EndText() pair. Move your line art code outside them.
There is a fix on this site:
http://sajeevkumar.com/blog/?p=155
It is more specific to java but I believe the api's are very similar. My other thoughts are to do with there being a page object, sometimes pdf frameworks produce mangled output unless you explicitly create a page object within said pdf and then draw onto the page...
Related
Tried to open a pdf result in blank pages. Retry with same pdf displayed all pages with content.
It happen only once & couldn't reproduce it.
My application work with 3 steps.
Open PDF
Add Barcode Image
Save PDF
Source pdf had 2 pages with text content, output pdf had only stamped pdf without content.
I believe something went wrong in following line because number of pages are correct but blank.
PdfDocument document = PdfReader.Open(filePath, PdfDocumentOpenMode.Modify);
I need find reason of failure but don't have any idea what went wrong at first time. I have already gone through following questions but they have different case.
https://stackoverflow.com/a/52453789/9102192
PDFSharp returning blank pages when adding password
Can anyone help me finding root cause for this incident or any guesses ?
I would try to do something like this:
using (PdfDocument pdfDoc = PdfReader.Open("source", PdfDocumentOpenMode.Modify)){
// Logic to insert image into pdf //
pdfDoc.Save("targetPath")
}
I use PdfSharp/Migradoc to generate PDF file in c#, now i tried to add document link inside the pdf file, basically it's to create a table of items, and click the item name to navigate to another detail page. I tried to use the Migradoc Paragraph.AddHyperlink() method, below it's the code used
Paragraph p = cell.AddParagraph();
Hyperlink link = p.AddHyperlink(tmp_value, HyperlinkType.Bookmark);
link.AddText(tmp_value);
link.AddPageRefField(some_bookmark);
My issue is this navigation page is created before the bookmarks are generated later on, after execution, the link is not appeared and only error message like "[item name] the bookmark [some_bookmark] is not defined", anyone can highlight me what's the proper way to acheve this? thanks.
You need something like paragraph.AddBookmark(tmp_value); as the target of the jump. That's what the error message is telling you: target bookmark not defined.
MigraDoc creates the PDF in two passes and it does not matter where in the document the target is.
link.AddPageRefField(some_bookmark); will add the page number of the page with the bookmark "some_bookmark". What's the purpose of that?
See also:
http://pdfsharp.net/wiki/HelloMigraDoc-sample.ashx
Hyperlink hyperlink = paragraph.AddHyperlink("Paragraphs");
hyperlink.AddText("Paragraphs\t");
hyperlink.AddPageRefField("Paragraphs");
The first line defines the target - the string defined with AddBookmark elsewhere in the document.
The second line gives text that is shown in the link. The third line adds a page number to the link.
I have reports that are being converted into PDFs. Some of these reports have information missing simply because we don't track it. I have created another PDF with the shell of the report and placed input fields controls on it. I would like to know if there is a way to apply the shell PDF to the converted PDF so users can enter information in those blank fields without having to print them out and hand write them? I have done this manually through Adobe Acrobat Pro 9.3 by applying the generated PDF to the shell PDF as a Layer. I have done as much poking around with iTextSharp concerning Layers, but I still haven't found anything that has worked.
Thank you in advanced!
1) Layers won't work with fields. PDF Layers are a part of the page contents. Form fields, as with all annotations, float above the page.
2) Having said that, you can hide and reveal form fields using Acrobat/Reader JavaScript. The "doc" object is usually "this" in field and page entry points, so to show a given field, it's just:
var fld = this.getField("fieldName");
fld.hidden = false;
There are quite a few different places you can add JS to a PDF. Various field events, page events, and document events. You can also set a layer's action to some javaScript. Heck you can set a bookmark's action to be javascript instead of a "go over there" action.
Note that layers are called "Optional Content Groups" (OCGs) in PDF tech-speak. If you really want to create a layer, it looks like it would go something like this:
// layer implements PdfOCG
PdfLayer layer = new PdfLayer("MyLayer", writer);
PdfContentByte cb = getAContentByteFromSomewhere();
cb.beginLayer(layer); // takes PDFOCG object
/* draw stuff to be part of that layer */
cb.endLayer();
There are a number of examples on the iText site corresponding to "iText In Action, 2nd edition" (I don't get paid, the author is a friend). The aforementioned examples can be found here.
This bears repeating: Fields cannot be part of an OCG (layer). They can however be scripted to act like they are.
I am using MasterPage and using using a CSS in master page. My problem is in its content page, I have a messgage box comes up in some condition resulting FONT SIZE INCREASE. I do not have any font specification in my content page. I do not know how my content page font size change when I click on "OK" to my message box.
Please suggest me some solution.
Thanks
How are you applying you styles? Are you doing it with Javascript or some framework?
If that's the case obviously some sort of error could result in a page reset, or the page dropping the styles and going to a default rendering.
You really need to post code for a better response.
I got rid of it. Initially the problem cause was, click on 'ok' to message box which was generated by JavaScript 'alert'. I tried to put MessageBox from System.Windows.Forms reference.
Using MessageBox ,got the solution.
But I did not understand the reason, why using 'alert' was causing problem.
If someone can throw light on it, it would be helpful for me and may be for others as well.
I'm generating a coupon based on dynamic input and a cropped image, and I'm displaying the coupon using ntml and css right now, the problem is, printing this has become an issue because of how backgrounds disappear when printing and other problems, so I think the best solution would be to be able to generate an image based on the html, or set up some kind of template that takes in strings and an image, and generates an image using the image fed in as a background and puts the coupon information on top.
Is there anything that does this already?
This is for an ASP.NET 3.5 C# website!
Thanks in advance.
edit: It'd be great if the output could be based on the HTML input, as the coupon is designed by manipulating the DOM using jQuery and dragging stuff around, it all works fine, it's just when it comes to the printing (to paper) it has z-indexing issues.
What you can do is create an aspx page that changes the response type to be in the format you want and then put the image into the stream. I created a barcode generator that does a similar thing. Excluding all the formalities of generating the image, you'll Page_Load will look something like this:
Bitmap FinalBitmap = new Bitmap();
MemoryStream msStream = new MemoryStream();
strInputParameter == Request.Params("MagicParm").ToString()
// Magic code goes here to generate your bitmap image.
FinalBitmap.Save(msStream, ImageFormat.Png);
Response.Clear();
Response.ContentType = "image/png";
msStream.WriteTo(Response.OutputStream);
if ((FinalBitmap != null)) FinalBitmap.Dispose();
and that's it! Then all you have to do in your image is set the URL to be something like RenderImage.aspx?MagicParm=WooHoo or whatever you need. That way you can have it render whatever you want to specify.
You can render html to a bitmap using the WebBrowser control in either a winforms or console application.
An example of this can be found here: http://www.wincustomize.com/articles.aspx?aid=136426&c=1
The above example can be modified to run in ASP.Net by creating a new STAThread and performing an Application.Run on it to start a new message loop.
PHP/Ruby Alternative
If you have accessed this question and are actually looking for soething that will work without Windows, you can try the KHTML library: http://wiki.goatpr0n.de/projects/khtmld
The website has a ridiculous name I admit, but I can assure you it is genuine. Other related pages are: the sourceforge page http://khtml2png.sourceforge.net/
Try PDFSharp...it's not exactly a "take this HTML and make a PDF" but with a small amout of fiddling you can easily make a PDF out of the info you are using to make the HTML.
MARKUP ONLY ALTERNATE SOLUTION
Use SVG and XSLT to transform the html data into an image that can be rendered/saved/etc.
I'll admit that at first it was tedious getting this to work because of all of the coordinates, but well worth the effort once it is running.
There is a very powerful image creation library called GD which I often use with PHP.
I am led to believe there is a wrapper for this library that ASP programmers can use. Try this
Unless the "other problems" are pretty severe, couldn't you just instruct your users to turn on Background Images when printing?
In any case, I'd default to serving a PDF rather than an image, doubly so since it is intended for print.
Just set up your css properly, so that you have a css file targeted at the print medium. It is pretty easy to guarantee that the coupon will always be legible, without worrying about whether they have bg images on or not. Needlesly moving to an image doesn't make any sense, unless there is some reason you don't want it to be machine readable.
I haven't tried to myself, but you should be able to render HTML into an image by using the WebBrowser control and the DrawToBitmap() method inherited from the base Control class.
UPDATE: I tried this myself and there are some caveats. The WebBrowser control doesn't seem to render the web page until the control is show, so the WebBrowser needs to be in a Form and the Form must be shown for the HTML to be rendered and the DocumentCompleted event to be raised.