PDF Clown does not render text - c#

I use PDF Clown to create PDF files containing text and different shapes. Saving the files brings the desired result. But when I print the pages or render them to bitmaps, only the shapes are visible and the text elements are missing.
I tried already different versions of the library with multiple files, always getting the same result.
Maybe someone can give me a hint on this issue.
EDIT:
This is a simplified form of the source code I use (with same result as described above; see the image at the end):
File file = new File();
Document document = file.Document;
document.PageSize = PageFormat.GetSize(PageFormat.SizeEnum.A4, PageFormat.OrientationEnum.Portrait);
Page page = new Page(document);
document.Pages.Add(page);
PrimitiveComposer composer = new PrimitiveComposer(page);
//draw a rectangle
composer.SetFillColor(DeviceRGBColor.Get(System.Drawing.Color.LightSalmon));
composer.DrawRectangle(new RectangleF(30, 42, 300, 32));
composer.Fill();
//draw some text
composer.SetFillColor(DeviceRGBColor.Get(System.Drawing.Color.Black));
composer.SetFont(new StandardType1Font(document, StandardType1Font.FamilyEnum.Courier, true, false), 32);
composer.ShowText("Hello World!", new PointF(32, 48));
composer.Flush();
//save the file
file.Save(#"..\document.pdf", SerializationModeEnum.Standard);
//and print it
Renderer renderer = new Renderer();
renderer.Print(file.Document, false);
Result of the above code (the printed version was created with virtual printer Adobe PDF; also tested with XPS Document Writer):

(I don't have enough points to add a comment so I put this comment as an answer.) If you look in the source for the RenderingSample class, you will see this comment:
This sample demonstrates how to render a PDF page as a raster image.
Note: rendering is currently in pre-alpha stage; therefore this sample is
nothing but an initial stub (no assumption to work!).
I don't think Stephano Chizzolini got around to finishing it.
There is another NuGet download, PDFClown.Net version 2.0.0, by Matthieu. It has tags for PDF-To-Image, Rasterizer and PDF, but I have not been able to get it to work either. I cannot find documentation for it. Inspection of the properties for the downloaded NuGET assembly shows version 0.1.2.0 instead of 2.0.0.

Related

Sautfinsoft PDF Focus .Net Converter text issue

So I've been trying to use SautinSoft software to convert a pdf document to a docx document. However, whenever I run this code my word document ends up having squished text. I've attached the images below, any idea what is going on?
SautinSoft.PdfFocus f = new SautinSoft.PdfFocus()
f.OpenPdf(#source);
if (f.PageCount > 0){
string path = Path.ChangeExtension(source, ".docx");
f.WordOptions.Format = SautinSoft.PdfFocus.CWordOptions.eWordDocument.Docx;
f.ToWord(#path);
}
This is a docx file after conversion. The image rendered fine, but the text is all squished for some reason. I'm also running on macOS (if that makes a difference). Thank you for anyone that can help!
I found the answer. You have to make sure to set
f.WordOptions.KeepCharScaleAndSpacing = false;
Because by default, the converter will scale the font widths based on the PDF fonts rather than your default word doc fonts.
Just load your fonts which you are using in the PDF in "C:/Windows/Fonts" and reboot your PC.
Then to convert your PDF to WORD again.
If it doesn't work for you. Try to use an another way. Specify the path to your folder's font for PDF Focus like :
f.WordOptions.Fonts = D:/Fonts;

PDFsharp PdfDocument.Save causing blank pages on any additional saves

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.

How do I copy an image to the clipboard in Selenium?

I am writing a test and the functionality I need to replicate is essentially saving a image to the clipboard and paste it later on. I am using Selenium WebDriver v3.11.1.
I have attempted using ContextClick to copy an image in many various ways and it never quite did what I wanted for example:
Actions rightClickAction = new Actions(driver);
rightClickAction.MoveToElement(logo).ContextClick(logo).SendKeys(Keys.ArrowDown).SendKeys(Keys.ArrowDown).SendKeys(Keys.ArrowDown).SendKeys(Keys.Enter).Build().Perform();
But the arrow down/enter never worked because it didn't focus on the right click menu. So then I found this bug https://bugs.chromium.org/p/chromedriver/issues/detail?id=1003 which makes me think that I can't use context click to copy an image. I also, couldn't just 'ctrl+c' the image.
I then learned that I could Clipboard which I couldn't get to set an image from my directory:
Clipboard.SetImage(Image.FromFile("C://Image.png"));
I then tried taking a screenshot as done here: C# Selenium - How do you take a screenshot in Visual Studio 2015 and that didn't work with either. Trying to save the screenshot file and add it to the 'clipboard' got messy.
I have also tried grabbing an image from a page by getting a base64 string of the image with JavaScript that is executed by webdriver, then saving the base64 string of the image to a file, which I found here: Using selenium to save images from page
This also got messy and I wasn't sure how to then save it to the clipboard.
So, how can I save an image to my clipboard?
You can try something similar to this:
driver.get("https://stackoverflow.com/");
WebElement element = driver.findElement(By.xpath("//span[(text()='Stack Overflow') and #class='-img _glyph']"));
WrapsDriver wrapsDriver = (WrapsDriver) element;
File screenshot = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
Rectangle rectangle = new Rectangle(element.getSize().width, element.getSize().height, element.getSize().height, element.getSize().width);
Point location = element.getLocation();
BufferedImage bufferedImage = ImageIO.read(screenshot);
BufferedImage destImage = bufferedImage.getSubimage(location.x, location.y, rectangle.width, rectangle.height);
ImageIO.write(destImage, "png", screenshot);
File file = new File("C:\\tmp\\123.png");
FileUtils.copyFile(screenshot, file);
Let me know if it works for you

How to add image background to pdf for every page?

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.

How to display a PdfPage as image in WPF form using Docotic.pdf.dll

I need to display PdfPage of a PDF Document in a WPF form. I am using Docotic.pdf.dll and want to stick to it. The PdfPage.Thumbnail is also giving me null with all PDFs.
Just confirmed from Bit Miracle, this feature has been added in Docotic.Pdf 3.4 (available on their site) and currently it is in beta phase .
Below is the code that will save the image from the Pdf.
using (PdfDocument pdf = new PdfDocument("your_file.pdf"))
{
PdfDrawOptions options = PdfDrawOptions.CreateZoom(150);
options.BackgroundColor = new PdfRgbColor(255, 255, 255); // white background, transparent by default
options.Format = PdfDrawFormat.Jpeg;
pdf.Pages[0].Save("result.jpg", options);
}
There is also Draw and print PDF group of samples that might worth to look into.
As far as I know Docotic.Pdf cannot display PDF files (at least the current version), it can only create them. The PdfPage.Thumbnail refers to the page thumbnail embedded in the PDF file, which usually is missing, this is why the property returns null.

Categories

Resources