pdfsharp - font embed? - pdfsharp

I am given a file that has some other language font. They can not download the font, so they want me to embed it in the pdf.
Now, I only have PDF Reader, so I cant edit or create a pdf file. so I decided to quickly do it in C#.NET using PDFSharp library, but I just cant seem to figure out how to embed fonts using pdfSharp?!
Also, it's only 1 file that I have to process, so if you know of a way to do it manually, then that would be great too.

There are two ways to do this. For each font you want to embed like this:
var options = new XPdfFontOptions(PdfFontEmbedding.Always);
var font = new XFont("Times New Roman", 12, XFontStyle.Regular, options);
Then if you use the font, it will get embedded.
If you want all fonts use on a page to be embedded you can do it like this:
var page = new PdfPage();
var gfx = XGraphics.FromPdfPage(page);
gfx.MFEH = PdfFontEmbedding.Automatic;
The second approach will also work for any fonts used in MigraDoc code.

Related

Font of a webpage using custom font to trick scraper

A website I am attempting to scrape information off has decided to use a custom font that means all information in the code is a jumble of letters. For example: "aEfrg9" could look like "Booked" when under the use of the font. How could I scrape such a webpage?
My thoughts:
Download font from page
Somehow translate information I'm looking for into the code message font, eg. I want to find "guitar" in the webpage so my program translates it to "hGke8j" using the font file downloaded.
How would I achieve step 2?
I am using Python 3.X

fastest way to get all links and images from a webpage?

So this isn't relly a problem but more like automate thingy...
I built a website and had to copy loads of content from previous webpage. I did that by copy-pasting the content from old page to the new page made with wordpress.
All link and images in the content still point to the old page. So I'd like to find something like a webscraping tools which would analyze list of selected links and then output would be all link pointing outside of my webpage and list of all images that I have to download
Considering that your old and new websites are going to have the same URL structure, here is a bookmarklet that you can save as a bookmark to your toolbar.
To make your job easy, open an old website page, and simply click on the bookmarklet button you've saved (code below). This code will replace the links from old website to new website. The images will be treated similarly. Next, you can copy the updated content and paste it into the editor of your new website (wordpress admin).
On the developer's console (F12 key), you will get a list of all the images that you have to download.
javascript:(function(){
var jqscript = document.createElement('script');
jqscript.onload = function() {
// treat the <a> tags
jQuery('#my-content-container').find('a[href^="http://my-old-website.com"]').each(function(i, anchor) {
jQuery(anchor).attr('href', jQuery(anchor).attr('href').replace('http://my-old-website.com', 'http://my-new-website.com/new-directory'));
});
// treat the <img> tags, and make a list of images to download
var images_to_download = [];
jQuery('#my-content-container').find('img').each(function(i, image) {
images_to_download.push(jQuery(image).attr('src'));
jQuery(image).attr('src', jQuery(image).attr('src').replace('http://my-old-website.com', 'http://my-new-website.com/new-directory'));
});
// output a list of images to the developer console
console.log(images_to_download);
};
jqscript.src = "//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js";
}());
P.S. To save this bookmarklet code, rightclick the toolbar of your browser and create a new bookmark, and enter the above code as the Location/URL.
This is just an option you should think about: You could use absolute path instead of Relative path, this will help you reuse code without to have to remap every link in it.
Relatif Path :
Read about my Tahiti vacation.
Absolute path :
Read about my Tahiti vacation.

PDFsharp add private/installed font

