1
0
Fork 0
mirror of https://github.com/Nioux/AideDeJeu.git synced 2025-10-29 14:35:45 +00:00

Export pdf uwp

This commit is contained in:
Yan Maniez 2019-06-24 00:52:44 +02:00
parent de54fad9c5
commit 9e3a07efe4
6 changed files with 65 additions and 32 deletions

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
@ -61,6 +62,7 @@ namespace AideDeJeu.UWP
Xamarin.Forms.Forms.Init(e, Rg.Plugins.Popup.Popup.GetExtraAssemblies());
SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3());
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{

View file

@ -1,4 +1,5 @@
using AideDeJeu.ViewModels;
using AideDeJeu.Pdf;
using AideDeJeu.ViewModels;
using AideDeJeu.ViewModels.Library;
using AideDeJeu.ViewModels.PlayerCharacter;
using AideDeJeu.Views;
@ -19,6 +20,7 @@ namespace AideDeJeu
InitializeComponent();
DependencyService.Register<MainViewModel>();
DependencyService.Register<PdfService>();
DependencyService.Register<BookmarksViewModel>();
DependencyService.Register<StoreViewModel>();
DependencyService.Register<PlayerCharacterEditorViewModel>();

View file

@ -10,11 +10,19 @@ using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Xamarin.Forms;
namespace AideDeJeu.Pdf
{
public class PdfService
{
public static PdfService Instance
{
get
{
return DependencyService.Get<PdfService>();
}
}
public static void DrawText(PdfContentByte cb, string text, iTextSharp.text.Font font, float x, float y, float width, float height, int alignment)
{
cb.SetRGBColorFill(127, 127, 127);
@ -35,7 +43,7 @@ namespace AideDeJeu.Pdf
{
var pipeline = new Markdig.MarkdownPipelineBuilder().UseYamlFrontMatter().UsePipeTables().Build();
_Document = new Document(new Rectangle(822, 1122));
_Document = new Document(new iTextSharp.text.Rectangle(822, 1122));
_Writer = PdfWriter.GetInstance(_Document, stream);
_Document.Open();
//PdfReader reader = null;
@ -100,48 +108,48 @@ namespace AideDeJeu.Pdf
}
Font _Header1Font = null;
public Font Header1Font
iTextSharp.text.Font _Header1Font = null;
public iTextSharp.text.Font Header1Font
{
get
{
return _Header1Font ?? (_Header1Font = new iTextSharp.text.Font(CinzelBold, 30));
}
}
Font _Header2Font = null;
public Font Header2Font
iTextSharp.text.Font _Header2Font = null;
public iTextSharp.text.Font Header2Font
{
get
{
return _Header2Font ?? (_Header2Font = new iTextSharp.text.Font(CinzelRegular, 25));
}
}
Font _Header3Font = null;
public Font Header3Font
iTextSharp.text.Font _Header3Font = null;
public iTextSharp.text.Font Header3Font
{
get
{
return _Header3Font ?? (_Header3Font = new iTextSharp.text.Font(CinzelRegular, 20));
}
}
Font _Header4Font = null;
public Font Header4Font
iTextSharp.text.Font _Header4Font = null;
public iTextSharp.text.Font Header4Font
{
get
{
return _Header4Font ?? (_Header4Font = new iTextSharp.text.Font(CinzelRegular, 18));
}
}
Font _Header5Font = null;
public Font Header5Font
iTextSharp.text.Font _Header5Font = null;
public iTextSharp.text.Font Header5Font
{
get
{
return _Header5Font ?? (_Header5Font = new iTextSharp.text.Font(CinzelRegular, 16));
}
}
Font _Header6Font = null;
public Font Header6Font
iTextSharp.text.Font _Header6Font = null;
public iTextSharp.text.Font Header6Font
{
get
{
@ -150,8 +158,8 @@ namespace AideDeJeu.Pdf
}
Font _ParagraphFont = null;
Font ParagraphFont
iTextSharp.text.Font _ParagraphFont = null;
iTextSharp.text.Font ParagraphFont
{
get
{
@ -295,18 +303,18 @@ namespace AideDeJeu.Pdf
private Phrase Render(HeadingBlock block)
{
var fonts = new Font[] { Header1Font, Header2Font, Header3Font, Header4Font, Header5Font, Header6Font };
var colors = new Color[] { new Color(0x9B1C47), new Color(0, 0, 0), new Color(0, 0, 0), new Color(0, 0, 0), new Color(0, 0, 0), new Color(0, 0, 0) };
var fonts = new iTextSharp.text.Font[] { Header1Font, Header2Font, Header3Font, Header4Font, Header5Font, Header6Font };
var colors = new iTextSharp.text.Color[] { new iTextSharp.text.Color(0x9B1C47), new iTextSharp.text.Color(0, 0, 0), new iTextSharp.text.Color(0, 0, 0), new iTextSharp.text.Color(0, 0, 0), new iTextSharp.text.Color(0, 0, 0), new iTextSharp.text.Color(0, 0, 0) };
return CreateFormatted(block.Inline, fonts[block.Level - 1], 0, colors[block.Level - 1]);
}
private Phrase Render(ParagraphBlock block)
{
return CreateFormatted(block.Inline, ParagraphFont, 0, new Color(0, 0, 0));
return CreateFormatted(block.Inline, ParagraphFont, 0, new iTextSharp.text.Color(0, 0, 0));
//_Document.Add(CreateFormatted(block.Inline, Font.HELVETICA, 0, new Color(0, 0, 0), 20));
}
private Phrase CreateFormatted(ContainerInline inlines, Font fontFamily, int fontStyle, Color fontColor)
private Phrase CreateFormatted(ContainerInline inlines, iTextSharp.text.Font fontFamily, int fontStyle, iTextSharp.text.Color fontColor)
{
var phrase = new Phrase();
foreach (var inline in inlines)
@ -322,7 +330,7 @@ namespace AideDeJeu.Pdf
}
return phrase;
}
private Chunk[] CreateChunks(Inline inline, Font fontFamily, int fontStyle, Color fontColor)
private Chunk[] CreateChunks(Inline inline, iTextSharp.text.Font fontFamily, int fontStyle, iTextSharp.text.Color fontColor)
{
switch (inline)
{
@ -332,11 +340,11 @@ namespace AideDeJeu.Pdf
new Chunk()
{
Content = literal.Content.Text.Substring(literal.Content.Start, literal.Content.Length),
Font = new Font(fontFamily.BaseFont, fontFamily.Size, fontStyle, fontColor)
Font = new iTextSharp.text.Font(fontFamily.BaseFont, fontFamily.Size, fontStyle, fontColor)
}
};
case EmphasisInline emphasis:
var childStyle = fontStyle | (emphasis.DelimiterCount == 2 ? Font.BOLD : Font.ITALIC);
var childStyle = fontStyle | (emphasis.DelimiterCount == 2 ? iTextSharp.text.Font.BOLD : iTextSharp.text.Font.ITALIC);
var espans = emphasis.SelectMany(x => CreateChunks(x, fontFamily, childStyle, fontColor));
return espans.ToArray();

View file

@ -1,4 +1,5 @@
using AideDeJeu.Tools;
using AideDeJeu.Pdf;
using AideDeJeu.Tools;
using AideDeJeu.ViewModels.Library;
using AideDeJeu.Views;
using AideDeJeu.Views.Library;
@ -6,6 +7,7 @@ using AideDeJeuLib;
using Rg.Plugins.Popup.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
@ -152,11 +154,11 @@ namespace AideDeJeu.ViewModels
var lastPage = navigationPage.Navigation.NavigationStack.LastOrDefault();
var context = lastPage.BindingContext;
Item item = null;
if(context is ItemDetailViewModel)
if (context is ItemDetailViewModel)
{
item = (context as ItemDetailViewModel).Item;
}
else if(context is ItemsViewModel)
else if (context is ItemsViewModel)
{
item = (context as ItemsViewModel).AllItems;
}
@ -165,10 +167,10 @@ namespace AideDeJeu.ViewModels
var result = await Application.Current.MainPage.DisplayActionSheet("Ajouter à", "Annuler", "Nouvelle liste", vm.BookmarkCollectionNames.ToArray<string>());
if (result != "Annuler")
{
if(result == "Nouvelle liste")
if (result == "Nouvelle liste")
{
int i = 1;
while(vm.BookmarkCollectionNames.Contains(result = $"Nouvelle liste ({i})"))
while (vm.BookmarkCollectionNames.Contains(result = $"Nouvelle liste ({i})"))
{
i++;
}
@ -230,7 +232,7 @@ namespace AideDeJeu.ViewModels
{
get
{
return _NavigateToLinkCommand ?? (_NavigateToLinkCommand = new Command<string>(async(s) => await NavigateToLinkAsync(s)));
return _NavigateToLinkCommand ?? (_NavigateToLinkCommand = new Command<string>(async (s) => await NavigateToLinkAsync(s)));
}
}
@ -261,7 +263,7 @@ namespace AideDeJeu.ViewModels
var filterViewModel = items.GetNewFilterViewModel();
var itemsViewModel = new ItemsViewModel() { AllItems = items, Filter = filterViewModel };
itemsViewModel.LoadItemsCommand.Execute(null);
if(!string.IsNullOrEmpty(with))
if (!string.IsNullOrEmpty(with))
{
var swith = with.Split('_');
for (int i = 0; i < swith.Length / 2; i++)
@ -306,7 +308,7 @@ namespace AideDeJeu.ViewModels
// create the Transparent Popup Page
// of type string since we need a string return
var popup = new InputAlertDialogBase<Tuple<string, PopupResultEnum>>(inputView);
// subscribe to the TextInputView's Button click event
inputView.SaveButtonEventHandler +=
@ -349,5 +351,22 @@ namespace AideDeJeu.ViewModels
return result;
}
private Command<string> _GeneratePDFCommand = null;
public Command<string> GeneratePDFCommand
{
get
{
return _GeneratePDFCommand ?? (_GeneratePDFCommand = new Command<string>(async (markdown) => await ExecuteGeneratePDFCommandAsync(markdown)));
}
}
public async Task ExecuteGeneratePDFCommandAsync(string markdown)
{
using (var stream = new FileStream(Path.Combine(Xamarin.Essentials.FileSystem.CacheDirectory, "test.pdf"), FileMode.Create))
{
PdfService.Instance.MarkdownToPdf(new List<string>() { markdown }, stream);
}
}
}
}
}

View file

@ -14,6 +14,7 @@
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.ToolbarItems>
<ToolbarItem Name="Print" Text="Générer un PDF" Order="Primary" Icon="round_star.png" Command="{Binding Main.Navigator.GeneratePDFCommand}" CommandParameter="{Binding Item.Markdown}" />
<ToolbarItem Name="AddToFavorites" Text="Ajouter aux favoris" Order="Primary" Icon="round_star.png" Command="{Binding Main.Navigator.AddToFavoritesCommand}" />
</ContentPage.ToolbarItems>
<Grid>

View file

@ -17,6 +17,7 @@
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.ToolbarItems>
<ToolbarItem Name="Print" Text="Générer un PDF" Order="Primary" Icon="round_star.png" Command="{Binding Main.Navigator.GeneratePDFCommand}" CommandParameter="{Binding BindingContext.Items.Markdown, Source={x:Reference This}}" />
<ToolbarItem Name="AddToFavorites" Text="Ajouter aux favoris" Order="Primary" Icon="round_star.png" Command="{Binding Main.Navigator.AddToFavoritesCommand}" />
</ContentPage.ToolbarItems>
<Grid>