Skip to content
This repository has been archived by the owner on Jul 4, 2023. It is now read-only.

Commit

Permalink
Merge branch 'release'
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Hill committed Feb 19, 2019
2 parents 93946cf + 122d4f2 commit 9d3156f
Show file tree
Hide file tree
Showing 20 changed files with 860 additions and 813 deletions.
46 changes: 46 additions & 0 deletions DocxToPdf.Core.Tests/PdfRenderer.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using Xunit;

namespace DocxToPdf.Core.Tests
{
public class PdfRendererTests
{
[Fact]
public void CreatePdfShouldCreateValidPdfStructure()
{
PdfDocument pdf = new PdfDocument();
FontObject courierNew = pdf.AddFont("CourierNew");

pdf.AddInfoObject("pdftest", "Rob", "3Squared");

var page = pdf.AddPage(new PageExtents(612, 792, 24, 10, 24, 10));
page.AddFont(courierNew);

var contentObj = page.AddContentObject();

contentObj.AddTextObject(0, 0, "BOLLOX", courierNew, 12, "left");
contentObj.AddTextObject(0, 0, "BOLLOX", courierNew, 12, "left");
contentObj.AddTextObject(10, 10, "BOLLOX", courierNew, 12, "left");
contentObj.AddTextObject(20, 20, "BOLLOX", courierNew, 12, "left");
contentObj.AddTextObject(30, 30, "BOLLOX", courierNew, 12, "left");
contentObj.AddTextObject(40, 40, "BOLLOX", courierNew, 12, "left");
contentObj.AddTextObject(50, 50, "BOLLOX", courierNew, 12, "left");
pdf.Write(@"C:\Dumpzone\pdfUnit1.pdf");
}

[Fact]
public void PdfConvertConvertsDocXWhenDocXIsValid()
{
var reader = XmlReader.Create(@"C:\Dumpzone\steve\file_4.xml");
var xdoc = XDocument.Load(reader);
var nsm = xdoc.CreateReader().NameTable;

PdfDocument.FromDocX(xdoc).Write(@"C:\Dumpzone\steve\file_1.pdf");
xdoc.ToPdf().Write(@"C:\Dumpzone\steve\file_1a.pdf");
}
}
}

24 changes: 0 additions & 24 deletions DocxToPdf.Core.Tests/UnitTest1.cs

This file was deleted.

30 changes: 30 additions & 0 deletions DocxToPdf.Core/CatalogObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;

namespace DocxToPdf.Core
{
/// <summary>
/// Models the Catalog dictionary within a pdf file. This is the first created object.
/// It contains references to all other objects within the List of Pdf Objects.
/// </summary>
public class CatalogObject : PdfObject, IPdfRenderableObject
{
private readonly PageTreeObject _pageTreeObj;

public CatalogObject(PageTreeObject pageTreeObj, PdfDocument pdfDocument) : base(pdfDocument)
{
_pageTreeObj = pageTreeObj;
}

public string Render()
{
return ObjectRepresenation = string.Format("{0} 0 obj <</Type /Catalog /Lang(EN-US) /Pages {1} 0 R>>\rendobj\r",
this.PdfObjectId, _pageTreeObj.PdfObjectId);
}

public byte[] RenderBytes(long filePos, out int size)
{
Render(); //update representation
return this.GetUTF8Bytes(ObjectRepresenation, filePos, out size);
}
}
}
43 changes: 43 additions & 0 deletions DocxToPdf.Core/ContentObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace DocxToPdf.Core
{
/// <summary>
/// Represents the general content stream in a Pdf Page.
/// This is used only by the PageObjec
/// </summary>
public class ContentObject : PdfObject
{
//private string contentStream;
public PageObject ParentPage { get; }

public ContentObject(PdfDocument pdfDocument,PageObject parent) : base (pdfDocument)
{
//contentDict = null;
ObjectRepresenation = "%stream\r";
ParentPage = parent;
}

/// <summary>
/// Set the Stream of this Content Dict.
/// Stream is taken from TextAndTable Objects
/// </summary>
/// <param name="stream"></param>
public void AddObject(IPdfRenderableObject obj)
{
ObjectRepresenation += obj.Render();
}

public string IndirectRef() => $"/Contents {PdfObjectId} 0 R";

/// <summary>
/// Enter the text inside the table just created.
/// </summary>
/// <summary>
/// Get the Content Dictionary
/// </summary>
public byte[] RenderBytes(long filePos, out int size)
{
ObjectRepresenation = $"{this.PdfObjectId} 0 obj <</Length {ObjectRepresenation.Length}>>stream\r\n{ObjectRepresenation}\nendstream\rendobj\r";
return GetUTF8Bytes(ObjectRepresenation, filePos, out size);
}
}
}
19 changes: 19 additions & 0 deletions DocxToPdf.Core/ContentObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace DocxToPdf.Core
{
/// <summary>
/// Conventience Methods
/// </summary>
public static class ContentObjectExtensions
{
public static void AddTextObject(this ContentObject contentObj, double xPos, int yPos, string txt, FontObject font, int fontSize,
string alignment)
{
//Passes content object for parent page's pageDescription for margins.
contentObj.AddObject(new TextObject(xPos, yPos, txt, font, fontSize, alignment,contentObj));
}
}
}
44 changes: 44 additions & 0 deletions DocxToPdf.Core/FontObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace DocxToPdf.Core
{
///<summary>
/// Represents the font dictionary used in a pdf page
/// Times-Roman Helvetica CourierNew
/// Times-Bold Helvetica-Bold Courier-Bold
/// Times-Italic Helvetica-Oblique Courier-Oblique
/// Times-BoldItalic Helvetica-BoldOblique Courier-BoldOblique
///</summary>

public class FontObject : PdfObject, IPdfRenderableObject
{
//private string fontObject;
private string fontRef; //the PDF object tag
public string FontRef() => fontRef;

private string fontType; //the font name, eg, bit14 fonts like CourierNew
private readonly PdfDocument _pdfDocument;

public FontObject(string fType,PdfDocument pdfDocument) : base(pdfDocument)
{
_pdfDocument = pdfDocument;
fontRef = null;
fontRef = $"T{_pdfDocument.fontIndex}";
fontType = fType;

_pdfDocument.fontIndex++;
}


public string IndirectRef() => $"/{fontRef} {PdfObjectId} 0 R";

public string Render()
{
return ObjectRepresenation = $"{this.PdfObjectId} 0 obj <</Type/Font/Name /{fontRef}/BaseFont/{fontType}/Subtype/Type1/Encoding /WinAnsiEncoding>>\nendobj\n";
}

public byte[] RenderBytes(long filePos, out int size)
{
Render();
return this.GetUTF8Bytes(ObjectRepresenation, filePos, out size);
}
}
}
12 changes: 12 additions & 0 deletions DocxToPdf.Core/IPdfRenderableObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace DocxToPdf.Core
{
public interface IPdfRenderableObject
{
string Render();
byte[] RenderBytes(long filePos, out int size);
}
}
55 changes: 55 additions & 0 deletions DocxToPdf.Core/InfoObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;