I want to apply Trade gothic font to my pdf text using PDFsharp, I have installed the font and use below line of code to apply
XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
// var options = new XPdfFontOptions(PdfFontEmbedding.Always);
XFont font = new XFont("TRADE GOTHIC BOLD CONDENSED NO. 20", 20, XFontStyle.Bold, options);
But it does not work!!.
Also I wanted to know in production I'm using Windows server 2008, is there a way I can dynamically add this font in production server even it is not there?
As suggested I followed the pdfsharp forum ,
this is my sample code
XPrivateFontCollection privateFontCollection = XPrivateFontCollection.Global;
// Uri fontUri = new Uri(MappedApplicationPath + "Fonts\\trade-gothic-no-20-condensed-bold-1361518746.ttf");
Uri fontUri = new Uri("C:\\inetpub\\wwwroot\\wss\\VirtualDirectories\\80\\Fonts\\trade-gothic-no-20-condensed-bold-1361518746.ttf");
LoadPrivateFont(privateFontCollection, fontUri, "./#TradeGothicNo.20-Condensed");
I tried all possible combination of path and file name , the name as mentioned in .ttf file but still getting exception . I have a sharepoint Visual webpart, and on page load event of that webpart m writing this code..
This is load method
protected void LoadPrivateFont(PdfSharp.Drawing.XPrivateFontCollection privateFontCollection, Uri fontUri, string sFontFamilyname)
{
try
{
privateFontCollection.Add(fontUri, sFontFamilyname);
}
catch
{
}
}
I have followed this post
http://forum.pdfsharp.net/viewtopic.php?f=2&t=1880
Thanks
When using fonts with PDFsharp, make sure the font is a TrueType font (not a PostScript font).
Also make sure you write the font name correctly - as shown by the Font applet of Windows or as shown by Word.
You can use a private font collection to use fonts that are not installed on the computer. This should solve your "problem" with Windows Server 2008. Use the WPF build of PDFsharp.
The PDFsharp source package includes a full working sample that uses private fonts.
A code snippet can be seen here:
http://pdfsharp.net/wiki/PrivateFonts-sample.ashx

Windows API Code Pack Thumbnail gives preview thumb of pdf but not Word or Excel

I am using the Microsoft API Code Pack and have a handler using the following code
string filename = "C:\\Hello.pdf";
ShellFile shellFile = ShellFile.FromFilePath(filename);
Bitmap bitmap = shellFile.Thumbnail.ExtraLargeBitmap;
context.Response.ContentType = "image/jpeg";
bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);
This works fine to produce a "preview" style thumbnail for pdf documents, but when I try it with Word or Excel I get the generic thumbnail for the filetype instead of a mini image of the document (which is what I'm looking for).
Anyone have ideas on this? Can it be done using this API?
As David said, not all Office documents are saved with a thumbnail. It's a property on the document itself. In Office 2010 you can see the checkbox on the Save As dialog box. In older versions of Office you can find it in the document properties.
Once the Office document is saved with a thumbnail, the code you have will work.

How to get users try the fonts they want online

I'm trying to get the following done:
I have a set of fonts and I want users to be able to try these fonts online, but i want the result to appear in an image so no one can use the font unless they buy it. I found an example about what I exactly want here. Can anyone tell me how to do it ??
Thanks
The src of the image is a server side page that renders an image:
http://1001freefonts.com/image.php?text=test&font=KatyBerry
So what you want to do is to set the src to an aspx-page with the input text as a querystring. From that you create a graphics object, and use the font to draw the specified text onto that
excerpt from article
private void button4_Click(object sender, System.EventArgs e)
{
Graphics grf = this.CreateGraphics();
try
{
grf.Clear(Color.White);
using (Font myFont = new Font("Arial", 14))
{
grf.DrawString("Hello .NET Guide!", myFont, Brushes.Green, new PointF(10, 100));
}
}
finally
{
grf.Dispose();
}
}
After that, you need to do a Response.Clear() so that you don't get any other text content rendered, and set Response.ContentType = "image/jpeg"; (or whatever content type you'll be using), and then write your image to the response output buffer.
If you don't need to display user-entered text, won't using static images be the KISS solution?
There are a number of solutions to this problem :
render the font on the server - this is probably the best approach and from a copyright pov (if any) the safest. The answer from David outlines an approach.
Render the font on the client. For a busy site this would place less load on your server. There are a number of solutions in javascript, my favourite being Cufon. The browser downloads the cufon runtime as well as a font file and this is rendered by the browser. You need to be aware of font licensing when using this approach, but it does require no setup on the server, and can even be used offline.
You can also use Flash to render the fonts, without licensing issues.

Resources