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")
}
Related
While I have a workaround for this problem, I'd prefer to do it the way that I ran into this problem. Let me explain the context before getting to the problem:
I'm using a scanner to get virtual images which I want to use to create a PDF with each page being one single image. I got the working just fine using PDFsharp for creation of the PDF.
However; if I try to re-save the PDFsharp document, it ends up blanking all the previous pages, and then adding the new image.
For a little more context, I'm using ASP.NET Windows Forms, with NTwain for the scanning software, PDFsharp for creating the PDF, and PdfiumViewer to view the PDF on the Windows form.
I have a class-level variable for the document
PdfDocument document = new PdfDocument();
On image save, I simply save the image to a new page
var img = pictureBox1.Image;
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XImage image = XImage.FromGdiPlusImage(img);
gfx.DrawImage(image, 0, 0);
To render out the document, I copy the document (thought this might fix the problem) to a new document, save the contents to a new memory stream, and simply view the PDF
var viewDocument = (PdfDocument)document.Clone();
MemoryStream ms = new MemoryStream();
viewDocument.Save(ms, false);
ms.Position = 0;
var pdf = PdfiumViewer.PdfDocument.Load(ms);
pdfRenderer1.Load(pdf);
I got it working by saving the image to a list, instead of as a new page, and remade the document each time I wanted to display it. The issue is really strange, however.
Any help understanding why it does that is appreciated.
AFAIK this is the state of the implementation: The recommended way is to create a PdfDocument or open it from a file, make the changes you need, then save it once.
This is what you do in the implementation that works for you.
Saving the document, opening it, adding more pages, saving it again would also work. This way you would not need a list of all images. But IMHO using the list and creating a new PdfDocument each time you save is the cleanest way.
Unexpected results occur when saving again after further changes. This is a known issue. Feel free to investigate this issue and fix it. Maybe it is just a simple change, maybe it is complicated.
I use PdfSharp to create a PdfDocument object:
private readonly PdfDocument _pdf = new PdfDocument();
Along with the other methods for actually adding the text, setting font styles etc. I then write the pdf file to a temporary location on disk:
var tempPdf = string.Concat(Path.GetTempPath(), Guid.NewGuid().ToString(), ".pdf");
_pdf.Save(tempPdf);
Which I then use in System.Windows.Clipboard class to copy the content to the clipboard:
Clipboard.SetFileDropList(new StringCollection {tempPdf});
When the user pastes the Clipboard content into a Word document for example, the pdf content appears in the document as desired.
The problem I have is that when the user double-clicks on the embedded PDF it launches the PDF in a viewer such as Adobe Reader, also as expected. The (unanticipated) problem I have is that I need to prevent this pop-up from happening given that this content will be inserted into a legal document.
Can any suggest any approaches or strategies for helping me to achieve something like this? To summarise - allowing them to paste [pdf] content stored in the clipboard to a Word document, but without it opening on double-clicking.
The answer was in my question ;)
And credit to Nyerguds for helping me out of my cocoon - I was fixated on using PDF files to copy to the clipboard.
The solution is to replace the usages of PdfSharp for pdf generation with System.Drawing.Graphics / System.Drawing.Bitmap to create jpg images of that which we want to copy.
And once the clipboard contents have been pasted to the Word document, we no longer have that problem of the image being openable on double-clicking.
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 am very new to c#/WPF and I need help with what should be a very simple app. In the app I am designing a user simply browses for a .txt or .jpg file which is then loaded. I would like the user to then be able to print what's displayed. The code I have found to work appears to only print part of the contents on one page. In addition, when the .txt files are printed there are no margins and the text seems to go right off the page. Here is the code I am using as it seems to be very basic.
System.Windows.Controls.PrintDialog pd = new System.Windows.Controls.PrintDialog();
if (pd.ShowDialog() != true) return;
pd.PrintVisual(textbox2, "textbox2.");
I guess my question would be, how do I set margin spacing and allow for multiple page document printing? Any help would be greatly appreciated. Thank you.
To print text files you will probably want to use a FlowDocument, this allows you to set margins, pagination etc, e.g.
Printing a WPF FlowDocument
To print a JPEG, you can use PrintVisual, e.g.
Load image from file and print it using WPF... how?
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...