I'm trying to make a PDF utility app that will allow users to merge and split PDF files. The only problem I have right now is that on load my app shows the pdf file off center. Example
In order to make the PDF file center I have to manually mouse-click the dark grey area that is shown when the PDF file is off center. After that the pdf file will be center like so
So is there anyway possible to make the PDF file centered automatically like the 2nd image?
Code below is how I call webBrowser1 that is rendering the pdf file.
public Form1()
{
InitializeComponent();
string filename = "pdf_example.pdf";
string path = Path.Combine(Environment.CurrentDirectory, filename); // grab pdf file from root program file
webBrowser1.Url = new Uri(path); // <-- input pdf location, WebBrowser Code section, REDACTED
//"button1" == "Load PDF Files", EventHandler
button1.Click += new EventHandler(button1_click);
//"button2" == "Save PDF Files", EventHandler
button2.Click += new EventHandler(button2_click);
//"button3" == "Merge PDF Files", EventHandler
button3.Click += new EventHandler(button3_click);
//"button4" == "Split PDF Files", EventHandler
button4.Click += new EventHandler(button4_click);
}
I was thinking it had to do with the pdf being loaded before the Form1 window was completely loaded. So I tried making a method and set Form1 Shown properties to this method.
void Form1_Shown(object sender, EventArgs e)
{
string filename = "pdf_example.pdf";
string path = Path.Combine(Environment.CurrentDirectory, filename); // grab pdf file from root program file
webBrowser1.Url = new Uri(path); // <-- input pdf location, WebBrowser Code section, REDACTED
}
It didn't work.
Any help would be much appreciated.
Also I completely redid the app to use axAcroPDF control to render PDF instead of webBrowser control to render PDF and I still got the same problem where the PDF file was off-center.
For future reference. The problem was that my monitor resolution was set to 4k. This caused the pdf file to be open off center in IE every-time for some reason. If the program was open in a 1920x1080 resolution monitor then the pdf file is perfectly centered every-time the program starts.
Related
I made my own custom .ttf fonts for my UWP program and put them in Assets folder (vs 2017/2019). They are working well when the Document is processed in RichEditBox. However, when I Save the RTF file and then Open it, my custom fonts are ignored. If I install my custom fonts beforehand into Windows\Fonts folder , then Open file load the document with my custom fonts. Looks like without installing my custom fonts the program does not link them to the document.
Again - I wrote a program with RichEditBox and my custom fonts in that program. When processed - fonts changed, styles changed, etc - everything goes as designed. When I save RTF file with that program, and open that RTF with that (the same) program - color table is OK but my fonts are not shown though the fonts were compiled with that program (BuildAction - Content; CopyToOutputDirectory - Copy always). To simplify - I made button with the information the file contains. Though fonts are compiled (located in Assets folder), the program does not link them to the document.
Actually with that button I tried to reproduce what is described here: RichEditBox (UWP) ignores font and foreground when setting Rtf text However in my case, the RichEditBox shows only fonts installed in Windows\Fonts directory. How to overcome that and either use links to local fonts compiled with my program or make installer install the fonts to Windows\Fonts directory?
How could I use my custom fonts (link them to the document) without installing them or what I need to do my UWP program installs my custom fonts to user's device while installing itself?
This is code for button I used to display text:
private void Page_Click(object sender, RoutedEventArgs e)
{
string myRtfString = #"{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil MyFont;}{\f1\fnil MyFont1;}{\f2\fnil MyFont2;}} {\colortbl ;\red0\green0\blue0;\red255\green255\blue255;\red255\green100\blue0;} {\*\generator Riched20 10.0.18362}\viewkind4\uc1 \pard\sl480\slmult1\qj\cf1\highlight2\f0\fs36 tt\highlight3\f1 g\f0 acgt\f2 c\highlight2\f0 tt\highlight0\par}";
editor.Document.SetText(TextSetOptions.FormatRtf, myRtfString);
}
This is XAML for RichEditBox:
<RichEditBox
x:Name="editor"
Height="200"
FontFamily="Assets/Fonts/MyFont.ttf#MyFont"
FontSize="24" RelativePanel.Below="openFileButton"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.AlignRightWithPanel="True" />
Gosha, this way you can apply at least one of the fonts to that .rtf file - see below. For others, I think, you need to use either map information from that .rtf or make an additional map of your own. That will be some "trabajo", but what can you do?
private void applyMyFonts()
{
string TextOut;
MyRichEditBox.Document.GetText(TextGetOptions.None, out TextOut);
MyRichEditBox.Document.Selection.SetRange(0, TextOut.Length);
MyRichEditBox.Document.Selection.CharacterFormat.Name = "Assets/Fonts/MyFont.ttf#MyFont";
}
private async void OpenButton_Click(object sender, RoutedEventArgs e)
{
Windows.Storage.Pickers.FileOpenPicker open =
new Windows.Storage.Pickers.FileOpenPicker();
open.SuggestedStartLocation =
Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
open.FileTypeFilter.Add(".rtf");
Windows.Storage.StorageFile file = await open.PickSingleFileAsync();
if (file != null)
{
try
{
Windows.Storage.Streams.IRandomAccessStream randAccStream =
await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
// Load the file into the Document property of the RichEditBox.
MyRichEditBox.Document.LoadFromStream(Windows.UI.Text.TextSetOptions.FormatRtf, randAccStream);
}
catch (Exception)
{
ContentDialog errorDialog = new ContentDialog()
{
Title = "File open error",
Content = "Sorry, I couldn't open the file.",
PrimaryButtonText = "Ok"
};
await errorDialog.ShowAsync();
}
}
applyMyfonts();
}
I have a panel which displays a form which displays a webbrowser. In the UI designer I added the Url of my .html file and it worked fine.
But now I am using my application on a different system and thus the path to the .html does not exists.
So I have to do it in code and use string sspBaseFile = System.AppDomain.CurrentDomain.BaseDirectory;
In this function I am trying to get it to work. The form has a UI element called webbrowser1
private void SSP_Load(object sender, EventArgs e)
{
this.webBrowser1.DocumentTitleChanged += new EventHandler(webBrowser1_DocumentTitleChanged);
this.Controls.Add(webBrowser1);
webBrowser1.AllowNavigation = true;
this.webBrowser1.Navigate(new Uri(sspBaseFile + "frmSSPeditor.html"));
}
Why isn't the html displayed? I verified that the .html is in the correct place, next to the binary.
I have a menustrip that has a tab I have created to be labeled "Contents". I want to be able to click on the Content tab and a window pop up with a pdf file listing the contents. I have the pdf file that lists the contents saved to my desktop. Is there a way to add this pdf file to visual studio so that when I click on the Content tab, the pdf file will be opened?
I do not want to have to click another button to search my computer for the file such as using OpenFileDialog. I just want to click the Contents tab and have it open a window with the pdf file.
There are multiple ways of doing that.
1) One way would be to launch a process from your app that will open the default registered viewer of PDF files (such as Adobe Reader) on your PC and pass the full path to the PDF file as a parameter:
Here you can find out ho to determine the path to the default handler application by file extension (".pdf" in your case):
http://www.pinvoke.net/default.aspx/shlwapi/AssocQueryString.html
string execPath = GetAssociation(".pdf");
Once you know the path to the executable, you can launch it with a path to your PDF file as a parameter:
using System.Diagnostics;
...
// Start new process
Process.Start(execPath, "C:\\myfile.pdf").WaitForExit(0);
2) Another way would be to create a Windows form in your app and add web browser control to it. The web browser control can then be programmatically "navigated to" your specific PDF file. That is assuming that your Internet Explorer can display PDF files already by using something like Adobe Reader within its window, i.e. as an inline attachment:
Add a reference from your project to Microsoft Internet Controls 1.1 (Right-click on References > Add reference... > COM).
In your form code (here panePdfViewer is a placeholder System.Windows.Forms.Panel control):
private AxSHDocVw.AxWebBrowser axWebBrowser;
...
private void InitializeWebControl()
{
this.SuspendLayout();
this.axWebBrowser = new AxSHDocVw.AxWebBrowser();
((ISupportInitialize)(this.axWebBrowser)).BeginInit();
this.axWebBrowser.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom)
| AnchorStyles.Left)
| AnchorStyles.Right)));
this.axWebBrowser.Enabled = true;
this.axWebBrowser.Location = this.panelPdfViewer.Location;
this.axWebBrowser.Size = this.panelPdfViewer.Size;
((ISupportInitialize)(this.axWebBrowser)).EndInit();
this.axWebBrowser.Visible = false;
this.Controls.Add(this.axWebBrowser);
this.ResumeLayout(false);
}
and then:
// Clear browser
object blank = "about:blank";
this.axWebBrowser.Navigate2(ref blank);
// Display file
object loc = "file:///" + System.IO.Path.GetFullPath(fileName).Replace('\\', '/');
object null_obj_str = null;
object null_obj = null;
this.axWebBrowser.Navigate2(ref loc, ref null_obj, ref null_obj, ref null_obj_str, ref null_obj_str);
3) A third way is to use a third party control library that can display PDF files.
Hello i builded application with log that sow me what i am doing(where i saving files).
Sow my goal here is build links to all my new files that i created and open them by click
This my code
StreamWriter sw = new StreamWriter(Path.Combine(filepath), false, Encoding.GetEncoding("Windows-1255"));
sw.Write(Encoding.GetEncoding("Windows-1255").GetString(byteArray1255));
sw.Close();
rtx_Log.Text+= filepath;
here i created some file and i just want to show the pass in richtextbox and open it by click.
If RichTextBox.DetectUrls is set to true, the control will automatically detect links from the protocol and create a link.
"My File: file://c:/MyFile.txt" will display the file:// part as a link.
The RichTextBox.LinkClicked event is fired when the user clicks a link - and you can act on it as needed.
private void RichTextBox1OnLinkClicked(object sender, LinkClickedEventArgs e)
{
var filePath = new Uri(e.LinkText).AbsolutePath;
}
I have some trouble using the webbrowser control in C#, especially when I print it. The thing is, I have a Barcode in my html file and I have to print it (I use a font to create the code). When I open the html file with Firefox or any other Web browser, my barecode is good and I can scan it. But, when I open my file with my webbrowser in c#, or when I print it, the webbrowser ad 2 characters after the barecode. And, when I print the file, my document is not centered, it's like the webbrowser add a margin-left property. So my question is, is there any way to print an html file, using a webbroser, exactly how I see the html file when I use firefox or chrome for example. Here is the code I use.
curDir = Directory.GetCurrentDirectory();
webBrowserA4.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(PrintDocument);
webBrowserA4.Url = new Uri(String.Format("file:///{0}/print.html", curDir));
private void PrintDocument(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// Print the document now that it is fully loaded.
((WebBrowser)sender).Print();
// Dispose the WebBrowser now that the task is complete.
((WebBrowser)sender).Dispose();
}
EDIT: So, now, I have another problem, here is the screen of what my file looks like when I print it: http://imgur.com/q7ovEA1 As you can see, there is a "margin-left", and I can't remove it. I also want to remove this "page 1 of 1" and the Title.