- How to add page numbers to your PDF
- Add text field to PDF
- Append multiple PDF documents
- Bulleted list from XML and XSL
- Merge PDF
- Calculate the height of a paragraph in PDF
- Multipage TIFF to PDF
- Convert TXT to PDF
- Convert XHTML to PDF
- Create PDF in C# - Tall Components - Check out our PDF code samples
- Text formatting
- Generate PDF form from XML
- Generate PDF with local images from XML with Xamarin.iOS
- XhtmlParagraph and TrueType fonts
- Add footer with left and right aligned text on same line
- Read and write meta data from PDF
- Stitch PDF documents
- Use multiple licenses
- What is the resulting fontsize in PDF rich text used in SimpleXhtmlShape
Generate PDF with local images from XML with Xamarin.iOS
This code sample generates a PDF from XML with an image that is included as resource. Here is the XML:
<document>
<section>
<paragraph type="textparagraph">
<fragment>Hello world</fragment>
</paragraph>
<paragraph type="TallComponents.ResourceImage" path="foopdf.jpg"/>
</section>
</document>
Note that the type of the image paragraph is <var>TallComponents.ResourceImage</var>. This is a specialization of <samp>TallComponents.PDF.Layout.Paragraphs.Image.</samp> This derived class overrides Compose to change the Image.Path property so that it points to the embedded resource image:
using System;
using System.IO;
using Foundation;
using TallComponents.PDF.Layout;
using TallComponents.PDF.Layout.Paragraphs;
namespace TallComponents
{
public class ResourceImage : Image
{
protected override void Compose(Document doc)
{
base.Path = System.IO.Path.Combine(NSBundle.MainBundle.BundlePath, base.Path);
}
}
}
The following code reads the XML and generates the PDF document:
string bundlePath = NSBundle.MainBundle.BundlePath;
string xmlPath = Path.Combine(bundlePath, WebUtility.UrlEncode(customWebView.Uri));
Document document = new Document();
document.Read(xmlPath);
// convert to PDF and save
string pdfPath = Path.Combine(bundlePath, "out.pdf");
using (FileStream fs = new FileStream(pdfPath, FileMode.Create))
{
document.Write(fs);
}
The full code sample displays the generated PDF using UIWebView. It is available on github.