namespace DocxToPdf.Core
{
/// <summary>
///Store information about the document,Title, Author, Company,
/// </summary>
public class InfoObject : PdfObject,IPdfRenderableObject
{
private readonly string _title;
private readonly string _author;
private readonly string _company;

public InfoObject(string title,string author,string company,PdfDocument pdfDocument) : base(pdfDocument)
{
_title = title;
_author = author;
_company = company;
}

/// <summary>
/// Get Date as Adobe needs ie similar to ISO/IEC 8824 format
/// </summary>
/// <returns></returns>
private string GetDateTime()
{
DateTime universalDate = DateTime.UtcNow;
DateTime localDate = DateTime.Now;
string pdfDate = string.Format("D:{0:yyyyMMddhhmmss}", localDate);
TimeSpan diff = localDate.Subtract(universalDate);
int uHour = diff.Hours;
int uMinute = diff.Minutes;
char sign = '+';
if (uHour < 0)
sign = '-';
uHour = Math.Abs(uHour);
pdfDate += string.Format("{0}{1}'{2}'", sign, uHour.ToString().PadLeft(2, '0'),
uMinute.ToString().PadLeft(2, '0'));
return pdfDate;
}

public string Render()
{
return ObjectRepresenation = string.Format("{0} 0 obj <</ModDate({1}) /CreationDate({1}) /Title({2}) /Creator({4}) " +
"/Author({3}) /Producer(3Squared) /Company({4})>>\nendobj\n",
this.PdfObjectId, GetDateTime(), _title, _author, _company);
}

public byte[] RenderBytes(long filePos, out int size)
{
Render();
return GetUTF8Bytes(ObjectRepresenation, filePos, out size);
}
}
}
10 changes: 10 additions & 0 deletions DocxToPdf.Core/Justification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace DocxToPdf.Core
{
public enum Justification
{
None,
Left,
Center,
Right
}
}
30 changes: 30 additions & 0 deletions DocxToPdf.Core/ObjectXRef.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;

namespace DocxToPdf.Core
{
/// <summary>
/// Represents the Object Id and the byte offset in the file.
/// </summary>
public class ObjectXRef : IComparable
{
internal long offset;
internal uint objNum;

internal ObjectXRef(uint objectNum, long fileOffset)
{
offset = fileOffset;
objNum = objectNum;
}

#region IComparable Members

public int CompareTo(object obj)
{
int result = 0;
result = (this.objNum.CompareTo(((ObjectXRef) obj).objNum));
return result;
}

#endregion
}
}
30 changes: 30 additions & 0 deletions DocxToPdf.Core/PageExtents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace DocxToPdf.Core
{
public struct PageExtents
{
public uint xWidth;
public uint yHeight;
public uint leftMargin;
public uint rightMargin;
public uint topMargin;
public uint bottomMargin;

public PageExtents(uint width, uint height, uint left = 0, uint top = 0, uint right = 0, uint bottom = 0)
{
xWidth = width;
yHeight = height;
leftMargin = left;
rightMargin = right;
topMargin = top;
bottomMargin = bottom;
}

public void SetMargins(uint left, uint top, uint right, uint bottom)
{
leftMargin = left;
rightMargin = right;
topMargin = top;
bottomMargin = bottom;
}
}
}
Loading

0 comments on commit 9d3156f

Please sign in to comment.