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.
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 am trying to create a contract (document) that I can print like a normal word document.
I have a form which is my 'entry form') where i can type in details such as customer name, amount,date of contract etc. these information is then saved in ms access.
i have another form (which i call 'contract doc'). it has labels in it and the label holds the information i typed in my 'entry form'. I assigned one label to get the values that i entered in the textbox in the entry form.
contractdoc.contract_date_label.Text = contract_date_tb.Text;
contractdoc.deposit_label.Text = deposit_tb.Text;
contractdoc.customer_name_label.Text = customer_name_tb.Text;
i also added labels and typed the rest of the documents body and position them in the 'contract doc form ' to look like an actual contract.
but i dont know how to print it like a document. i iused :
printForm1.Print();
but what happens is, it asks me to save in xls format and only a messege box that says:"printing page 1 of document" with a cancel button.
I hope you can help. thank you in advance
I presume from the name printForm1, that you're using the PrintForm class provided as part of the Visual Basic PowerPack.
In that case, you need to make sure that the PrintAction property is set appropriately. To print to a physical printer, you need to set it to PrintAction.PrintToPrinter. Depending on what printer is configured as the system default, you might also need to set the PrinterSettings property. Then when you call the Print method, it will print to the correct printer.
It's probably prompting you to save the file in XLS format because your system default printer is a virtual one that generates XLS files.
I'm trying to set a background to a pdf and managed to set it with an image my pdf has a big table so the pages are added automatically not with the Document.NewPage() method so the image background is set only on the first page. This is the code that adds the background:
Image backImg = Image.GetInstance(#"D:\websites\DIS\bugs\130208\A4.png");
backImg.SetAbsolutePosition(0, 0);
backImg.Alignment = Image.UNDERLYING;
var doc = new Document(pageSize);
PdfWriter pdfWriter = PdfWriter.GetInstance(doc, new FileStream(filePath, FileMode.Create));
doc.Open();
doc.Add(backImg);
...
creating a big table
and not using the doc.NewPage() method. Do I have to loop throw every page and add the background image at the end before closing the doc, but how do I put it in the background not on top of the other elements?
Whenever you want to apply something to every page, you should use page events, more specifically PdfPageEvent.onEndPage(), to do it. You can find samples for its usage by keyword Page events > onEndPage --- these samples are taken from iText in Action 2nd Edition. The samples mainly add footers and headers while you want to add background graphics.
Be aware that you shouldn't add content to the Document instance here but instead directly to the PdfWriter, and as you want the image to be under the page content , not above it, you will need to use PdfWriter.getDirectContentUnder() like in the sample Stationery and not PdfWriter.getDirectContent() like in the other samples.
PS: The analogous samples for .Net can be found here.
PPS: The sample ImageDirect.java / ImageDirect.cs shows how to add an image to some direct content which might be the information missing here.
go for
PdfPageEvent.onStartPage()
. In this event, write your code to insert the image (as you are doing it). What it will do is that as soon as a new page is created, it will add the image to it and then the content over it; giving a watermark effect.
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...
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.